Cod sursa(job #1260729)

Utilizator ade_tomiEnache Adelina ade_tomi Data 11 noiembrie 2014 15:30:20
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<stdio.h>
#define inf 1000000004
int  v[200004],poz[200004],q,n,i,nod,nc,aux,h[200004];
int tata(int x)
{
	return x/2;
}
int fiu_drept(int x)
{
	return x*2;
}
int fiu_stang(int x)
{
	return x*2+1;
}
int urc(int x)
{
	while(v[h[x]] < v[h[tata(x)]])
	{

		aux = h[x];
		h[x] = h[tata(x)];
		h[tata(x)] = aux;
		poz[h[tata(x)]] = tata(x);
		poz[h[x]] = x;
		x = tata(x);
	}
}
int cobor(int x)
{
	while((v[h[x]]>v[h[fiu_drept(x)]]&&v[h[fiu_drept(x)]]!=-1)||(v[h[x]]>v[h[fiu_stang(x)]]&&v[h[fiu_drept(x)]]!=-1))
	{
		if(v[h[x]]>v[h[fiu_drept(x)]])
		{
			aux=h[x];
			h[x]=h[fiu_drept(x)];
			h[fiu_drept(x)]=aux;
			poz[h[fiu_drept(x)]] = fiu_drept(x);
			poz[h[x]] = x;
			x=fiu_drept(x);
		}
		else
		{
			aux=h[x];
			h[x]=h[fiu_stang(x)];
			h[fiu_stang(x)]=aux;
			poz[h[fiu_stang(x)]] = fiu_stang(x);
			poz[h[x]] = x;
			x=fiu_stang(x);

		}
	}
	nc=x;
}


int x;
int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	scanf("%d",&n);
	for(i=0;i<=n;i++)
		v[i]=inf;
	v[0]=-1;
	for(i = 1;i <= n;i ++)
	{
		scanf("%d",&q);
		if(q == 1)
		{
			nod ++;
			scanf("%d", &v[nod]);
			h[nod] = nod;
			poz[nod] = nod;
			urc(nod);
		}
		if(q == 3)
			printf("%d\n", v[h[1]]);
		if(q == 2)
		{
			scanf("%d", &x);
			int p = poz[x];
			h[p] = h[nod];
			nc=p;
			urc(p);
			cobor(nc);
		}
	}
	return 0;
}