Cod sursa(job #2376303)

Utilizator RaduMirceaAndreiRadu Mircea Andrei RaduMirceaAndrei Data 8 martie 2019 14:47:04
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
# include <fstream>
# define DIM 200010
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int h[DIM],v[DIM],poz[DIM],t,x,n,nh,i,op;
void add(){
    int c=nh,p=c/2;
    while(c!=1)
        if(v[h[c]]<v[h[p]]){
            swap(h[c],h[p]);
            poz[h[c]]=c;
            poz[h[p]]=p;
            c=p;
            p/=2;

        }
        else
            break;
}
void off(int val){
    int p=val,c=2*p;
    swap(h[val],h[nh]);
    nh--;
    while(c<=nh){
        if(c<nh&&v[h[c+1]]<v[h[c]])
            c++;
        if(v[h[c]]<v[h[p]]){
            swap(h[c],h[p]);
            poz[h[c]]=c;
            poz[h[p]]=p;
            p=c;
            c*=2;

        }
        else
            break;
    }
}
int main () {
    fin>>t;
    for(i=1;i<=t;i++){
        fin>>op;
        if(op==3){
            fout<<v[h[1]]<<"\n";
            continue;
        }
        fin>>x;
        if(op==1){
            n++;
            v[n]=x;
            nh++;
            poz[n]=nh;
            h[nh]=n;
            add();
            continue;
        }
        off(poz[x]);
    }
    return 0;
}