Cod sursa(job #1042537)

Utilizator cristigrigoreGrigore Cristan Andrei cristigrigore Data 27 noiembrie 2013 10:37:27
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>

using namespace std;
int n,i,x,y,j,k,a[200002],poz[200002],h[200002];
void swap(int y, int k)
{
    int aux;
    aux=a[poz[y]];
    a[poz[y]]=a[k];
    a[k]=aux;
    aux=h[k];
    h[k]=h[y];
    h[y]=aux;
    aux=poz[h[k]];
    poz[h[k]]=y;
    poz[h[y]]=aux;

}
void swap1(int i, int j)
{
    int aux;
    aux=a[i];
    a[i]=a[j];
    a[j]=aux;
    aux=h[i];
    h[i]=h[j];
    h[j]=aux;
    aux=poz[h[i]];
    poz[h[i]]=poz[h[j]];
    poz[h[j]]=aux;
}
void HeapUp(int i)
{
    int r;
    if(i<=1) return;
    r=i/2;
    if(a[i]<a[r]){ swap1(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[k]=k;
            poz[k]=k;
        }
        if(x==2)
        {
            scanf("%d",&y);
            swap(y,k);
            k--;
        }
        if(x==3)
        {
            for(j=2; j<=k; j++)
            HeapUp(j);
            printf("%d\n",a[1]);
        }
    }
    return 0;
}