Cod sursa(job #1023030)

Utilizator heracleRadu Muntean heracle Data 6 noiembrie 2013 12:30:27
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <cstdio>
const int Q=200500;
int v[Q],poz[Q],h[Q],nh;


void schimba(int p1, int p2)
{
    int aux=h[p1];
    h[p1]=h[p2];
    h[p2]=aux;
    poz[h[p1]]=p[1];
    poz[h[p2]]=p2;
}

void urca(int p)
{
    while(p!=1 && v[h[p]]<v[h[p/2]])
    {
        schimba(p,p/2);
        p/=2;
    }
}

void coboara(int p)
{
    int fs=2*p, fd=2*p+1, bun=p;
    if(fs<=nh && v[h[fs]] < v[h[bun]])
        bun=fs;
    if(fd<=nh && v[h[fd]] < v[h[bun]])
        bun=fd;
    if(bun!=p)
    {
        schimba(p,bun);
        coboara(bun);
    }

}

void adauga(int p)
{
    h[++nh]=p;
    poz[p]=nh;
    urca(nh);
}

void sterge(int p)
{
    schimba(poz[p],nh--);
    urca(poz[p]);
    coboara(poz[p]);
}


int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);

    int n,t,x,nr=0;
    scanf("%d",&n);

    for(int i=1; i<=n; i++)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d",&v[++nr]);
            adauga(nr);
        }
        if(t==2)
        {
            scanf("%d",&x);
            sterge(poz[p]);
        }
        if(t==3)
            printf("%d\n",v[h[1]]);
    }

    return 0;
}