Cod sursa(job #1577048)

Utilizator casuneanu.andreiCasuneanu Andrei Dan casuneanu.andrei Data 23 ianuarie 2016 10:47:09
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <iostream>
#define IN "heapuri.in"
#define OUT "heapuri.out"
#define DMAX 200000
using namespace std;

ifstream fin(IN);
ofstream fout(OUT);

int h[DMAX];
int pos[DMAX], npos;
int nb[DMAX];
int nbx;
int n;

void insert(int);
void merge(int);

int main(){
	fin >>nbx;

	int i, type, x, j;
	for (i = 0; i < nbx; ++i){
		fin >>type;
		if (type == 1){
			//insert
			fin >>x;
			insert(x);
		}
		else if (type == 2){
			fin >>x;
			// for (j = 1; j <= n; ++j){
			// if (pos[j] == x){
			//sterg
			// cout <<nb[x]<<' ';
			j = nb[x];
			h[j] = h[n];
			pos[j] = pos[n];
			nb[pos[j]] = j;
			--n;
			merge(j);
		}
		else if (type == 3){
			//min value
			fout <<h[1]<<'\n';
		}
	}

	fout.close();
}

void merge(int i){
	int inf = h[i];
	int father = i;
	int son = 2*i;
	int final = pos[i];

	while (son <= n){
		if (son+1 <= n && h[son] > h[son+1])
			++son;

		if (h[son] < inf){
			h[father] = h[son];
			pos[father] = pos[son];
			nb[pos[father]] = father;
			father = son;
			son *=2;
		}
		else break;
	}
	h[father] = inf;
	pos[father] = final;
	nb[pos[father]] = father;
}

void insert(int inf){
	int son = ++n;

	while (son/2 > 0 && h[son/2] > inf){
		h[son] = h[son/2];
		pos[son] = pos[son/2];
		nb[pos[son]] = son;
		son /= 2;
	}
	// fout <<son<<' ';
	h[son] = inf;
	pos[son] = ++npos;
	nb[pos[son]] = son;
}