Cod sursa(job #993456)

Utilizator mlupseLupse-Turpan Mircea mlupse Data 3 septembrie 2013 21:24:51
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#define NMax 100005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int K,H[NMax],A[NMax],Poz[NMax],N,i;
void Swap(int x, int y)
{
    swap(H[x],H[y]);
    swap(Poz[H[x]],Poz[H[y]]);
}
void Percolate(int x)
{
    int TT=x>>1;
    if(TT && A[H[x]]<A[H[TT]])
    {
     Swap(x,TT);
     Percolate(TT);
    }
}
void Sift(int x)
{
    int Son=x<<1;
    if(Son+1<=K && A[H[Son+1]]<A[H[Son]])
        Son++;
    if(Son<=K && A[H[Son]]<A[H[x]])
        {
            Swap(Son,x);
            Sift(Son);
        }
}
void Insert(int x)
{
    H[++K]=x;
    Poz[x]=K;
    Percolate(K);
}
void Erase(int x)
{
    Swap(x,K);
    K--;
    Sift(x);
}
int main()
{
    fin>>N;
    while(N--)
    {
        int op,x;
        fin>>op;
        if(op!=3)
            fin>>x;
        if(op==1)
            {
                A[++i]=x;
                Insert(i);
            }
        if(op==2)
            {
                Erase(Poz[x]);
            }
        if(op==3)
            fout<<A[H[1]]<<"\n";
    }
    return 0;
}