Cod sursa(job #1392971)

Utilizator CostanMiriamCostan Miriam CostanMiriam Data 19 martie 2015 00:06:22
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>

using namespace std;

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

int h[200010], v[200010], op, p, c, poz[200010], k, m, n, x;

void insert(int x) {
	h[++k] = x;
	poz[x] = k;
	c = k;
	p = k / 2;
	while (p>0) {
		if (v[h[p]]>v[h[c]]) {
			swap(h[c], h[p]);
			swap(poz[h[c]], poz[h[p]]);
			c = p;
			p /= 2;
		}
		else
			break;
	}
}

void delete_root() {
	h[1] = h[k];
	poz[h[k]] = 1;
	k--;
	p = 1; c = 2;
	while (c <= k) {
		if (c + 1 <= k && v[h[c + 1]]<v[h[c]])
			c++;
		if (v[h[p]]>v[h[c]]) {
			swap(h[c], h[p]);
			swap(poz[h[c]], poz[h[p]]);
			p = c;
			c *= 2;
		}
		else
			break;
	}
}

void erase(int x) {
	v[x] = -1;
	c = poz[x];
	p = c / 2;
	while (p>0) {
		if (v[h[p]]>v[h[c]]) {
			swap(h[c], h[p]);
			swap(poz[h[c]], poz[h[p]]);
			c = p;
			p /= 2;
		}
		else
			break;
	}
	delete_root();
}

int main() {

	fin >> m;
	while (m--) {
		fin >> op;
		if (op == 3) {
			fout << v[h[1]] << "\n";
			continue;
		}
		if (op == 1) {
			fin >> v[++n];
			insert(n);
			continue;
		}
		fin >> x;
		erase(x);
	}

	return 0;
}