Cod sursa(job #432651)

Utilizator DraStiKDragos Oprica DraStiK Data 2 aprilie 2010 16:27:07
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <algorithm>
using namespace std;

#define INF 0x3f3f3f3f
#define DIM 100005

int ins[DIM],h[DIM],poz[DIM];
int n,m,l;

inline void upheap (int nod)
{
    int tata;

    for ( ; nod>1; )
    {
        tata=nod>>1;
        if (ins[h[tata]]>ins[h[nod]])
        {
            poz[h[tata]]=nod;
            poz[h[nod]]=tata;
            swap (h[tata],h[nod]);
            nod=tata;
        }
        else
            return ;
    }
}

inline void downheap (int nod)
{
    int fiu;

    for ( ; nod<=l; )
    {
        if ((nod<<1)<=l)
        {
            fiu=nod<<1;
            if (fiu+1<=l)
                if (ins[h[fiu+1]]<ins[h[fiu]])
                    ++fiu;
        }
        else
            return ;
        if (ins[h[fiu]]<ins[h[nod]])
        {
            poz[h[fiu]]=nod;
            poz[h[nod]]=fiu;
            swap (h[fiu],h[nod]);
            nod=fiu;
        }
        else
            return ;
    }
}

int main ()
{
    freopen ("heapuri.in","r",stdin);
    freopen ("heapuri.out","w",stdout);
    int i,tip,x;

    scanf ("%d",&n);
    for (i=1; i<=n; ++i)
    {
        scanf ("%d",&tip);
        if (tip==1)
        {
            scanf ("%d",&ins[++m]);
            poz[h[++l]=m]=l;
            upheap (l);
        }
        else if (tip==2)
        {
            scanf ("%d",&x);
            ins[x]=-INF;
            upheap (poz[x]);
            h[1]=h[l--];
            poz[h[1]]=1;
            downheap (1);
        }
        else
            printf ("%d\n",ins[h[1]]);
    }

    return 0;
}