Cod sursa(job #2398846)

Utilizator The_one_and_onlyMironica Vasile The_one_and_only Data 6 aprilie 2019 11:46:56
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
using namespace std;

ifstream cin("heapuri.in");
ofstream cout("heapuri.out");

int n, a[200010], vf, x, cer, h[200010], p[200010], size;

void push(int x) {
	while(x >> 1 && a[h[x]] < a[h[x >> 1]])
		swap(h[x], h[x >> 1]),
		p[h[x]] = x,
		p[h[x >> 1]] = x >> 1,
		x >>= 1;
}

void pop(int x) {
	int y = 0;
	while(x != y) {
		y = x;
		if(2 * y <= vf && a[h[x]] > a[h[2 * y]])
			x = 2 * y;
		if(2 * y + 1 <= vf && a[h[x]] > a[h[2 * y + 1]])
			x = 2 * y + 1;
		
		swap(h[x], h[y]);
		
		p[h[x]] = x;
		p[h[y]] = y;
	}
}

int main() {
	cin >> n;
	while(n--) {
		cin >> cer;
		switch(cer) {
			case 1:
				cin >> x;
				
				vf++; size++;
				
				a[size] = x;
				h[vf] = size;
				p[size] = vf;
				
				push(vf);
				break;
			case 2:
				cin >> x;
				
				a[x] = -1;
				push(p[x]);
				
				p[h[1]] = 0;
				h[1] = h[vf--];
				p[h[1]] = 1;
				
				if(vf > 1)
					pop(1);
				break;
			case 3:
				cout << a[h[1]] << '\n';
				break;
		}
	}
	return 0;
}