Cod sursa(job #1812284)

Utilizator RaduMirceaAndreiRadu Mircea Andrei RaduMirceaAndrei Data 21 noiembrie 2016 22:33:59
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
# include <fstream>
# define DIM 200010
# define val first
# define nr second
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
pair<int,int> h[DIM];
int poz[DIM],n,m,k,x,i,op,r;
void insert(int x){
    h[++k].first=x;
    h[k].nr=r;
    poz[r]=k;
    int p=k/2,c=k;
    while(p){
        if(h[p].val>h[c].val){
            swap(h[c],h[p]);
            poz[h[c].nr]=c;
            poz[h[p].nr]=p;
            c=p;
            p/=2;
        }
        else
            break;
    }
}
void erase(int t){
    int p=t,c=2*t;
    swap(h[k--],h[t]);
    poz[h[t].nr]=t;
    while(c<=k){
        if(h[c].val>h[c+1].val&&c<k)
            c++;
        if(h[p].val>h[c].val){
            swap(h[c],h[p]);
            poz[h[c].nr]=c;
            poz[h[p].nr]=p;
            p=c;
            c*=2;
        }
        else
            break;
    }
}
int main () {
    fin>>n;
    for(i=1;i<=n;i++){
        fin>>op;
        if(op==1){
            fin>>x;
            r++;
            insert(x);
            continue;
        }
        if(op==3){
            fout<<h[1].val<<"\n";
            continue;
        }
        fin>>x;
        erase(poz[x]);
    }
    return 0;
}