Cod sursa(job #3357864)

Utilizator TestLicenta123Test Test TestLicenta123 Data 13 iunie 2026 18:28:47
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <fstream>

using namespace std;

ifstream cin("heapuri.in");
ofstream cout("heapuri.out");

int v[200010];
int h[200010];
int pos[200010];
int sz, n;
bool deleted[200010];

void UpHeap(int nod) {
    while ((nod >> 1) > 0) {
        if (v[h[nod >> 1]] > v[h[nod]]) {
            swap(h[nod >> 1], h[nod]);
            pos[h[nod >> 1]] = nod >> 1;
            pos[h[nod]] = nod;
            nod >>= 1;
        } else {
            break;
        }
    }
}

void DownHeap(int nod) {
    while (nod * 2 <= sz) {
        int child = nod * 2;
        if (child + 1 <= sz && v[h[child + 1]] < v[h[child]]) {
            child++;
        }
        if (v[h[child]] < v[h[nod]]) {
            swap(h[nod], h[child]);
            pos[h[nod]] = nod;
            pos[h[child]] = child;
            nod = child;
        } else {
            break;
        }
    }
}

void TypeOne(int x) {
    n++;
    v[n] = x;
    sz++;
    h[sz] = n;
    pos[n] = sz;
    UpHeap(sz);
}

void TypeTwo(int x) {
    deleted[x] = 1;
    int p = pos[x];
    if (p <= sz) {
        h[p] = h[sz];
        pos[h[sz]] = p;
        sz--;
        if (p <= sz) {
            UpHeap(p);
            DownHeap(p);
        }
    }
}

int TypeThree() {
    while (deleted[h[1]]) {
        sz--;
    }
    return v[h[1]];
}

int main() {
    int t;
    cin >> t;
    for (int i = 1; i <= t; i++) {
        int type;
        cin >> type;
        if (type == 1) {
            int x;
            cin >> x;
            TypeOne(x);
        } else if (type == 2) {
            int x;
            cin >> x;
            TypeTwo(x);
        } else if (type == 3) {
            cout << TypeThree() << '\n';
        }
    }
    return 0;
}