Cod sursa(job #807070)

Utilizator gegeadDragos Gegea gegead Data 4 noiembrie 2012 00:25:56
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include<cstdio>
int a[200001],h[200001],p[200001],n,l,nr;



void percolate(int x)
{
    int aux;
    while(x/2&&a[h[x]]<a[h[x/2]])
    {
        aux=h[x];
        h[x]=h[x/2];
        h[x/2]=aux;
        p[h[x]]=x;
        p[h[x/2]]=x/2;
        x/=2;
    }
}



void inserare(int x)
{
    ++l;
    ++nr;
    a[nr]=x;
    h[l]=nr;
    p[nr]=l;
    percolate(l);
}





void cernere(int x)
{
    int aux,y=0;
    while(x!=y)
    {
        y=x;
        if(y*2<=l&&a[h[x]]>a[h[y*2]])
            x=y*2;
        if(y*2+1<=l&&a[h[x]]>a[h[y*2+1]])
            x=y*2+1;
        aux=h[x];
        h[x]=h[y];
        h[y]=aux;
        p[h[x]]=x;
        p[h[y]]=y;
    }
}




int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    int n,i,k,x;
    scanf("%d",&n);
    for(i=1;i<=n;++i)
    {
        scanf("%d",&k);
        if(k==1)
        {
            scanf("%d",&x);
            inserare(x);
        }
        else
            if(k==2)
            {
                scanf("%d",&x);
                a[x]=-1;
                percolate(p[x]);
                p[h[1]]=0;
                h[1]=h[l--];
                p[h[1]]=1;
                if(l>1)
                    cernere(1);
            }
            else
                printf("%d\n",a[h[1]]);
    }
    return 0;
}