Cod sursa(job #918996)

Utilizator taigi100Cazacu Robert taigi100 Data 19 martie 2013 12:13:20
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include<stdio.h>

#define max 200010
#define inf 2000000000
int v[max],heap[max],pos[max],n,nr,l;

void swap(int a,int b)
{
	int aux;

	aux=heap[a];
	heap[a]=heap[b];
	heap[b]=aux;

	pos[heap[a]]=a;
	pos[heap[b]]=b;
}

void UpHill(int node)
{
	while(v[heap[node/2]]>v[heap[node]] && node/2>0)
		swap(node/2,node) , node/=2;
}
void DownHill(int node)
{
	int min,y=0;
	while(y!=node)
	{
		y=node;
		if(y*2<=nr && v[heap[node]]>v[heap[y*2]] ) node= y*2;
		if(y*2+1<=nr && v[heap[node]]>v[heap[y*2+1]]) node=y*2+1;
		swap(node,y);
	}
}
int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);

	scanf("%d",&n);

	int x,y;
	for(int i=1;i<=n;i++) v[i]=inf;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&x);

		switch(x)
		{
		case 1:
			scanf("%d",&y);
			l++,nr++;
			v[l]=y;
			heap[nr]=l;
			pos[l]=nr;
			UpHill(nr);
			break;
		case 2:
			scanf("%d",&y);
			v[y]=-1;
			UpHill(pos[y]);

			heap[1]=heap[nr--];
			pos[heap[1]]=1;
			if(l>1) DownHill(1);
			break;
		case 3:
			printf("%d ",v[heap[1]]);
			printf("\n");
			break;
		}
	}
}