Cod sursa(job #251746)

Utilizator maria_pparcalabescu maria daniela maria_p Data 3 februarie 2009 11:45:56
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#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;
}