Cod sursa(job #1260740)

Utilizator ade_tomiEnache Adelina ade_tomi Data 11 noiembrie 2014 15:40:42
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 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);
	}
}
void pop(int x)
{
    int aux, y = 0;

    while (x != y)
    {
        y = x;

        if (y*2<=nod && v[h[x]]>v[h[y*2]]) x = y*2;
        if (y*2+1<=nod && v[h[x]]>v[h[y*2+1]]) x = y*2+1;

        aux = h[x];
        h[x] = h[y];
        h[y] = aux;

        poz[h[x]] = x;
        poz[h[y]] = y;
    }
}



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];
			pop(p);
		}
	}
	return 0;
}