Cod sursa(job #331930)

Utilizator ciprianfFarcasanu Alexandru Ciprian ciprianf Data 15 iulie 2009 21:17:53
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <stdio.h>
#define N 200100
int n,m,h[N],poz[N],v[N];
void swap(int &x,int &y){
	int z=x;x=y;y=z;
}
void sift(int k){
	int son;
	do{
		son=0;
		if(2*k<=n) son=2*k;
		if(2*k+1<=n && v[h[2*k+1]]<v[h[son]]) son=2*k+1;
		if(son && v[h[son]]<v[h[k]]){
			poz[h[son]]=k;
			poz[h[k]]=son;
			swap(h[k],h[son]);
			k=son;
		}
		else son=0;
	}
	while(son);
}
void percolate(int k){
	while(k>1 && v[h[k/2]]>v[h[k]]){
		poz[h[k]]=k/2;
		poz[h[k/2]]=k;
		swap(h[k],h[k/2]);
		k/=2;
	}
}
void add(){
	h[++n]=v[0];
	poz[v[0]]=n;
	percolate(n);
}
void cut(int x){
	swap(h[n],h[poz[x]]);
	poz[h[poz[x]]]=poz[h[n]];
	n--;
	percolate(poz[x]);
	sift(poz[x]);
}
int main(){
	int i,x,y;
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	scanf("%d",&m);
	for(i=1;i<=m;i++){
		scanf("%d",&x);
		if(x!=3){
			scanf("%d",&y);
			if(x==1) v[++v[0]]=y,add();
			else 
				cut(y);
		}
		else printf("%d\n",v[h[1]]);
	}
	return 0;
}