Cod sursa(job #822235)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 23 noiembrie 2012 00:34:19
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#include <algorithm>


using namespace std;

const int MAXN = 200010;

int H[MAXN], A[MAXN], Pos[MAXN];
int m, x, N, szHeap;

void push(int x) {
	while(x / 2 && A[H[x / 2]] > A[H[x]]) {
		Pos[H[x / 2]] = x; 
		Pos[H[x]] = x / 2;

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

		Pos[H[x]] = y; Pos[H[y]] = x;  
		swap(H[x], H[y]); 
	}
}

int main() {
	freopen("heapuri.in", "r", stdin);
	freopen("heapuri.out", "w", stdout);
	
	int tip;
	for( scanf("%d", &m); m--; ) {
		scanf("%d", &tip);
		if(tip == 1) {
			//adauga in heap
			scanf("%d", &x);
			A[++N] = x; 
			H[++szHeap] = N; 
			Pos[N] = szHeap;
			
			push(szHeap);
		} 
		if(tip == 2) {
			//sterge
			scanf("%d", &x);
			A[x] = -1; push(Pos[x]);
			
			Pos[H[1]] = 0;		
			H[1] = H[szHeap--];
			Pos[H[1]] = 1;
			if(szHeap > 1) pop(1);
		}
		if(tip == 3) //be the bauss
			printf("%d\n", A[H[1]]);
	}

	return 0;
}