Cod sursa(job #854005)

Utilizator SovStoStoicescu Mihail Cristian SovSto Data 12 ianuarie 2013 17:27:45
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <cstdio>
#include <cstdlib>
#define nmax 200001
#define mmax 100000001
using namespace std;


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 (2*x+1<=ind && A[heap[2*x+1]]<A[heap[2*x]])
			m=2*x+1;
		if (A[heap[m]]<A[heap[x]])
		{
			swap(heap[m],heap[x]);
			swap(poz[heap[m]],poz[heap[x]]);
			down_heap(m);
		}
	}
}

int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	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;
}