Cod sursa(job #603153)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 14 iulie 2011 20:25:26
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <iostream>

#define NMax 200005

using namespace std;

int V[NMax], Poz[NMax], H[3*NMax], NH;

inline void Swap (int X, int Y)
{
    int Aux;
    Aux=Poz[H[X]];
    Poz[H[X]]=Poz[H[Y]];
    Poz[H[Y]]=Aux;
    Aux=H[X];
    H[X]=H[Y];
    H[Y]=Aux;
}

void Percolate (int X)
{
    int F=X/2;
    while (F!=0)
    {
        if (V[H[X]]<V[H[F]])
        {
            Swap (X, F);
            X=F;
            F=X/2;
        }
        else
        {
            return;
        }
    }
}

void Sift (int X)
{
    int S=2*X;
    while (S<=NH)
    {
        if (S+1<=NH && V[H[S]]>V[H[S+1]])
        {
            ++S;
        }
        if (V[H[X]]>V[H[S]])
        {
            Swap (X, S);
            X=S;
            S=2*X;
        }
        else
        {
            return;
        }
    }
}

void Insert (int X)
{
    H[++NH]=X;
    Poz[X]=NH;
    Percolate (Poz[X]);
}

void Delete (int X)
{
    Swap (X, NH);
    --NH;
    Sift (X);
}

int main()
{
    freopen ("heapuri.in", "r", stdin);
    freopen ("heapuri.out", "w", stdout);
    int N, i=0;
    scanf ("%d", &N);
    for (; N>0; --N)
    {
        int Tip;
        scanf ("%d", &Tip);
        if (Tip==1)
        {
            scanf ("%d", &V[++i]);
            Insert (i);
        }
        if (Tip==2)
        {
            int X;
            scanf ("%d", &X);
            Delete (Poz[X]);
        }
        if (Tip==3)
        {
            printf ("%d\n", V[H[1]]);
        }
    }
    return 0;
}