Pagini recente » Cod sursa (job #2050840) | Cod sursa (job #2912384)
#include<fstream>
using namespace std;
ifstream F("heapuri.in");
ofstream G("heapuri.out");
#define N 200001
int x,y,z,t,n,r,j,p[N],v[N],h[N];
void A(int t)
{
for(;t>1&&v[h[t]]<v[h[t>>1]];h[t]^=h[t>>1]^=h[t]^=h[t>>1],p[h[t]]=t,p[h[t>>1]]=t>>1,t>>=1);
}
int main()
{
F>>n;
while(n--) {
F>>z;
if(z==3)
G<<v[h[1]]<<"\n";
else {
F>>r;
if(z==1)
v[++t]=r,h[++j]=t,p[t]=j,A(j);
else
for(v[r]=-1,A(p[r]),p[h[1]]=y=0,h[1]=h[j--],p[h[1]]=x=1;x!=y;z=h[x],h[x]=h[y],h[y]=z,p[h[x]]=x,p[h[y]]=y) {
y=x;
if((y<<1)<=j&&v[h[x]]>v[h[y<<1]])
x=y<<1;
if((y<<1)<j&&v[h[x]]>v[h[1+(y<<1)]])
x=(y<<1)+1;
}
}
}
return 0;
}