Pagini recente » Cod sursa (job #3363486) | Cod sursa (job #3363487) | Cod sursa (job #3362229) | Cod sursa (job #3363485) | Cod sursa (job #3362235)
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(time(0));
const int nmax = 200'000;
struct node {
node(int v) : v(v) {}
int v;
node *l = nullptr, *r = nullptr, *p = nullptr;
};
node* merge(node* a, node* b) {
if(!a || !b) return a ? a : b;
if(a->v > b->v) swap(a, b);
if(rng() & 1) swap(a->l, a->r);
a->l = merge(a->l, b);
if(a->l) a->l->p = a;
return a;
}
node* heap = nullptr;
void del(node *u) {
if(u == heap) {
heap = merge(heap->l, heap->r);
return;
}
if(u->p->l == u) u->p->l = nullptr;
else u->p->r = nullptr;
heap = merge(heap, merge(u->l, u->r));
}
node* pos[nmax];
int cnt = 0;
int n;
int main() {
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 0; i < n; i ++) {
int t; cin >> t;
if(t == 1) {
int x; cin >> x;
pos[cnt] = new node(x);
heap = merge(heap, pos[cnt ++]);
} else if(t == 2) {
int x; cin >> x; x --;
del(pos[x]);
} else {
cout << heap->v << '\n';
}
}
}