Cod sursa(job #1051271)

Utilizator bughybv31bogdan bughybv31 Data 9 decembrie 2013 21:32:57
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <cstdio>
#define N 200010
#define max  1000000100
int n;
int viz[N];
struct element
{
	int nr;
	int ordine;
};
element heap[N];
void shiftup(int i)
{
	if (i == 0)
		return;
	if (heap[i].nr < heap[i >> 1].nr)
	{
		element aux = heap[i];
		heap[i] = heap[i >> 1];
		heap[i >> 1] = aux;
		shiftup(i >> 1);
	}
}
void insertheap(element x , int *n)
{
	(*n)++;
	heap[*n] = x;
	shiftup(*n);
}
void shiftdown(int i , int k)
{
	int fiu = i;
	if (i << 1 > k)
		return;
	if (heap[fiu].nr > heap[i << 1].nr)
		fiu = i << 1;
	if (heap[fiu].nr > heap[(i << 1) + 1].nr)
		fiu = (i << 1) + 1;
	if (i == fiu)
		return;
	element aux = heap[i];
	heap[i] = heap[fiu];
	heap[fiu] = aux;
	shiftdown(fiu , k);
}
int main()
{
	int x;
	freopen ("heapuri.in" , "r" , stdin);
	freopen ("heapuri.out" , "w" , stdout);
	scanf ("%d" , &n);
	heap[0].nr = -max;
	int s = 0;
	int k = 0;
	for (int i = 0 ; i < n ; ++i)
	{
		int x;
		element y;
		scanf ("%d" , &x);
		if (x == 1)
		{
			scanf("%d" , &y.nr);
			s++;
			y.ordine = s;
			insertheap(y , &k);
		}
		if (x == 3)
		{
			while (viz[heap[1].ordine] == 1)
			{
				heap[1] = heap[k];
				k--;
				shiftdown(1 , k);
				//k--;
			}
			printf("%d\n" , heap[1].nr);
		}
		if (x == 2)
		{
			int c;
			scanf("%d" , &c);
			viz[c] = 1;
			
		}
				
		
	}
	/*
	for (int i = 1 ; i <= k ; ++i)
		printf ("%d " , heap[i].nr);*/
	return 0;
}