Cod sursa(job #1182258)

Utilizator livliviLivia Magureanu livlivi Data 5 mai 2014 16:42:16
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include<cstdio>
int h[200001];
int t[200001];
int v[200001];
void schimba(int x,int y){
    int aux;
    aux=t[h[x]];
    t[h[x]]=t[h[y]];
    t[h[y]]=aux;
    aux=h[x];
    h[x]=h[y];
    h[y]=aux;
}
void urca(int x){
    while(v[h[x]]<v[h[x/2]] &&x>1){
        schimba(x,x/2);
        x/=2;
    }
}
void coboara(int x){
    int fd=x*2,fs=x*2+1,bun=x;
    if (v[h[fd]]<v[h[bun]]) bun=fd;
    if (v[h[fs]]<v[h[bun]]) bun=fs;
    if (bun!=x){
        schimba(x,bun);
        coboara(bun);
    }
}
int main(){
    freopen ("heapuri.in","r",stdin);
    freopen ("heapuri.out","w",stdout);
    int n,i,p,hn,x;
    scanf ("%d%d%d",&n,&p,&v[1]);
    t[1]=1;
    h[1]=1;
    hn=1;
    for(i=2;i<=n;i++){
        scanf ("%d",&p);
        if (p==3) printf ("%d\n",v[h[1]]);
        else
        if (p==1){
            hn++;
            scanf ("%d",&v[hn]);
            t[hn]=hn;
            h[hn]=hn;
            urca(hn);
        }
        else {
            scanf ("%d",&x);
            p=h[hn];
            schimba(hn,t[x]);
            hn--;
            urca(t[p]);
            coboara(t[p]);
        }
    }
    return 0;
}