Cod sursa(job #2695808)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 14 ianuarie 2021 16:09:36
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <fstream>

using namespace std;

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

const int qmax = 200000;

int p[qmax+1];

int h[qmax+1], hd[qmax+1];

void hswap ( int *h, int x, int y) {
    int aux = h[x];
    h[x] = h[y];
    h[y] = aux;
    return;
}

void hpercolate ( int *h, int x) {
    int xf = x/2;
    if ( xf!=0 && h[xf]>h[x] ) {
        hswap(h, x, xf);
        hpercolate(h, xf);
    }
    return;
}

void hsift( int *h, int x ) {
    int hn = h[0], xs = x*2;
    if ( xs<=hn ) {
        if ( xs+1<=hn && h[xs+1]<h[xs] ) {
            ++ xs;
        }
        if ( h[xs]<h[x] ) {
            hswap(h, x, xs);
            hsift(h, xs);
        }
    }
    return;
}

int main(  ) {
    int q;
    fin >> q;
    for ( int qi = 1; qi<=q; ++ qi ) {
        int x;
        fin >> x;
        if ( x==1 ) {
            int y;
            fin >> y;
            ++ p[0];
            p[p[0]] = y;
            ++ h[0];
            h[h[0]] = y;
            hpercolate(h, h[0]);
        } else if ( x==2 ) {
            int y;
            fin >> y;
            ++hd[0];
            hd[hd[0]] = p[y];
            hpercolate(hd, hd[0]);
        } else {
            while ( hd[0]>0 && h[1]==hd[1] ) {
                hswap(h, 1, h[0]);
                --h[0];
                hsift(h, 1);
                hswap(hd, 1, hd[0]);
                --hd[0];
                hsift(hd, 1);
            }
            fout << h[1] << "\n";
        }
    }

    return 0;
}