Cod sursa(job #2616050)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 16 mai 2020 15:13:37
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
#define nmax 200005

using namespace std;

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

int n, heap[nmax], val[nmax], poz[nmax], i;

inline void Swap (int x, int y) {
    swap (heap[x], heap[y]);
    swap (poz[heap[x]], poz[heap[y]]);
}

inline bool fcmp (int x,int y) {
    return x <= y;
}

inline void UpHeap (int x) {
    int tata = x / 2;
    if (tata && fcmp (val[heap[x]],val[heap[tata]])) {
        Swap (x, tata);
        UpHeap (tata);
    }
}

inline void DownHeap (int x) {
    int son = x * 2;
    if (son + 1 <= n && fcmp (val[heap[son + 1]], val[heap[son]])) son ++;
    if (son <= n && fcmp(val[heap[son]], val[heap[x]])) {
        Swap (son, x);
        DownHeap (son);
    }
}

inline void Insert (int x) {
    i ++;
    val[i] = x;
    n ++;
    heap[n] = i;
    poz[i] = n;
    UpHeap (n);
}

inline void Erase (int x) {
    Swap (x, n);
    n --;
    DownHeap (x);
}

inline void Read_Solve () {
    int Q, op, x;
    fin >> Q;
    while (Q --) {
        fin >> op;
        if (op == 1) {
            fin >> x;
            Insert (x);
        }
        if (op == 2) {
            fin >> x;
            Erase (poz[x]);
        }
        if (op == 3) fout << val[heap[1]] << '\n';
    }
}

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

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