Cod sursa(job #853951)

Utilizator SovStoStoicescu Mihail Cristian SovSto Data 12 ianuarie 2013 16:35:44
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#include <cstlib>
#define nmax 200001
#define mmax 100000001
using namespace std;

fopen("heapuri.in","r",stdin);
fopen("heapuri.out","w",stdout);

int heap[nmax],poz[mmax],A[nmax];
int ind=0;


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

void up_heap(int x)
{
	if(x>1)
		if (A[heap[x/2]]>A[heap[x]])
		{
			swap(heap[x/2],heap[x]);
			swap(poz[heap[x/2]],poz[heap[x]]);
			up_heap(x/2);
		}
}

void down_heap(int x)
{
	int m;
	if (2*x<=ind)
	{
		m=2*x;
		if (m<=ind && A[heap[m+1]]<A[heap[m]])
			m++;
		if (A[heap[m]]<A[heap[x]])
		{
			swap(heap[m],heap[x]);
			swap(poz[heap[m]],poz[heap[x]]);
			down_heap(m);
		}
	}
}

int main()
{
	int n,tip,x;
	scanf("%d",&n);
	while(n>0)
	{
		scanf("%d",&tip);
		if(tip==1)
		{
			scanf("%d",&x);
			ind++;
			heap[ind]=ind;
			A[ind]=x;
			poz[ind]=ind;
			up_heap(ind);
		}
		else if(tip==2)
		{
			scanf("%d",&x);
			A[x]=mmax;
			down_heap(poz[x]);
		}
		else printf("%d \n",A[heap[1]]);
		n--;
	}
	return 0;