Pagini recente » Cod sursa (job #830293) | Cod sursa (job #368172) | Cod sursa (job #3268867) | Cod sursa (job #2194348) | Cod sursa (job #3266294)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define NMAX 200005
int h[NMAX], poz[NMAX], v[NMAX];
int N, k;
void schimba(int x, int y) {
swap(h[x], h[y]);
poz[h[x]] = x;
poz[h[y]] = y;
}
void sift(int poz) {
int nod = poz;
if (2 * poz <= N && v[h[2 * poz]] < v[h[nod]]) {
nod = 2 * poz;
}
if (2 * poz + 1 <= N && v[h[2 * poz + 1]] < v[h[nod]]) {
nod = 2 * poz + 1;
}
if (nod != poz) {
schimba(nod, poz);
sift(nod);
}
}
void perlocate(int poz) {
while (poz > 1 && v[h[poz]] < v[h[poz / 2]]) {
schimba(poz, poz / 2);
poz /= 2;
}
}
void del(int poz) {
schimba(poz, N--);
perlocate(poz);
sift(poz);
}
int main() {
int n;
fin >> n;
for (int i = 1; i <= n; ++i) {
int op;
fin >> op;
if (op == 3) {
fout << v[h[1]] << '\n';
} else if (op == 2) {
int x;
fin >> x;
del(poz[x]);
} else {
fin >> v[++k];
h[++N] = k;
poz[k] = N;
perlocate(N);
}
}
return 0;
}