Pagini recente » Cod sursa (job #1102496) | Cod sursa (job #3146429) | Cod sursa (job #1977643) | Cod sursa (job #2924837) | Cod sursa (job #372794)
Cod sursa(job #372794)
#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--];
pos[h[pos[x]]]=pos[x];
pop(pos[x]);
}
if(op==3) printf("%d\n",a[h[1]]);
}
return 0;
}