Cod sursa(job #563492)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 25 martie 2011 12:18:45
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <stdio.h>
#define DIM 200005

int N, X, H[DIM], I[DIM];

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

void urca (int n)
{
	int t = n >> 1;
	if (t && H[t] > H[n])
	{
		swap (H[t], H[n]);
		swap (I[t], I[n]);
		urca (t);
	}
}


int main ()
{
	freopen ("heapuri.in", "r", stdin);
	freopen ("heapuri.out", "w", stdout);
	
	scanf ("%d", &N);
	for (int i = 0, t, x; i < N; i++)
	{
		scanf ("%d", &t);
		switch (t)
		{
			case 1:
				scanf ("%d", &X);
				H[++H[0]] = X;
				I[++I[0]] = H[0];
				urca (H[0]);
				
				break;
			case 2:
				scanf ("%d", &X);
				x = I[X]; 				
				urca (x);
				
				break;
			case 3:
				printf ("%d ", H[1]);
				break;
		}
	}
	
	
	return 0;
}