Cod sursa(job #855263)

Utilizator mihai10stoicaFMI - Stoica Mihai mihai10stoica Data 14 ianuarie 2013 20:22:35
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include<cstdio>
int n,l,nr,a[200022],heap[200022],pos[200022];
void insert(int x)
{
    int aux;
    while(x/2 && a[heap[x]]<a[heap[x/2]])
    {
        aux=heap[x];
        heap[x]=heap[x/2];
        heap[x/2]=aux;
        pos[heap[x]]=x;
        pos[heap[x/2]]=x/2;
        x/=2;
    }
}
void remove(int x)
{
    int aux,y=0;
    while(x!=y)
    {
        y=x;
        if(y*2<=l && a[heap[x]]>a[heap[y*2]]) x=y*2;
        if(y*2+1<=l && a[heap[x]]>a[heap[y*2+1]]) x=y*2+1;
        aux=heap[x];
        heap[x]=heap[y];
        heap[y]=aux;
        pos[heap[x]]=x;
        pos[heap[y]]=y;
    }
}
int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    int i,x,cod;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&cod);
        if(cod<3)
        {
            scanf("%d",&x);
            if(cod==1) 
            {
                l++;
                nr++;
                a[nr]=x;
                heap[l]=nr;
                pos[nr]=l;
                insert(x);
            }
            if(cod==2)
            {
                a[x]=-1;
                insert(pos[x]);
                pos[heap[1]]=0;
                heap[1]=heap[l--];
                pos[heap[1]]=1;
                if(l>1) remove(1);
            }
        }
        else if(cod==3) printf("%d",a[heap[1]]);
    }
    return 0;
}