Cod sursa(job #436537)

Utilizator ooctavTuchila Octavian ooctav Data 8 aprilie 2010 17:24:36
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
#define NMAX 200001
int N;
int NR = 0;
int V[NMAX];
int L = 0;
int Heap[NMAX];
int P[NMAX];

void PUSH(int x)
{
	while(x>>1)
	{
		if(V[Heap[x>>1]] > V[Heap[x]]){
			swap(Heap[x>>1], Heap[x]);
			P[Heap[x]] = x;
			P[Heap[x>>1]] = x>>1;
			x = x>>1;
		}
		else
			break;
	}
}

void POP()
{
	int x = 1, y;
	while(2 * x <= L)
	{
		y = 2 * x;
		if(y + 1 <= L && V[Heap[2 * x + 1]] < V[Heap[2 * x]])
			y++;
		swap(Heap[y], Heap[x]);
		P[Heap[x]] = x;
		P[Heap[y]] = y;
		x = y;
	}
	Heap[x] = Heap[L];
	P[Heap[x]] = x;
}

void rezolva()
{
	int iden, x;
	scanf("%d",&N);
	for(int i = 1 ; i <= N ; i++)
	{
		scanf("%d",&iden);
		if(iden == 1){
			scanf("%d",&x);
			V[++NR] = x;
			Heap[++L] = NR;
			P[NR] = L;
			if(L > 1)
				PUSH(L);
		}
		if(iden == 2){
			scanf("%d",&x);
			V[x] = -1;
			PUSH(P[x]);
			POP();
			L--;
		}
		if(iden == 3)
			printf("%d\n",V[Heap[1]]);
	}
}

int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	rezolva();
	return 0;
}