Cod sursa(job #663668)

Utilizator ELHoriaHoria Cretescu ELHoria Data 18 ianuarie 2012 20:17:39
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int H[200005] , pos[200005] , A[200005] , N , K , NR , cod ;

void heap_up(int nod)
{
	for(;nod > 1 &&  A[H[nod]] < A[H[nod>>1]];nod>>=1)
	{
		swap(H[nod],H[nod>>1]);
		pos[H[nod]] = nod;
		pos[H[nod>>1]] = nod>>1;
	}
}

void heap_down(int x)
{
	int y = 0;
	while(x != y)
	{
		y = x;
		if(2*y <= K && A[H[x]] > A[H[y*2]]) x = y*2;
		if(2*y + 1 <= K && A[H[x]] > A[H[y*2 + 1]]) x = y*2 + 1;
		swap(H[x],H[y]);
		pos[H[x]] = x , pos[H[y]] = y;
	}
}

int main()
{
	for(fin>>N;N;N--)
	{
		int x;
		fin>>cod;
		if(cod == 3) fout<<A[H[1]]<<'\n';
		else
		{
			fin>>x;
			if(cod == 2) 
			{
				A[x] = -1;
				heap_up(pos[x]);
				pos[H[1]] = 0;	H[1] = H[K--]; pos[H[1]] = 1;
				if(K > 1) heap_down(1);
			}
			else
			if(cod == 1)
			{
				NR++ ,K++;
				A[NR] = x; H[K] = NR; pos[NR] = K;
				heap_up(K);
			}
		}
	}
	return 0;
}