Cod sursa(job #2416509)

Utilizator AlexNeaguAlexandru AlexNeagu Data 27 aprilie 2019 17:18:54
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 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 heap_swap(int a,int b)
{
    swap(Heap[a],Heap[b]);
    T[Heap[a].Position]=a;
    T[Heap[b].Position]=b;
}
void Upheap(int P)
{
    int Father=P/2;
    if(!Father)return;
    if(Heap[P].Value<Heap[Father].Value)
    {
        heap_swap(P,Father);
        Upheap(Father);
    }
}
void Downheap(int P)
{
    int Son=2*P;
    if(Son>L)return;
    if(Heap[Son].Value>Heap[Son+1].Value)
    Son++;
    if(Heap[P].Value>Heap[Son].Value)
    {
        heap_swap(P,Son);
        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;
}