Cod sursa(job #651396)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 20 decembrie 2011 11:02:00
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
using namespace std;

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

const int dim = 200005;
int A[dim], H[dim], P[dim], N, t, x;

void upheap (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]]);
		
		f = t;
		t >>= 1;		
	}
}

void downheap (int t)
{
	int f = t<<1;
	if (f+1 <= H[0] && A[H[f+1]] < A[H[f]])
		f++;
	while (f <= H[0] && A[H[f]] < A[H[t]])
	{
		swap (H[t], H[f]);
		swap (P[H[t]], P[H[f]]);
		
		t = f;
		f = t<<1;
		if (f+1 <= H[0] && A[H[f+1]] < A[H[f]])
			f++;
	}	
}

int main ()
{
	fi >> N;
	while (N --)
	{
		fi >> t;
		if (t == 3)
		{
			fo << A[H[1]] << '\n';			
		}
		else
		{
			fi >> x;
			if (t == 1)
			{
				A[++A[0]] = x;
				H[++H[0]] = A[0];
				P[A[0]] = H[0];
				upheap (H[0]);
			}
			else
			{
				//p = P[x];
				H[P[x]] = H[H[0]];
				P[H[H[0]]] = P[x];
				H[0]--;
				upheap (P[x]);
				downheap (P[x]);
			}			
		}		
	}
	
	return 0;
}