Cod sursa(job #260871)

Utilizator MarquiseMarquise Marquise Data 17 februarie 2009 17:32:07
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <stdio.h>

#define NMAX 200005

int N, NH, NV, v[NMAX], h[NMAX], poz[NMAX];


void swap(int i, int j)
{
	int aux;

	aux = h[i];
	h[i] = h[j];
	h[j] = aux;
}


void HeapUp(int k)
{
	int t;

	t = k >> 1;
	if ( t <= 1) return;

	if ( v[h[t]] > v[h[k]] )
	{
		poz[h[t]] = k;
		poz[h[k]] = t;

		swap(t, k);
		HeapUp(t);
	}
}


void HeapDw(int t)
{
	int i;

	i = t << 1;

	if ( i <= NH)
	{
		if ( ( i | 1) <= NH && v[h[i]] > v[h[i|1]] )
			i++;

		if ( v[h[t]] > v[h[i]])
		{
			poz[h[t]] = i;
			poz[h[i]] = t;

			swap(i, t);
			HeapDw(i);
		}
	}
}


int main()
{
	int i, c, x;

	freopen("heapuri.in", "r", stdin);
	freopen("heapuri.out", "w", stdout);

	scanf("%d", &N);

	for ( i = 1; i <= N; i++)
	{
		scanf("%d", &c);

		if ( c == 1)
		{
			scanf("%d", &x);
			NV++;
			v[NV] = x;
			NH++;
			h[NH] = NH;
			poz[NV] = NH;

			HeapUp(NH);
		}

		if ( c == 2)
		{
			scanf("%d", &x);

			v[x] = -1;
			HeapUp(x);

			poz[x] = 0;
			h[1] = h[NH--];
			poz[h[1]] = 1;

			HeapDw(1);


		}

		if ( c == 3)
			printf("%d\n", v[h[1]]);

	}

	return 0;
}