Pagini recente » Cod sursa (job #1909285) | Cod sursa (job #2689807) | Cod sursa (job #908108) | Cod sursa (job #760360) | Cod sursa (job #918996)
Cod sursa(job #918996)
#include<stdio.h>
#define max 200010
#define inf 2000000000
int v[max],heap[max],pos[max],n,nr,l;
void swap(int a,int b)
{
int aux;
aux=heap[a];
heap[a]=heap[b];
heap[b]=aux;
pos[heap[a]]=a;
pos[heap[b]]=b;
}
void UpHill(int node)
{
while(v[heap[node/2]]>v[heap[node]] && node/2>0)
swap(node/2,node) , node/=2;
}
void DownHill(int node)
{
int min,y=0;
while(y!=node)
{
y=node;
if(y*2<=nr && v[heap[node]]>v[heap[y*2]] ) node= y*2;
if(y*2+1<=nr && v[heap[node]]>v[heap[y*2+1]]) node=y*2+1;
swap(node,y);
}
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
int x,y;
for(int i=1;i<=n;i++) v[i]=inf;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
switch(x)
{
case 1:
scanf("%d",&y);
l++,nr++;
v[l]=y;
heap[nr]=l;
pos[l]=nr;
UpHill(nr);
break;
case 2:
scanf("%d",&y);
v[y]=-1;
UpHill(pos[y]);
heap[1]=heap[nr--];
pos[heap[1]]=1;
if(l>1) DownHill(1);
break;
case 3:
printf("%d ",v[heap[1]]);
printf("\n");
break;
}
}
}