Cod sursa(job #1920610)

Utilizator duesakBourceanu Cristian duesak Data 10 martie 2017 08:40:00
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include<fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int hp[200001];
int poz[200001];
int vl[200001];
int lhp=0;
void inserthp(int el,int x){
    ++lhp;
    hp[lhp]=x;
    poz[x]=lhp;
    int y=lhp,aux;
    vl[x]=el;
    while(vl[hp[y]]<vl[hp[y>>1]]&&0<y>>1){
        aux=hp[y];
        hp[y]=hp[y>>1];
        hp[y>>1]=aux;
        poz[hp[y]]=y;
        poz[hp[y>>1]]=y>>1;
        y>>=1;
    }
}
void deletehp(int x){
    hp[poz[x]]=hp[lhp];
    poz[hp[lhp]]=poz[x];
    lhp--;
    int y=poz[x],aux;
    while(1){
        if(y<<1<=lhp&&vl[hp[y]]>vl[hp[y<<1]]&&(vl[hp[y<<1]]<=vl[hp[(y<<1)+1]]||lhp<(y<<1)+1)){
            aux=hp[y];
            hp[y]=hp[y<<1];
            hp[y<<1]=aux;
            poz[hp[y]]=y;
            poz[hp[y<<1]]=y<<1;
            y<<=1;
        }
        else if(1+y<<1<=lhp&&vl[hp[y]]>vl[hp[1+(y<<1)]]&&vl[hp[1+(y<<1)]]<vl[hp[y<<1]]){
            aux=hp[y];
            hp[y]=hp[(y<<1)+1];
            hp[(y<<1)+1]=aux;
            poz[hp[y]]=y;
            poz[hp[(y<<1)+1]]=(y<<1)+1;
            y<<=1;
            ++y;
        }
        else break;
    }
}
int main(){
    int n,i,x,el,p;
    fin>>n;
    x=0;
    for(i=1;i<=n;i++){
        fin>>p;
        if(p==1){
            fin>>el;
            ++x;
            inserthp(el,x);
        }
        if(p==2){fin>>el;deletehp(el);}
        if(p==3)fout<<vl[hp[1]]<<'\n';
    }
    fin.close();
    fout.close();
    return 0;
}