Pagini recente » Cod sursa (job #514420) | Cod sursa (job #885975) | Cod sursa (job #2700755) | Cod sursa (job #1018156) | Cod sursa (job #606558)
Cod sursa(job #606558)
#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;
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,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;
p[h[l]]=l,p[h[s]]=l=s;}}
while(s);}}}
return 0;}