Cod sursa(job #3320000)

Utilizator ElizaTElla Rose ElizaT Data 4 noiembrie 2025 03:04:06
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 2e5 + 10;
int v[NMAX],heap[NMAX],pos[NMAX];
int n,l,nr;

void push(int x) {
    while ((x >> 1) && v[heap[x]] < v[heap[(x >> 1)]]) {
        swap(heap[x], heap[(x >> 1)]);
        pos[heap[x]] = x;
        pos[heap[(x >> 1)]] = (x >> 1);
        x >>= 1;
    }
}
void pop(int x) {
    int y = 0;
    while (x != y) {
        y = x;
        if ((y << 1) <= l && v[heap[x]] > v[heap[(y << 1)]])
            x = (y << 1);
        if ((y << 1) + 1 <= l && v[heap[x]] > v[heap[(y << 1) + 1]])
            x = (y << 1) + 1;
        swap(heap[x], heap[y]);
        pos[heap[x]] = x;
        pos[heap[y]] = y;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ifstream fin("heapuri.in");
    ofstream fout("heapuri.out");
    int i,x,op;
    fin >> n;
    for (int i = 1;i <= n;i++) {
        fin >> op;
        if (op < 3)
            fin >> x;
        if (op == 1) {
            l++;
            nr++;
            v[nr] = x;
            heap[l] = nr;
            pos[nr] = l;
            push(l);
        }
        else if (op == 2) {
            v[x] = -1;
            push(pos[x]);
            pos[heap[1]] = 0;
            heap[1] = heap[l--];
            pos[heap[1]] = 1;
            if (l > 1)
                pop(1);
        }
        else
            fout << v[heap[1]] << '\n';
    }
    return 0;
}