Cod sursa(job #663814)

Utilizator BarracudaFMI-Alex Dobrin Barracuda Data 18 ianuarie 2012 23:05:14
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <cstdio>
#define lim 200005
using namespace std;
int V[lim], Poz[lim], H[3*lim], 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;
}