Cod sursa(job #606560)
Utilizator | Data | 4 august 2011 19:30:23 | |
---|---|---|---|
Problema | Heapuri | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.44 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;
x=p[h[k]],p[h[k]]=p[h[k/2]],p[h[k/2]]=x,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,t=h[l];
while(l>1&&v[t]<v[h[l/2]])
{x=h[l],h[l]=h[l/2],h[l/2]=x;
x=p[h[l]],p[h[l]]=p[h[l/2]],p[h[l/2]]=x,l/=2;}
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;
x=p[h[l]],p[h[l]]=p[h[s]],p[h[s]]=x,l=s;}}
while(s);}}}
return 0;}