Cod sursa(job #1016709)

Utilizator dunhillLotus Plant dunhill Data 26 octombrie 2013 17:20:30
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <algorithm>
using namespace std;

#define NMAX 200001
#define T (u >> 1)
#define L (u << 1)
#define R (L | 1)

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

int i, c, n, p, x, N;

int poz[NMAX];
int v[NMAX];
int H[NMAX];

void upheap(int u) {
	while (u != 1 && v[H[u]] < v[H[T]]) swap(H[u], H[T]), poz[H[u]] = u, poz[H[T]] = T, u = T;
}

void downheap(int u) {
	while (1) {
		int m = u;
		if (L <= n && v[H[L]] < v[H[m]]) m = L;
		if (R <= n && v[H[R]] < v[H[m]]) m = R;
		if (m == u) 
			break;
		swap(H[u], H[m]);
		poz[H[u]] = u;
		poz[H[m]] = m;
		u = m;
	}
}

int main() {
	fin >> N;
	for (i = 1; i <= N; ++i) {
		fin >> c;
		if (c != 3) {
			fin >> x;
			if (c == 1) {
				++n;
				v[++p] = x;
				H[n] = p;
				poz[H[n]] = n;
				upheap(n);
				continue;
			}
			int u = poz[x];
			swap(H[u], H[n]);
			poz[H[u]] = poz[x];
			poz[H[n]] = n;
			--n;
			downheap(u);
		}
		else 
			fout << v[H[1]] << '\n';
	}
	return 0;
}