Cod sursa(job #2375986)

Utilizator RaduMirceaAndreiRadu Mircea Andrei RaduMirceaAndrei Data 8 martie 2019 13:13:46
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
# include <fstream>
# define DIM 200010
# define f first
# define s second
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
pair<int,int> h[DIM];
int poz[DIM],n,k,q,r,op,x;
void add(int val){
    h[++n].f=val;
    h[n].s=k;
    int c=n,p=c/2;
    while(p!=0)
        if(h[p].f>h[c].f){
            swap(h[p],h[c]);
            poz[h[p].s]=p;
            poz[h[c].s]=c;
            c=p;
            p/=2;
        }
        else
            break;
}
void erase(int Poz){
    h[Poz]=h[n--];
    int p=Poz,c=2*p;
    while(c<=n){
        if(c<n&&h[c+1].f<h[c].f)
            c++;
        if(h[p].f>h[c].f){
            swap(h[p],h[c]);
            poz[h[p].s]=p;
            poz[h[c].s]=c;
            p=c;
            c*=2;
        }
        else
            break;
    }
}
int main () {
    fin>>q;
    for(r=1;r<=q;r++){
        fin>>op;
        if(op==3){
            fout<<h[1].f<<"\n";
            continue;
        }
        fin>>x;
        if(op==1){
            poz[++k]=n+1;
            add(x);
        }
        else
            erase(poz[x]);
    }
    return 0;
}