Pagini recente » Cod sursa (job #809067) | Cod sursa (job #1518562) | Cod sursa (job #2432809) | Cod sursa (job #267782) | Cod sursa (job #372788)
Cod sursa(job #372788)
#include <stdio.h>
#define max 200010
int h[max],a[max],pos[max],lg,i,n,nr,op,x;
void swap(int a,int b)
{
int aux=h[a];
h[a]=h[b];
h[b]=aux;
}
void push(int x)
{
while(x/2 && a[h[x]]<a[h[x/2]])
{
swap(x,x/2);
pos[h[x]]=x;
pos[h[x/2]]=x/2;
x/=2;
}
}
void pop(int x)
{
int y=0;
while(x!=y)
{
y=x;
if(y*2<=lg && a[h[x]]>a[h[y*2]]) x=y*2;
if(y*2+1<=lg && a[h[x]]>a[h[y*2+1]]) x=y*2+1;
swap(x,y);
pos[h[x]]=x;
pos[h[y]]=y;
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&x);
lg++; nr++;
a[nr]=x;
h[lg]=nr;
pos[nr]=lg;
push(lg);
}
if(op==2)
{
scanf("%d",&x);
h[pos[x]]=h[lg--];
if(pos[x]>1 && a[h[pos[x]]]>a[h[pos[x]/2]]) push(pos[x]);
else pop(pos[x]);
}
if(op==3) printf("%d\n",a[h[1]]);
}
return 0;
}