Cod sursa(job #281056)

Utilizator igsifvevc avb igsi Data 13 martie 2009 19:11:43
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include<fstream.h>

#define xx 200001

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int h[xx],n,pos[xx],a[xx],nr;

inline void schimba(int i,int j)
{
       int aux;
       aux=h[i]; h[i]=h[j]; h[j]=aux;
       pos[h[i]]=i;
       pos[h[j]]=j;
}
       
void jos(int i)
{
     int j=0;
     
     while(i!=j)
     {
       j=i;
       
       if(2*j<=n && a[h[i]]>a[h[2*j]]) i=2*j;
       
       if(2*j+1<=n && a[h[i]]>a[h[2*j+1]]) i=2*j+1;
       
       schimba(i,j);
     }
}

void sus(int i)
{
     while(i/2 && a[h[i/2]]>a[h[i]])
     {
          schimba(i,i/2);
          i/=2;
     }
}

void sterge(int i)
{
     int k=pos[i];
     
     schimba(k,n);
     pos[h[n]]=0;
     h[n]=0;
     n--;
     jos(k);
}

int main()
{
    int op,i,x,t=0;
    fin>>nr;
    
    for(i=1;i<=nr;i++)
    {
         fin>>op;
         if(op==1)
         {
             fin>>x;
             a[++t]=x;
             h[++n]=t;
             pos[t]=n;
             sus(n);
         }
         else
            if(op==2)
            {
                fin>>x;
                sterge(x);
            }
            else
              fout<<a[h[1]]<<'\n';
    }
    
    fout.close();
    return 0;
}