Cod sursa(job #1333867)

Utilizator rsteliRadu Stelian rsteli Data 3 februarie 2015 17:43:43
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");

#define nmax 200010

int n,l,nr,tip,x,v[nmax],h[nmax*4],p[nmax];

void push(int x)
{
    int aux;
    while (x/2 && v[h[x]]<v[h[x/2]])
    {
        aux=h[x];
        h[x]=h[x/2];
        h[x/2]=aux;
        p[h[x]]=x;
        p[h[x/2]]=x/2;
        x/=2;
    }
}

void pop(int x)
{
    int aux,y=0;
    while (x!=y)
    {
        y=x;
        if (y*2<=l && v[h[x]]>v[h[y*2]]) x=y*2;
        if (y*2+1<=l && v[h[x]]>v[h[y*2+1]]) x=y*2+1;
        aux=h[x];
        h[x]=h[y];
        h[y]=aux;
        p[h[x]]=x;
        p[h[y]]=y;
    }
}


int main()
{
    int i,j;
    cin>>n;
    for (i=1;i<=n;i++)
    {
        cin>>tip;
        if (tip==1)
        {
            cin>>x;
            l++;
            nr++;
            v[nr]=x;
            h[l]=nr;
            p[nr]=l;
            push(l);
        }
        else if (tip==2)
        {
            cin>>x;
            v[x]=-1;
            push(p[x]);
            p[h[1]]=0;
            h[1]=h[l];
            l--;
            p[h[1]]=1;
            if (l>1) pop(1);
        }
        else
            cout<<v[h[1]]<<'\n';
    }
}