Cod sursa(job #1136224)

Utilizator vladutz15FMI Cornoiu Vlad vladutz15 Data 8 martie 2014 22:42:06
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,v[200001],val,x,p,numar,hp[200001],pos[200001];
void push(int k)
{
    int aux;
    while(k>1 && v[hp[k]]<v[hp[k/2]])
    {
        aux=hp[k];
        hp[k]=hp[k/2];
        hp[k/2]=aux;
        pos[hp[k]]=k;
        pos[hp[k/2]]=k/2;
        k/=2;
    }
}
 
void pop(int k)
{
    int aux,t=0;
    while(k!=t)
    {
        t=k;
        if (t*2<=p && v[hp[k]]>v[hp[t*2]]) k=t*2;
        if (t*2+1<=p && v[hp[k]]>v[hp[t*2+1]]) k=t*2+1;
        aux=hp[k];
        hp[k]=hp[t];
        hp[t]=aux;
        pos[hp[k]]=k;
        pos[hp[t]]=t;
    }
}
 
int main()
{
    int i;
    f>>n;
    for (i=1;i<=n;i++)
    {
        f>>val;
        if (val==1)
        {
            f>>x;
            v[++numar]=x;
            p++;
            hp[p]=numar;
            pos[numar]=p;
            push(p);
        }
        else if (val==2)
        {
            f>>x;
            v[x]=-1;
            push(pos[x]);
            pos[hp[1]]=0;
            hp[1]=hp[p];
			p--;
            pos[hp[1]]=1;
            if (p>1)pop(1);
        }
        else g<<v[hp[1]]<<'\n';
    }
}