Cod sursa(job #563761)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in ("heapuri.in");
ofstream out ("heapuri.out");
const int N = 200002;
int lung_h, h[N], que[N], h_pos[N];
void up_heap(int query, int nod) {
if (h[nod / 2] > h[nod] && nod / 2) {
swap(h[nod / 2], h[nod]);
swap(que[nod / 2], que[nod]);
swap(h_pos[query],h_pos[que[nod]]);
up_heap(query, nod / 2);
}
}
void insert_heap(int query, int val) {
++ lung_h;
h_pos[query] = lung_h;
h[lung_h] = val;
que[lung_h] = query;
up_heap(query, lung_h);
}
void down_heap(int query, int nod) {
int p = nod;
if (h[2 * nod] < h[p] && 2 * nod <= lung_h)
p = 2 * nod;
if (h[2 * nod + 1] < h[p] && 2 * nod + 1 <= lung_h)
p = 2 * nod + 1;
if (p != nod) {
swap(h[nod], h[p]);
swap(que[nod], que[p]);
swap(h_pos[que[p]],h_pos[query]);
down_heap(query, p);
}
}
void remove_heap(int val) {
int newnod = h_pos[val], newq = que[lung_h];
swap(h[h_pos[val]], h[lung_h]);
swap(que[h_pos[val]], que[lung_h]);
swap(h_pos[que[h_pos[val]]], h_pos[val]);
-- lung_h;
up_heap(newq , newnod);
down_heap(newq, newnod);
}
int main() {
int n, tip, x, query = 1;
in >> n;
for (int i = 1; i <= n; ++ i) {
in >> tip;
if (tip == 3) {
out << h[1] << '\n';
continue;
}
in >> x;
if (tip == 1) {
insert_heap(query, x);
++ query;
continue;
}
remove_heap(x);
}
return 0;
}