Cod sursa(job #495602)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 25 octombrie 2010 22:30:17
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <stdio.h>
#include <stdlib.h>

int n,i,l,x,nr,c;
int a[200001],h[200001],pos[200001];

void swap(int k, int t){
	int x;
	x=a[t]; 
	a[t]=a[k];
	a[k]=x;
}

void HeapUp(int k) {
	int t;
	if (k<=1) return;
	t=k/2;
	if (h[a[k]]<h[a[t]]) {
		swap(k,t);
		pos[a[k]]=k;
		pos[a[t]]=t;
		HeapUp(t);
	}
}

void sterge(int k) {
	int aux,r;
	r=0;
	while (k!=r) {
		r=k;
		if (r*2<=l && h[a[k]]>h[a[r*2]]) k=r*2;
		if (r*2+1<=l && h[a[k]]>h[a[r*2+1]]) k=r*2+1;
		
	    aux=a[k]; a[k]=a[r]; a[r]=aux;
		pos[a[k]]=k;
		pos[a[r]]=r;
}
}
	
int main () {
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	scanf("%d",&n);
	l=0;
	nr=0;
	for (i=1; i<=n; i++) {
		scanf("%d",&c);
		if (c==1 || c==2) scanf("%d",&x);
		if (c==1) {
			l++;
			nr++;
			h[nr]=x;
			a[l]=nr;
			pos[nr]=l;
			HeapUp(l);
		}
		else
			if (c==2) {
				h[x]=-1;
				HeapUp(pos[x]);
				pos[a[1]]=0;
				a[1]=a[l--];
				pos[a[1]]=l;
				if (l>1) sterge(1);
			}
			else printf("%d\n",h[a[1]]);
	}
	return 0;
}