Cod sursa(job #464130)

Utilizator alisssiaMititelu Andra alisssia Data 18 iunie 2010 21:52:26
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
using namespace std;
#include<cstdio>
#define nmax 200005
int h[nmax],a[nmax],b,tip;
int poz[nmax],nr,k,n,j,l;

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(a[h[tata]]>a[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 && a[h[fiu]]>a[h[fiu+1]]) fiu++;
		if(a[h[fiu]]<a[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("%d",&n); 
	for(k=1;k<=n;k++)
	{
		scanf("%d",&tip);
		if(tip==1) {scanf("%d",&b); a[++j]=b;h[++nr]=j; poz[j]=nr;upheap(nr);}
		else
		if(tip==2) {scanf("%d",&b); l=poz[b];schimb(poz[b],nr); nr--; downheap(l);}
		else
		if(tip==3) printf("%d\n",a[h[1]]);
	}
	return 0;
}