Cod sursa(job #805632)

Utilizator radu_bucurRadu Bucur radu_bucur Data 31 octombrie 2012 20:15:05
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int h[200001], v[200001], poz[200001],i,n,x,y,nh,k;
void schimba(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 urc(int p)
{
    if (p==1||v[h[p/2]]<=v[h[p]]) return;
    schimba(p/2,p);
    urc(p/2);
}
void cobor(int p)
{
    int fs=p*2,fd=p*2+1,max1=p;
    if(fs<=nh && v[h[fs]]<v[h[max1]]) max1=fs;
    if(fd<=nh && v[h[fd]]<v[h[max1]]) max1=fd;
    if (max1!=p)
    {
        schimba(p,max1);
        cobor(max1);
    }

}
int main()
{

  in>>n; nh=0; k=0;
  for (i=1;i<=n;i++)
  {
     in>>x;
     if (x==1)
     {
        in>>v[++k];
        h[++nh]=k;
        poz[k]=nh;
        urc(nh);
     }
     if(x==2)
     {
        in>>y;
        h[poz[y]]=h[nh];
        nh--;
        cobor(poz[y]);
        urc(poz[y]);
     }
     if(x==3) out<<v[h[1]]<<"\n";
  }
    return 0;
}