Cod sursa(job #2416385)

Utilizator AlexNeaguAlexandru AlexNeagu Data 27 aprilie 2019 14:45:13
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define NMAX 200005
int T[NMAX],N,x,y,L=0,K=0;
struct Tabel
{
    int Value;
    int Position;
} Heap[NMAX*5],aux;
void Upheap(int X)
{
    int Father=X/2;
    if (!Father)
        return;
    if (Heap[Father].Value<Heap[X].Value)
        return;

    T[Heap[Father].Position]=X;
    T[Heap[X].Position]=Father;

    aux=Heap[Father];
    Heap[Father]=Heap[X];
    Heap[X]=aux;

    Upheap(Father);
}
void Downheap(int X)
{
    int Left_Son=X*2;
    int Right_Son=X*2+1;
    if (Left_Son>L)
        return;
    int Son=Left_Son;
    if (Right_Son<=L && Heap[Right_Son].Value<=Heap[Son].Value)
        Son=Right_Son;
    if (Heap[Son].Value>Heap[X].Value)
        return;

    T[Heap[Son].Position]=X;
    T[Heap[X].Position]=Son;

    aux=Heap[Son];
    Heap[Son]=Heap[X];
    Heap[X]=aux;

    Downheap(Son);
}
void Insert(int X)
{
    Heap[L].Value=X;
    T[K]=L;
    Heap[L].Position=K;
    Upheap(L);
}
void Pop(int X)
{
    X=T[X];
    Heap[X]=Heap[L];
    L--;
    Downheap(X);
    Upheap(X);
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin>>N;
    while (N--)
    {
        fin>>x;
        if (x<3)
        {
            fin>>y;
            if (x==1)
            {
                K++;
                L++;
                Insert(y);
            }
            else
            Pop(y);
        }
        else fout<<Heap[1].Value<<"\n";
    }
    return 0;
}