Cod sursa(job #995098)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 7 septembrie 2013 14:50:13
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N;
int Heap[200002],Value[200002];
int NHeap,Poz[200002];
bool Use[200002];
void Swap(int X, int Y)
{
    swap(Heap[X],Heap[Y]);
    swap(Poz[Heap[X]],Poz[Heap[Y]]);
}
void Percolate (int X)
{
    int Father=(X>>1);
    if (Father>0 && Value[Heap[X]]<Value[Heap[Father]])
    {
        Swap (X,Father);
        Percolate (Father);
    }
}
void Sift(int X)
{
    int Son=(X<<1);
    if(Son+1<=NHeap && Value[Heap[Son+1]]<Value[Heap[Son]])
      Son++;
    if(Son<=NHeap && Value[Heap[Son]]<Value[Heap[X]])
        {
            Swap(Son,X);
            Sift(Son);
        }
}
void Erase(int K)
{
    int Father=(K>>1);
    Heap[K] = Heap[NHeap];
    --NHeap;
    if ((K > 1) && (Value[Heap[K]] > Value[Heap[Father]]))
        Percolate(K);
    else
        Sift(K);
}
void Solve_Heap()
{
    int i,number=0,introduce=1;
    for(i=1;i<=N;i++)
    {
        int type,value;
        f>>type;
        if(type==1)
        {
            f>>value;
            Heap[++NHeap]=introduce;
            Poz[introduce++]=NHeap;
            Value[NHeap]=value;
            Percolate(NHeap);
        }
        if(type==2)
        {
            f>>value;
            Erase(Poz[value]);
        }
        if(type==3)
            g<<Value[Heap[1]]<<"\n";
    }
}
int main()
{
    f>>N;
    Solve_Heap();
    return 0;
}