Cod sursa(job #1464990)

Utilizator TeodorescuStefanEduardTeodorescu Stefan Eduard TeodorescuStefanEduard Data 26 iulie 2015 11:38:52
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include<cstdio>
using namespace std;
int v[200010],poz[200010],heap[200010],l=0;
void push(int p){
    int aux;
    while(p>1&&v[heap[p]]<v[heap[p/2]]){
        aux=heap[p];
        heap[p]=heap[p/2];
        heap[p/2]=aux;
        poz[heap[p]]=p;
        poz[heap[p/2]]=p/2;
        p/=2;
    }
}
void pop(int p){
    int aux,temp,pp1,pp2;
    while(p){
        pp1=0;
        pp2=0;
        aux=p;
        if(p*2<=l)
            if(v[heap[aux]]>v[heap[2*p]]){
                aux=2*p;
                pp1=1;
            }
        if(p*2+1<=l)
            if(v[heap[aux]]>v[heap[2*p+1]]){
                aux=2*p+1;
                pp2=1;
            }
        if(pp1==0&&pp2==0)
            break;
        temp=heap[p];
        heap[p]=heap[aux];
        heap[aux]=temp;
        poz[heap[p]]=p;
        poz[heap[aux]]=aux;
        p=aux;
    }
}
int main(){
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    int n,i,x,tip,nr=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&tip);
        if(tip==1||tip==2)
            scanf("%d",&x);
        if(tip==1){
            nr++;
            l++;
            v[nr]=x;
            poz[nr]=l;
            heap[l]=nr;
            push(l);
        }
        if(tip==2){
            v[x]=-1;
            push(poz[x]);
            heap[1]=heap[l];
            heap[l]=0;
            l--;
            poz[heap[1]]=1;
            pop(1);
        }
        if(tip==3)
            printf("%d\n",v[heap[1]]);
    }
    return 0;
}