Cod sursa(job #1042545)

Utilizator cristigrigoreGrigore Cristan Andrei cristigrigore Data 27 noiembrie 2013 11:16:46
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <cstdio>

using namespace std;
int n,i,x,y,j,k1,k,a[200002],poz[200002],h[200002];
void swap(int x,int y)
{
    int aux;
    aux=h[x];
    h[x]=h[y];
    h[y]=aux;
    poz[h[x]]=x;
    poz[h[y]]=y;
}
void HeapDw(int x)
{
    int St=x*2,Dr=x*2+1,p;
    if (St<=k1)
        {
            p=St;
            if (Dr<=k1 && a[h[Dr]]<a[h[St]]) p=Dr;
            if (a[h[p]]<a[h[x]])
                {
                    swap(x,p);
                    HeapDw(p);
                }
        }
}
void HeapUp(int i)
{
    int r;
    if(i<=1) return;
    r=i/2;
    if(a[h[i]]<a[h[r]])
    { swap(i,r);
    HeapUp(r);}
}
int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        scanf("%d",&x);
        if(x==1)
        {
            scanf("%d",&y);
            a[++k]=y;
            h[++k1]=k;
            poz[k]=k1;
            HeapUp(k1);
        }
        if(x==2)
        {
            scanf("%d",&y);
            int q=poz[y];
            swap(poz[y],k1);
            k1--;
            HeapDw(q);
        }
        if(x==3)
        {
            printf("%d\n",a[h[1]]);
        }
    }
    return 0;
}