Cod sursa(job #2695822)

Utilizator GligarEsterabadeyan Hadi Gligar Data 14 ianuarie 2021 17:19:33
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>

using namespace std;

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

const int nqmax=200000;

int h[nqmax+1], p[nqmax+1], hd[nqmax+1];

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

void hpercolate(int *h, int x){
    if(x>1&&h[x/2]>h[x]){
        hswap(h,x/2,x);
        hpercolate(h,x/2);
    }
}

void hsieve(int *h, int x){
    int hn=h[0];
    if(hn>=2*x+1&&(h[x*2+1]<h[x]||h[x*2]<h[x])){
        if(h[x*2+1]>h[x*2]){
            hswap(h,x,x*2);
            hsieve(h,x*2);
        }else{
            hswap(h,x,x*2+1);
            hsieve(h,x*2+1);
        }
    }else if(hn>=2*x&&h[x*2]<h[x]){
        hswap(h,x*2,x);
        hsieve(h,x*2);
    }
}

int main(){
    int nq;
    fin>>nq;
    int pn=0;
    for(int cq=1;cq<=nq;cq++){
        int x;
        fin>>x;
        if(x==1){
            int y;
            fin>>y;
            h[0]++;
            h[h[0]]=y;
            pn++;
            p[pn]=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,h[0],1);
                h[0]--;
                hsieve(h,1);
                hswap(hd,hd[0],1);
                hd[0]--;
                hsieve(hd,1);
            }
            fout<<h[1]<<"\n";
        }
    }
    return 0;
}