Pagini recente » Cod sursa (job #205360) | Cod sursa (job #383795) | Cod sursa (job #356337) | Cod sursa (job #754825) | Cod sursa (job #1202518)
#include <iostream>
#include <fstream>
using namespace std;
#define NMAX 200001
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int i, n, p, cod, x, N;
int val[NMAX], poz[NMAX];
int H[NMAX];
void Swap(int i, int j) {
swap(H[i], H[j]);
poz[ H[i] ] = i;
poz[ H[j] ] = j;
}
void UP_heap(int u) {
H[++n] = u;
u = n;
while (u != 1 && val[H[u]] < val[H[u / 2]])
Swap(u, u / 2), u /= 2;
}
void DOWN_heap(int u) {
u = poz[u];
Swap(u, n--);
while (1) {
int m = u;
if (u * 2 <= n && val[H[u * 2]] < val[H[u]]) m = u * 2;
if (u * 2 + 1 <= n && val[H[u * 2 + 1]] < val[H[m]]) m = u * 2 + 1;
if (u == m)
break;
Swap(u, m);
u = m;
}
}
int main() {
fin >> N;
bool ok;
for (i = 1; i <= N; ++i) {
fin >> cod;
if (cod != 3) {
fin >> x;
if (cod == 1) {
val[++p] = x;
poz[p] = p;
UP_heap(p);
continue;
}
DOWN_heap(x);
}
else
fout << val[H[1]] << '\n';
}
return 0;
}