Cod sursa(job #918992)

Utilizator taigi100Cazacu Robert taigi100 Data 19 martie 2013 12:10:09
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 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,aux=0;
	while(aux<=nr)
	{
		if(node*2+1<=nr)
		{
		min= v[heap[node*2+1]]<v[heap[node*2]] ? v[heap[node*2+1]]:v[heap[node*2]];
		if(min==v[heap[node*2]]) aux=node*2;
		else aux=node*2+1;
		}
		else if(node*2<=nr)
			aux=node*2;
		else
			aux=nr+10;
		if(v[heap[aux]]<v[heap[node]] && aux<=nr)
		{
		swap(aux,node);
		node=aux;
		}
	}
}
int main()
{
	freopen("dijkstra.in","r",stdin);
	freopen("dijkstra.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;
		}
	}
}