Cod sursa(job #339412)

Utilizator razvi9Jurca Razvan razvi9 Data 9 august 2009 18:21:50
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include<cstdio>

int v[200001],h[200001],size,m,n,p[2000001],x,y;
int *poz = p;

void heapup(int p)
{
	if(p<=0) return;
	int t = (p-1)>>1;
	if(v[h[t]] > v[h[p]])
	{
		x = h[t];
		h[t] = h[p];
		h[p] = x;

		x = poz[h[t]];
		poz[h[t]] = poz[h[p]];
		poz[h[p]] = x;
		heapup(t);
	}
}

void heapdown(int p)
{
	if(p>=size) return;
	int st = (p<<1) + 1;
	int dr = st+1;
	int min;
	if(st<size)
	{
		min = st;
		if(dr<size && v[h[dr]]<v[h[st]])
			min = dr;
		if(v[h[min]]<v[h[p]])
		{
			x = h[min];
			h[min] = h[p];
			h[p] = x;

			x = poz[h[min]];
			poz[h[min]] = poz[h[p]];
			poz[h[p]] = x;
			heapdown(min);
		}
	}
}

int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	scanf("%d",&m);++m;
	while(--m)
	{
		scanf("%d",&x);
		if(x!=3)
			scanf("%d",&y);
		switch(x)
		{
		case 1:
			v[++n]=y;
			h[size++] = n;
			p[n] = size - 1;
			heapup(size-1);
			break;
		case 2:
			--size;
			if(p[y]<size)
			{
				h[p[y]] = h[size];
				heapdown(p[y]);
			}
			break;
		case 3:
			printf("%d\n",v[h[0]]);
			break;
		}
	}
	fclose(stdout);
	return 0;
}