Cod sursa(job #630616)

Utilizator tomaAndrei Toma toma Data 6 noiembrie 2011 00:45:13
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include<stdio.h>
#include<vector>
#include<algorithm>
#define Nmax 200005

using namespace std;

int Poz,x,N,i,j,M,type,nr;
vector<int> H(Nmax),poz(Nmax);

void push_heap(int nod)
{
	while ( (nod>>1) && (H[nod>>1]>H[nod]) )
	{
		swap(H[nod],H[nod>>1]);
		swap(poz[nr],poz[nod>>1]);
		
		nod>>=1;
	}
}

void pop_heap(int nod)
{
	while (nod<<1<=N)
	{
		Poz=nod<<1;
		if ( ((nod<<1)+1<=N) && H[(nod<<1)+1]<H[Poz]) Poz++;
		
		swap(H[nod],H[Poz]);
		swap(poz[nod],poz[Poz]);
		nod=Poz;
	}
}

int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	
	scanf("%d",&M);
	for (i=1;i<=M;++i)
	{
		scanf("%d",&type);
		
		if (type==1)
		{
			scanf("%d",&x);
			H[++N]=x;
			poz[++nr]=N;
			push_heap(N);
		}
		else if (type==2)
		{
			scanf("%d",&x);
			swap(H[poz[x]],H[N--]);
			pop_heap(poz[x]);
		}
		else if (type==3)
			printf("%d\n",H[1]);
	}
}