Cod sursa(job #1043452)

Utilizator rebound212Mihnea Savu rebound212 Data 28 noiembrie 2013 17:01:52
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <cstdio>

using namespace std;
int a[200002],q,poz[200002],h[200002],nr,i,n,x,numar,qq;
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 heapup(int i)
{
    int x;
    if(i<=1) return;
    x=i/2;
    if(a[h[i]]<a[h[x]])
    { swap(i,x);
    heapup(x);}
}
void heapdown(int x)
{
    int st=x*2,dr=x*2+1;
    if (st<=nr)
        {
             int p=st;
            if (dr<=nr && a[h[dr]]<a[h[st]]) p=dr;
            if (a[h[p]]<a[h[x]])
                {
                    swap(x,p);
                    heapdown(p);
                }
        }
}

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",&numar);
                   a[++q]=numar;
                   h[++nr]=q;
                   poz[q]=nr;
                   heapup(nr);
               }
          if(x==2)
          {
            scanf("%d",&numar);
             qq=poz[numar];
            swap(poz[numar],nr);
            nr--;
            heapdown(qq);
          }
          if(x==3)
        {
            printf("%d\n",a[h[1]]);
        }
       }
    return 0;
}