Cod sursa(job #606555)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 4 august 2011 19:21:52
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.61 kb
#include<fstream.h>
#define N 200001
long h[N],n,k,i,r,j,p[N],v[N],z,x,t,s,l;
int main()
{ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>n;
while(n--)
      {f>>z;
      if(z==3)
              g<<v[h[1]]<<"\n";
      else
              {f>>r;
              if(z==1)
                      {v[++j]=r,h[++k]=j,p[j]=k,l=h[k];
                      while(k>1&&v[l]<v[h[k/2]])
                             {x=h[k],h[k]=h[k/2],h[k/2]=x;
                             p[h[k]]=k,p[h[k/2]]=k/2,k/=2;}}
              else
                      {l=p[r],t=h[l],h[l]=h[k],h[k]=t;
                      x=p[h[k]],p[h[k--]]=p[h[l]],p[h[l]]=x;
                      if(l>1&&v[h[l]]<v[h[l/2]])
                             {t=h[l];
                             while(l>1&&v[t]<v[h[l/2]])
                                    {x=h[l],h[l]=h[l/2],h[l/2]=x;
                                    p[h[l]]=l,p[h[l/2]]=l/2;}}
                      else
                             {do
                                    {s=0;
                                    if(2*l<=k)
                                             {s=2*l;
                                             if(2*l<k&&v[h[2*l+1]]<v[h[2*l]])
                                                    s=2*l+1;
                                             if(v[h[s]]>=v[h[l]])
                                                    s=0;}
                                    if(s)
                                             {x=h[l],h[l]=h[s],h[s]=x;
                                             p[h[l]]=l,p[h[s]]=l=s;}}
                             while(s);}}}}
return 0;}