Cod sursa(job #572356)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 5 aprilie 2011 11:27:01
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
using namespace std;
#define DIM 200005

ifstream fi("heapuri.in");
ofstream fo("heapuri.out");

int N, NH, NP;
int H[DIM], A[DIM], P[DIM];

void swap(int &a, int &b)
{
	int x = a;
	a = b;
	b = x;
}

void sus(int f)
{
	int t = f >> 1;
	while (t != 0 && A[H[t]] > A[H[f]])
	{		
		swap(H[t], H[f]);
		//swap(P[H[t]], P[H[f]]);
		P[H[t]] = t, P[H[f]] = f;
		f = t, t >>= 1;
	}
}

void jos(int t)
{
	int f = t << 1;
	while (f <= NH && A[H[t]] > A[H[f]])
	{
		if (f+1 <= NH && A[H[f]] > A[H[f+1]]) f++;
		
		swap(H[t], H[f]);
//		swap(P[H[t]], P[H[f]]);
		P[H[t]] = t, P[H[f]] = f;		
		t = f, f <<= 1;
	}	
}

int main()
{
	int t, x;
	fi >> N;
	while (N--)
	{
		fi >> t;
		switch (t)
		{
		case 1:
			NH++, NP++;
			fi >> A[NP];
			
			P[NP] = NH;
			H[NH] = NP;
			
			sus(NH);
			break;
		case 2:
			NH--;
			fi >> x;
			
			H[P[x]] = H[NH+1];
			P[H[NH+1]] = P[x];
			
			sus(P[x]);
			jos(P[x]);
			break;
		case 3:
			fo << A[H[1]] << '\n'; 
			break;
		}	
	}	
	
	return 0;
}