Cod sursa(job #3362223)

Utilizator pkseVlad Bondoc pkse Data 4 august 2026 14:54:19
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>
using namespace std;

mt19937 rng(time(0));

struct node {
    node(int v, int p) : v(v), p(p) {}
    int v, p;
    node *l = nullptr, *r = 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);
    return a;
}

const int nmax = 200'000;

node* heap = nullptr;
int cnt = 0;
bool todel[nmax];

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;
            heap = merge(heap, new node(x, cnt ++));
        } else if(t == 2) {
            int x; cin >> x; x --;
            todel[x] = true;
        } else {
            while(todel[heap->p]) {
                heap = merge(heap->l, heap->r);
            }
            cout << heap->v << '\n';
        }
    }
}