Cod sursa(job #933451)

Utilizator siminescuPaval Cristi Onisim siminescu Data 29 martie 2013 23:22:51
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include<fstream>
using namespace std;

#define nmax 200002
ifstream f("heapuri.in");
ofstream g("heapuri.out");

long long P[nmax], H[nmax], N;

int leftSon(int pos) {
	
	return 2 * pos + 1;
}

int rightSon(int pos) {
	
	return 2 * (pos + 1);
}

int father(int pos) {
	
	return (pos - 1) / 2;
}

void swap(int i, int j) {
	
	int aux = H[j];
	H[j] = H[i];
	H[i] = aux;
	aux = P[i];
	P[i] = P[j];
	P[j] = aux;
}

void percolate(int pos) {
	
	while(H[pos] < H[father(pos)]) {
		swap(pos, father(pos));
		pos = father(pos);
		if(pos == 0)
			break;
	}
}

void addElement(long long e) {

	H[N] = e; P[N] = N;
	N++;
	percolate(N - 1);
}

int posOfMinimSon(int pos) {
	
	if(H[leftSon(pos)] < H[rightSon(pos)])
		return leftSon(pos);
	return rightSon(pos);
}

void sift(int pos) {
	
	while(posOfMinimSon(pos) < N) {
		if(H[pos] < H[posOfMinimSon(pos)])
			break;
		swap(pos, posOfMinimSon(pos));
		pos = posOfMinimSon(pos);
	}
}

void removeElement(int indecs) {
	
	int pos = P[indecs];
	swap(pos, N - 1);
	N--;
	sift(pos);
}

void printMinim() {
	
	g<<H[0]<<' ';
}

int main() {
	
	int M, opp;
	long long x;
	
	f>>M;
	
	for(;M;--M) {
		
		f>>opp;
		if(opp == 1) {
			f>>x;
			addElement(x);
		}
		else if(opp == 2) {
			f>>x;
			removeElement(x);
		}
		else {
			f>>x;
			printMinim();
		}
	}
	return 0;
}