Cod sursa(job #2235031)

Utilizator danielsociuSociu Daniel danielsociu Data 26 august 2018 13:09:50
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
std::ifstream cin("heapuri.in");
std::ofstream cout("heapuri.out");
#define maxn 500002
int L=0, NR=0, N;
int heap[maxn],pos[maxn],v[maxn];

void push(int x){
    int aux;

    while(x/2 && v[heap[x/2]]>v[heap[x]])
    {
        aux=heap[x];
        heap[x]=heap[x/2];
        heap[x/2]=aux;

        pos[heap[x]]=x;
        pos[heap[x/2]]=x/2;
        x/=2;
    }
}

void pop(int x){
    int y=0,aux;

    while(y!=x){
        y=x;
        if(2*y<=L&&v[heap[x]]>v[heap[2*y]])
            x=2*y;
        if(2*y+1<=L&&v[heap[x]]>v[heap[2*y+1]])
            x=2*y+1;
        aux=heap[y];
        heap[y]=heap[x];
        heap[x]=aux;

        pos[heap[x]]=x;
        pos[heap[y]]=y;
    }
}


int main()
{
    int x,cod;
    cin>>N;
    for(;N--;){
        cin>>cod;
        if(cod<3){
            cin>>x;
        }
        if(cod==1){
            L++;NR++;
            v[NR]=x;
            heap[L]=NR;
            pos[NR]=L;
            push(L);
        }
        if(cod==2){
            v[x]=-1;
            push(pos[x]);

            pos[heap[1]]=0;
            heap[1]=heap[L--];
            pos[heap[1]]=1;
            if(L>1)
                pop(1);
        }
        if(cod==3)
            cout<<v[heap[1]]<<'\n';
    }
    return 0;
}