Cod sursa(job #1199954)

Utilizator livliviLivia Magureanu livlivi Data 21 iunie 2014 12:41:46
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include<cstdio>
using namespace std;
int h[200001];
int pos[200001];
int val[200001];
int k;

void add(int x,int i){
    k++;
    pos[i]=k;

    while(x<val[h[pos[i]/2]] &&pos[i]>1){
        h[pos[i]]=h[pos[i]/2];
        pos[h[pos[i]]]=pos[i];
        pos[i]/=2;
    }
    h[pos[i]]=i;
}

void sterge(int i){
    i=pos[i];

    while(val[h[k]]<val[h[i/2]] &&i>1){
        h[i]=h[i/2];
        pos[h[i]]=i;
        i/=2;
    }
    while((val[h[k]]>val[h[i*2]] &&i*2<=k) ||(val[h[k]]>val[h[i*2+1]] &&i*2+1<=k)){
        if (i*2+1>k){
            h[i]=h[i*2];
            i*=2;
        }
        else
        if (val[h[i*2]]<val[h[i*2+1]]){
            h[i]=h[i*2];
            i*=2;
        }
        else {
            h[i]=h[i*2+1];
            i=i*2+1;
        }
    }

    h[i]=h[k];
    pos[h[k]]=i;
    k--;
}

int main(){
    freopen ("heapuri.in","r",stdin);
    freopen ("heapuri.out","w",stdout);
    int n,i,m,a;
    scanf ("%d",&n);
    m=1;
    for(i=1;i<=n;i++){
        scanf ("%d",&a);
        if (a==3) printf ("%d\n",val[h[1]]);
        else
        if (a==1){
            scanf ("%d",&val[m]);
            add(val[m],m);
            m++;
        }
        else {
            scanf ("%d",&a);
            sterge(a);
        }
    }
    return 0;
}