Cod sursa(job #398325)

Utilizator swift90Ionut Bogdanescu swift90 Data 18 februarie 2010 14:50:28
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include<stdio.h>
#define N 200100
int H[N],nr[N],ord[N],hp;
void swp(int a,int b){
	int ax;
	ax=H[a],H[a]=H[b],H[b]=ax;
	nr[H[a]]=a;
	nr[H[b]]=b;
}
void upheap(int poz){
	int up=poz>>1;
	if(ord[H[up]]>ord[H[poz]] && up>0){
		swp(up,poz);
		upheap(up);
	}
}
void downheap(int poz){
	int jos=poz<<1,pm=poz;
	if(jos<=hp && ord[H[poz]]>ord[H[jos]])
		pm=jos;
	++jos;
	if(jos<=hp && ord[H[pm]]>ord[H[jos]])
		pm=jos;
	if(poz!=pm){
		swp(pm,poz);
		downheap(pm);
	}
}
int main(){
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	int i,n,x,y;
	scanf("%d",&n);
	for(i=1;i<=n;++i){
		scanf("%d",&x);
		if(x==1){
			scanf("%d",&y);
			ord[++ord[0]]=y;
			H[++hp]=ord[0];
			nr[ord[0]]=hp;
			upheap(hp);
		}
		if(x==2){
			scanf("%d",&y);
			y=nr[y];
			nr[H[hp]]=y;
			H[y]=H[hp--];
			y=H[y];
			upheap(nr[y]);
			downheap(nr[y]);
		}
		if(x==3)
			printf("%d\n",ord[H[1]]);
	}
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}