Cod sursa(job #2707448)

Utilizator DordeDorde Matei Dorde Data 17 februarie 2021 00:31:29
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <fstream>

using namespace std;
ifstream f ("heapuri.in");
ofstream g ("heapuri.out");
int const N = 2e5 + 1;
class Heap{
public :
    int v [N] , k , in , p [N] , h [N];
    int top (){
        return v [h [1]];
    }
    void chg (int x , int y){
        swap (h [x] , h [y]);
        p [h [x]] = x;
        p [h [y]] = y;
    }
    void up (int node){
        if (! node || v [h [node]] >= v [h [node / 2]])
            return;
        chg (node , node / 2);
        up (node / 2);
    }
    void down (int node){
        int nxt = node;
        if (node * 2 <= k && v [h [node * 2]] < v [h [nxt]])
            nxt = node * 2;
        if (node * 2 + 1 <= k && v [h [node * 2 + 1]] < v [h [nxt]])
            nxt = node * 2 + 1;
        if (nxt == node)
            return;
        chg (node , nxt);
        down (nxt);
    }
    void add (int x){
        v [++ in] = x;
        h [++ k] = in;
        p [in] = k;
        up (k);
    }
    void rem (int x){
        v [x] = -1;
        up (p [x]);
        chg (1 , k --);
        if (k)
            down (1);
    }
} H;
int main()
{
    int n;
    f >> n;
    for(int i = 1 ; i <= n ; ++ i){
        int c , x;
        f >> c;
        if (c == 3){
            g << H . top () << '\n';
            continue;
        }
        f >> x ;
        if (c == 1)
            H . add (x);
        else
            H . rem (x);
    }
    return 0;
}