Cod sursa(job #632233)

Utilizator DraStiKDragos Oprica DraStiK Data 10 noiembrie 2011 17:14:21
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <algorithm>
#include <cstdio>
using namespace std;

#define INF 0x3f3f3f3f
#define DIM 200005

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

inline void upheap (int nod)
{
    int tata;

    for ( ; nod>1; )
    {
        tata=nod>>1;
        if (v[h[tata]]>v[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 && v[h[fiu+1]]<v[h[fiu]])
                ++fiu;
        }
        else
            return ;
        if (v[h[nod]]>v[h[fiu]])
        {
            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",&m);
    for (i=1; i<=m; ++i)
    {
        scanf ("%d",&tip);
        if (tip==3)
            printf ("%d\n",v[h[1]]);
        else
        {
            scanf ("%d",&x);
            if (tip==1)
            {
                v[++n]=x;
                poz[h[++l]=n]=l;
                upheap (l);
            }
            else
            {
                v[x]=INF;
                downheap (poz[x]);
            }
        }
    }
}