Cod sursa(job #1484049)

Utilizator iulianrotaruRotaru Gheorghe-Iulian iulianrotaru Data 10 septembrie 2015 13:21:37
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#define Xp 200012
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int NR,L,i,t,n,x,A[Xp],heap[Xp],pos[Xp];
void push(int x)
{
    while(x/2&&A[heap[x]]<A[heap[x/2]])
    {
        swap(heap[x],heap[x/2]);
        swap(pos[heap[x]],pos[heap[x/2]]);
        x/=2;
    }
}
void pop(int x)
{
    int y=0;
    while(x!=y)
    {
        y=x;
        if(y*2<=L&&A[heap[x]]>A[heap[y*2]]) x=y*2;
        if(y*2+1<=L&&A[heap[x]]>A[heap[y*2+1]]) x=y*2+1;
        swap(heap[x],heap[y]);
        pos[heap[x]]=x;
        pos[heap[y]]=y;
    }
}
int main()
{
    f>>n;
    for(i=1;i<=n;++i)
    {
        f>>t;
        if(t==3) g<<A[heap[1]]<<'\n';
        else if(t==2)
        {
            f>>x;
            A[x]=-1;
            push(pos[x]);
            pos[heap[1]]=0;
            heap[1]=heap[L--];
            pos[heap[1]]=1;
            if(L>1) pop(1);
        }
        else
        {
            f>>x;
            ++L,++NR;
            A[NR]=x;
            heap[L]=NR;
            pos[NR]=L;
            push(L);
        }
    }
    return 0;
}