Cod sursa(job #604363)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 21 iulie 2011 22:46:51
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>

#define NMax 200005

using namespace std;

int Value[NMax], N, NHeap, Heap[NMax], Poz[NMax];

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

void Percolate (int X)
{
    int Father=(X>>1);
    if (Father>0 and Value[Heap[X]]<Value[Heap[Father]])
    {
        Swap (X, Father);
        Percolate (Father);
    }
}

void Sift (int X)
{
    int Son=(X<<1);
    if (Son+1<=NHeap and Value[Heap[Son+1]]<Value[Heap[Son]])
    {
        ++Son;
    }
    if (Son<=NHeap and Value[Heap[Son]]<Value[Heap[X]])
    {
        Swap (X, Son);
        Sift (Son);
    }
}

void Insert (int X)
{
    Heap[++NHeap]=X;
    Poz[X]=NHeap;
    Percolate (X);
}

void Delete (int X)
{
    Swap (X, NHeap);
    Poz[Heap[NHeap]]=0;
    Heap[NHeap]=0;
    --NHeap;
    Sift (X);
}

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