Cod sursa(job #632234)

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

#define INF 0x3f3f3f3f
#define DIM 200005
#define MAX 10005

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

int pozitie=MAX-1;
char buff[MAX];

inline void cit (int &nr)
{
    for (nr=0; buff[pozitie]<'0' || buff[pozitie]>'9'; )
        if (++pozitie==MAX)
        {
            fread (buff,1,MAX,stdin);
            pozitie=0;
        }
    for ( ; '0'<=buff[pozitie] && buff[pozitie]<='9'; )
    {
        nr=nr*10+buff[pozitie]-'0';
        if (++pozitie==MAX)
        {
            fread (buff,1,MAX,stdin);
            pozitie=0;
        }
     }
}

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;

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