Cod sursa(job #1925930)

Utilizator AGMinformaticaAGMInformatica AGMinformatica Data 13 martie 2017 20:39:26
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>
#include <algorithm>

using namespace std ;

const char IN[] = "heapuri.in" ;
const char OUT[] = "heapuri.out" ;
const int MAX = 2e5 + 14 ;

ifstream cin (IN) ;
ofstream cout (OUT) ;

int v[MAX], h[MAX], n, sz ;
bool deleted[MAX] ;

void UpHeap (int nod)
{
	if ((nod>>1) == 0){
		return ;
	}
	if (v[h[nod>>1]]>v[h[nod]]) {
		swap (h[nod>>1], h[nod]) ;
		UpHeap (nod>>1) ;
	}
	return ;
}

void DownHeap (int nod)
{
	if (nod > sz) {
		return ;
	}
	if (nod * 2 <= sz and nod * 2 + 1 <= sz and v[h[nod*2]] < v [h[nod]] and v [h[nod*2+1]] < v[h[nod]]) {
		if (v[h[nod*2]] < v [h[nod*2+1]]) {
			swap (h[nod*2], h[nod]) ;
			DownHeap (nod * 2) ;
			return ;
		}
		swap (h[nod*2 + 1], h[nod]) ;
		DownHeap (nod * 2 + 1) ;
		return ;
	}
	if (nod * 2 <= sz and v[h[nod*2]] < v [h[nod]]){
		swap (h[nod*2], h[nod]) ;
		DownHeap (nod * 2) ;
		return ;
	}
	if (nod * 2 + 1 <= sz and v [h[nod*2+1]] < v[h[nod]]) {
		swap (h[nod*2 + 1], h[nod]) ;
		DownHeap (nod * 2 + 1) ;
		return ;
	}
	return ;
}

void TypeOne (int x)
{
	n += 1 ;
	v [n] = x ;
	sz += 1 ;
	h [sz] = n ;
	UpHeap (sz) ;
}

void TypeTwo (int x)
{
	deleted [x] = 1;
}

int TypeThree ()
{
	while (deleted [h[1]] == 1) {
		swap (h[sz], h[1]) ;
		sz -= 1 ;
		DownHeap (1) ;
	}
	return v[h[1]] ;
}

int main()
{
	int m ;
	cin >> m ;
	while (m --){
		int type ;
		cin >> type ;
		switch (type){
			int x ;
			case 1 :
				cin >> x ;
				TypeOne (x) ;
				break ;
			case 2 :
				cin >> x ;
				TypeTwo (x) ;
				break ;
			case 3 :
				cout << TypeThree () << '\n' ;
				break ;
		}
	}
	return 0;
}