Cod sursa(job #1934108)

Utilizator RaduGiucleaGiuclea Radu RaduGiuclea Data 21 martie 2017 10:09:05
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>
#include <algorithm>
using namespace std;
int v[200002],poz[200002],h[200002],n;
void up(int x)
{
    if(x/2&&v[h[x]]<v[h[x/2]])
    {
        swap(poz[h[x]],poz[h[x/2]]);
        swap(h[x],h[x/2]);
        up(x/2);
    }
}
void down(int x)
{
    if(x*2<=n)
    {
        int y=2*x;
        if(y+1<=n&&v[h[y]]>v[h[y+1]])
            y++;
        if(v[h[x]]>v[h[y]])
        {
            swap(poz[h[x]],poz[h[y]]);
            swap(h[x],h[y]);
            down(y);
        }
    }
}
int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    int N,k=0,i,p,ind,val,x;
    scanf("%d",&N);
    for(i=1;i<=N;i++)
    {
        scanf("%d",&p);
        if(p==1)
        {
            scanf("%d",&val);
            v[++k]=val;
            h[++n]=k;
            poz[k]=n;
            up(n);
        }
        if(p==2)
        {
            scanf("%d",&ind);
            x=poz[ind];
            swap(poz[h[x]],poz[h[n]]);
            swap(h[x],h[n]);
            n--;
            up(x);
            down(x);
        }
        if(p==3)
            printf("%d\n",v[h[1]]);

    }
    return 0;
}