Cod sursa(job #2616057)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 16 mai 2020 15:35:39
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>
#define maxn 200010

using namespace std;

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

int N, L, NR, A[maxn], heap[maxn], pos[maxn];

inline void Push (int x) {
    while (x / 2 && A[heap[x]] < A[heap[x / 2]]) {
        swap (heap[x], heap[x / 2]);
        pos[heap[x]] = x;
        pos[heap[x / 2]] = x / 2;
        x /= 2;
    }
}

inline void Pop (int x) {
    int y = 0;
    while (x != y) {
        y = x;
        if (y * 2 <= L && A[heap[x]] > A[heap[y * 2]]) x = y * 2;
        if (y * 2 + 1 <= L && A[heap[x]] > A[heap[y * 2 + 1]]) x = y * 2 + 1;
        swap (heap[x], heap[y]);
        pos[heap[x]] = x;
        pos[heap[y]] = y;
    }
}

inline void Read_Solve () {
    int i, x, op;
    fin >> N;
    for (i = 1; i <= N; ++i) {
        fin >> op;
        if (op < 3) fin >> x;
        if (op == 1) {
            L ++, NR ++;
            A[NR] = x;
            heap[L] = NR;
            pos[NR] = L;
            Push (L);
        }
        if (op == 2) {
            A[x] = -1;
            Push (pos[x]);
            pos[heap[1]] = 0;
            heap[1] = heap[L --];
            pos[heap[1]] = 1;
            if (L > 1) Pop (1);
        }
        if (op == 3) fout << A[heap[1]] << '\n';
    }
}

inline void Close () {
    fin.close ();
    fout.close ();
}

int main() {
    Read_Solve ();
    Close ();
    return 0;
}