Cod sursa(job #306540)

Utilizator tibiletsKoos Tiberiu Iosif tibilets Data 21 aprilie 2009 11:39:50
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include<fstream.h>
int N,n,P,s[200001],H[200001],p[200001];//n - dimensiunea heap-ului,P - nr maxim de ordine cronoligica a elementelor intrate, p - sirul cu pozitia in heap a elementelor din s in ordine cronologica
inline void swap(int &x,int &y)
{int z=x;x=y;y=z;}
void perc(int k)
{while(k>1&&s[H[k]]<s[H[k/2]])
 {swap(H[k],H[k/2]);
  p[H[k]]=k;
  k/=2;
  p[H[k]]=k;}
}
void sift(int k)
{int f;
 do
 {f=0;
  if(2*k<=n)
   f=2*k;
  if(2*k+1<=n&&s[H[f]]>s[H[2*k+1]])
   f=2*k+1;
  if(s[H[f]]>=s[H[k]])
   f=0;
  if(f)
  {swap(H[k],H[f]);
   p[H[k]]=k;
   p[H[f]]=k=f;}}
 while(f);
}
int main()
{ifstream f("heapuri.in");
ofstream g("heapuri.out");
short cod;int x,k;
f>>N;
for(;N--;)
{f>>cod;
 if(cod<3)
 {f>>x;
  if(cod<2)
  {s[++P]=x;
   H[++n]=P;
   p[P]=n;
   perc(n);}
  else
  {k=p[x];H[k]=H[n--];
   p[H[k]]=k;
   if(k>1&&s[H[k]]<s[H[k/2]])
    perc(k);
   else
    sift(k);
  }
 }
 else g<<s[H[1]]<<'\n';
}
return 0;
}