Pagini recente » Cod sursa (job #749800) | Cod sursa (job #2480467) | Cod sursa (job #1000960) | Cod sursa (job #2957539) | Cod sursa (job #2294612)
#include <bits/stdc++.h>
using namespace std;
FILE *fin = fopen ("heapuri.in", "r"), *fout = fopen ("heapuri.out", "w");
const int MAXN = 2e5;
int yep[MAXN + 1], poz[MAXN + 1], v[MAXN + 1];
int sz, nr;
void bambo (int x, int y) {
swap (yep[x], yep[y]);
poz[yep[x]] = x;
poz[yep[y]] = y;
}
void up (int nod) {
while (nod != 1 && v[yep[nod]] < v[yep[nod / 2]]) {
bambo (nod, nod / 2);
nod = nod / 2;
}
}
void chromosomes (int nod) {
int hump = nod;
if (2 * nod <= sz && v[yep[2 * nod]] < v[yep[hump]])
hump = 2 * nod;
if (2 * nod + 1 <= sz && v[yep[2 * nod + 1]] < v[yep[hump]])
hump = 2 * nod + 1;
if (hump != nod) {
bambo (nod, hump);
chromosomes (hump);
}
}
void erase (int x) {
if (x < sz) {
yep[x] = yep[sz--];
poz[yep[x]] = x;
chromosomes (x);
up (x);
}
else {
sz--;
}
}
void add (int x) {
yep[++sz] = x;
poz[x] = sz;
up (sz);
}
// val prea mari
int main() {
int t, type, x;
fscanf (fin, "%d", &t);
sz = 0;
nr = 0;
while (t--) {
fscanf (fin, "%d", &type);
if (type == 1) {
fscanf (fin, "%d", &x);
v[++nr] = x;
add (nr);
}
else if (type == 2) {
fscanf (fin, "%d", &x);
erase (poz[x]);
}
else {
fprintf (fout, "%d\n", v[yep[1]]);
}
}
fclose (fin);
fclose (fout);
return 0;
}