Cod sursa(job #372794)

Utilizator cristikIvan Cristian cristik Data 11 decembrie 2009 18:20:18
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <stdio.h>
#define max 200010
int h[max],a[max],pos[max],lg,i,n,nr,op,x;
void swap(int a,int b)
{
    int aux=h[a];
    h[a]=h[b];
    h[b]=aux;
}
void push(int x)
{
    while(x/2 && a[h[x]]<a[h[x/2]])
    {
        swap(x,x/2);
        pos[h[x]]=x;
        pos[h[x/2]]=x/2;
        x/=2;
    }
}
void pop(int x)
{
    int y=0;
    while(x!=y)
    {
        y=x;
        if(y*2<=lg && a[h[x]]>a[h[y*2]]) x=y*2;
        if(y*2+1<=lg && a[h[x]]>a[h[y*2+1]]) x=y*2+1;
        swap(x,y);
        pos[h[x]]=x;
        pos[h[y]]=y;
    }
}
int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d",&x);
            lg++; nr++;
            a[nr]=x;
            h[lg]=nr;
            pos[nr]=lg;
            push(lg);
        }
        if(op==2)
        {
            scanf("%d",&x);
            h[pos[x]]=h[lg--];
            pos[h[pos[x]]]=pos[x];
            pop(pos[x]);
        }
        if(op==3) printf("%d\n",a[h[1]]);
    }
    return 0;
}