Pagini recente » Cod sursa (job #2414779) | Cod sursa (job #685747) | Cod sursa (job #2334772) | Cod sursa (job #724540) | Cod sursa (job #251746)
Cod sursa(job #251746)
#include<cstdio>
long heap[200010],b[1000010],a[200010],x,y,nr,n,i;
void swap(long z, long y){
long aux;
aux=heap[z];heap[z]=heap[y];heap[y]=aux;
}
void heapup(long x){
long t;
t=(long)((x-1)/2);
if(t>=0 && heap[t]>heap[x]){
b[heap[t]]=x;
b[heap[x]]=t;
swap(t,x);
heapup(t);
}
}
void heapdw(long x){
long min;
if(2*x+2<nr)
if(heap[2*x+1]<heap[2*x+2])min=2*x+1;
else min=2*x+2;
else
if(2*x+1<nr)min=2*x+1;
else min=x;
if(min==x)return;
swap(x,min);
b[heap[x]]=min;
b[heap[min]]=x;
heapdw(min);
}
int main(){
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%ld",&n);
nr=0;
for(i=0;i<n;i++){
scanf("%ld",&x);
if(x<3)scanf("%ld",&y);
if(x==1){
heap[nr++]=y;
a[nr]=y;
b[y]=nr-1;
heapup(nr-1);
}
else if(x==2){
heap[b[a[y]]]=heap[nr-1];nr--;
heapdw(b[a[y]]);
}
else printf("%ld\n",heap[0]);
}
fclose(stdin);
fclose(stdout);
return 0;
}