Cod sursa(job #2695799)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 14 ianuarie 2021 15:56:48
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], pn;

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

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 hn, int x ) {
    int 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, hn, 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;
            ++ pn;
            p[pn] = y;
            ++ hn;
            h[hn] = y;
            hpercolate(h, hn);
        } else if ( x==2 ) {
            int y;
            fin >> y;
            ++hdn;
            hd[hdn] = p[y];
            hpercolate(hd, hdn);
        } else {
            while ( hdn>0 && h[1]==hd[1] ) {
                hswap(h, 1, hn);
                --hn;
                hsift(h, hn, 1);
                hswap(hd, 1, hdn);
                --hdn;
                hsift(hd, hdn, 1);
            }
            fout << h[1] << "\n";
        }
    }

    return 0;
}