Cod sursa(job #955998)

Utilizator tudorv96Tudor Varan tudorv96 Data 1 iunie 2013 23:45:10
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#include <iostream>
#include <cassert>
using namespace std;

#define N 200005
#define in "heapuri.in"
#define out "heapuri.out"
#define xx first
#define yy second
#define oo (1 << 30)

typedef pair <int, int> date;

int ord[N], n, elem, hs; //ord - pozitia in heap a elemntului al i-lea adaugat
date H[N]; //first - elem, second - pozitia in ord a elementului

void swap(date &x, date &y) {
	date aux = x;
	x = y;
	y = aux;
}

void swap(int &x, int &y) {
	int aux = x;
	x = y;
	y = aux;
}

void insert(int x) {
	H[++hs].xx = x;
	H[hs].yy = ++elem;
	ord[elem] = hs;
	int k = hs;
	while (k / 2 > 0 && H[k / 2].xx > H[k].xx) {
		swap (ord[H[k].yy], ord[H[k / 2].yy]);
		swap (H[k], H[k / 2]);
		k /= 2;
	}
}

void eject(int i) {
	H[i] = H[hs];
	H[hs--].xx = oo;
	ord[H[i].yy] = i;
	int k = i, fiu = 0;
	while (fiu <= hs) {
		fiu = k * 2;
		if (fiu + 1 <= hs && H[fiu + 1].xx < H[fiu].xx)
			fiu++;
		if (fiu <= hs && H[fiu].xx < H[k].xx) {
			swap (ord[H[k].yy], ord[H[fiu].yy]);
			swap (H[k], H[fiu]);
			k = fiu;
		}
		else
			break;
	}
}
				

int main() {
	ifstream fin (in);
	fin >> n;
	ofstream fout (out);
	for (int i = 1; i < N; ++i)
		H[i].xx = oo;
	for (int i = 0; i < n; ++i) {
		int t;
		fin >> t;
		if (t == 3) {
			fout << H[1].xx << "\n";
			continue;
		}
		int x;
		fin >> x;
		if (t == 1) {
			insert(x);
		}	
		if (t == 2) {
			eject(ord[x]);
		}
	}
	fcloseall();
	return 0;
}