Pagini recente » Cod sursa (job #1270913) | Cod sursa (job #1352970) | Cod sursa (job #2813781) | Cod sursa (job #2805155) | Cod sursa (job #807070)
Cod sursa(job #807070)
#include<cstdio>
int a[200001],h[200001],p[200001],n,l,nr;
void percolate(int x)
{
int aux;
while(x/2&&a[h[x]]<a[h[x/2]])
{
aux=h[x];
h[x]=h[x/2];
h[x/2]=aux;
p[h[x]]=x;
p[h[x/2]]=x/2;
x/=2;
}
}
void inserare(int x)
{
++l;
++nr;
a[nr]=x;
h[l]=nr;
p[nr]=l;
percolate(l);
}
void cernere(int x)
{
int aux,y=0;
while(x!=y)
{
y=x;
if(y*2<=l&&a[h[x]]>a[h[y*2]])
x=y*2;
if(y*2+1<=l&&a[h[x]]>a[h[y*2+1]])
x=y*2+1;
aux=h[x];
h[x]=h[y];
h[y]=aux;
p[h[x]]=x;
p[h[y]]=y;
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int n,i,k,x;
scanf("%d",&n);
for(i=1;i<=n;++i)
{
scanf("%d",&k);
if(k==1)
{
scanf("%d",&x);
inserare(x);
}
else
if(k==2)
{
scanf("%d",&x);
a[x]=-1;
percolate(p[x]);
p[h[1]]=0;
h[1]=h[l--];
p[h[1]]=1;
if(l>1)
cernere(1);
}
else
printf("%d\n",a[h[1]]);
}
return 0;
}