Cod sursa(job #266517)

Utilizator peanutzAndrei Homorodean peanutz Data 25 februarie 2009 18:46:44
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <stdio.h>

#define NMAX 200100

int t;
int h[NMAX];
int poz[NMAX];
int val[NMAX];
int n, nr;

void up(int x)
{
	int aux;
	while(x/2 && val[ h[x] ] < val[ h[x/2] ])
	{
		aux = h[x];
		h[x] = h[x/2];
		h[x/2] = aux;

		poz[ h[x] ] = x;
		poz[ h[x/2] ] = x/2;
                x /= 2;
	}
}

void down(int x)
{
	int aux, s = 0;
	while(s != x)
	{
		s = x;
		if(s*2 <= n && val[ h[x] ] > val[ h[s*2] ])
			x = s*2;
		if(s*2+1 <= n && val[ h[x] ] > val[ h[s*2+1] ])
			x = s*2+1;

		aux = h[x];
		h[x] = h[s];
		h[s] = aux;

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



int main()
{
	int x, y;
	freopen("heapuri.in", "r", stdin);
	freopen("heapuri.out", "w", stdout);

	scanf("%d", &t);

	while(t--)
	{
		scanf("%d", &x);
		if(x != 3)
			scanf("%d", &y);
		if(x == 1)
		{
			h[++n] = ++nr;
			poz[nr] = n;
			val[nr] = y;
			up(n);
		}
		else if(x == 2)
		{
			val[ y ] = -1;
			up(poz[y]);

			h[1] = h[n];
			poz[ h[1] ] = 1;
			--n;
			if(n > 1)
				down(1);
		}
		else
			printf("%d\n", val[ h[1] ]);
	}

	return 0;
}