Cod sursa(job #464090)

Utilizator alisssiaMititelu Andra alisssia Data 18 iunie 2010 18:28:42
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
using namespace std;
#include<cstdio>
#define nmax 200005
long long h[nmax],ord[nmax],b;
long long a,poz[nmax],nr,k,n,j;

void schimb (int x, int y)
{
	int aux=h[x];
	h[x]=h[y];
	h[y]=aux;
	poz[h[x]]=x;
	poz[h[y]]=y;
}

void upheap(int i)
{
	int tata=i/2;
	while(h[tata]>h[i] && tata)
		{schimb(tata,i); i=tata; tata=tata/2;}
}

void downheap(int i)
{
	int fiu=2*i;
	while(fiu<=nr)
	{
		if(fiu<nr && h[fiu]>h[fiu+1]) fiu++;
		if(h[fiu]<h[i]) {schimb(fiu,i); i=fiu; fiu=fiu*2;}
		else fiu=nr+1;
	}
}

int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	scanf("%lli",&n);
	for(k=1;k<=n;k++)
	{
		scanf("%lli",&a);
		if(a==1) {scanf("%lli",&b); ord[++j]=b; h[++nr]=b; poz[b]=nr; upheap(nr);}
		else
		if(a==2) {scanf("%lli",&b); b=poz[ord[b]]; schimb(b,nr); nr--; downheap(b);}
		else
		if(a==3) printf("%lli\n",h[1]);
	}
	return 0;
}