Pagini recente » Cod sursa (job #1433663) | Cod sursa (job #2285103) | Cod sursa (job #2968996) | Cod sursa (job #1434134) | Cod sursa (job #855257)
Cod sursa(job #855257)
#include<cstdio>
int n,l,nr,a[200002],heap[200002],pos[200002];
void insert(int x)
{
int aux;
while(x/2 && a[heap[x]]<a[heap[x/2]])
{
aux=heap[x];
heap[x]=heap[x/2];
heap[x/2]=aux;
pos[heap[x]]=x;
pos[heap[x/2]]=x/2;
x=x/2;
}
}
void remove(int x)
{
int aux,y=0;
while(x!=y)
{
y=x;
if(y*2<=l && a[heap[x]]>a[heap[y*2]]) x=y*2;
if(y*2+1<=l && a[heap[x]]>a[heap[y*2+1]]) x=y*2+1;
aux=heap[x];
heap[x]=heap[y];
heap[y]=aux;
pos[heap[x]]=x;
pos[heap[y]]=y;
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int i,x,cod;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&cod);
if(cod<3)
{
scanf("%d",&x);
if(cod==1)
{
l++;
nr++;
heap[l]=nr;
pos[nr]=l;
insert(x);
}
if(cod==2)
{
a[x]=-1;
insert(pos[x]);
pos[heap[1]]=0;
heap[1]=heap[l--];
pos[heap[1]]=1;
if(l>1) remove(1);
}
}
else if(cod==3) printf("%d",a[heap[1]]);
}
return 0;
}