Cod sursa(job #331070)

Utilizator freak93Adrian Budau freak93 Data 12 iulie 2009 15:52:43
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>

using namespace std;

const int maxn=200010;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int n, p, k;
int a[maxn], h[maxn], pos[maxn];

void push(int x)
{
	int aux;

	while (x/2 && a[h[x]]<a[h[x/2]])
	{
		aux = h[x];
		h[x] = h[x/2];
		h[x/2] = aux;

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

void pop(int x)
{
	int aux, y = 0;

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

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

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

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

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

	f>>n;

	for (i=1; i<=n; i++)
	{
		f>>cod;
		if (cod < 3)
			f>>x;

		if (cod == 1)
		{
			p++, k++;
			a[k] = x;
			h[p] = k;
			pos[k] = p;

			push(p);
		}

		if (cod == 2)
		{
			a[x] = -1;
			push(pos[x]);

			pos[h[1]] = 0;
			h[1] = h[p--];
			pos[h[1]] = 1;
			if (p>1) pop(1);
		}

		if (cod == 3) g<<a[h[1]]<<"\n";
	}

	f.close();
	g.close();

	return 0;
}