Cod sursa(job #921574)

Utilizator Kira96Denis Mita Kira96 Data 21 martie 2013 08:53:39
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include<fstream>
#define NM 200001
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int t,h[NM],a[NM],poz[NM],po,i,n,m,x,u;
void sw(int x,int y)
{
	int aux;
	aux=poz[a[x]];
	poz[a[x]]=poz[a[y]];
	poz[a[y]]=aux;
	
	aux=h[x];
	h[x]=h[y];
	h[y]=aux;
	
	aux=a[x];
	a[x]=a[y];
	a[y]=aux;
}
int mi(int a,int b)
{
	if(h[a]>h[b])
	return b;
	return a;
}
void urca(int k)
{
	while(h[k]<h[k>>1]&&k>=2)
	{
		sw(k,k>>1);
		k>>=1;	}
}
void coboara(int k)
{
	while((k<<1)<=n&&(h[k]>h[(k<<1)]||h[k]>h[(k<<1)+1]))
	{
		int c=mi(k<<1,(k<<1)+1);
		if((k<<1)+1>n)
		c=k<<1;
		sw(c,k);
		k=c;
	}
}
int main ()
{
	f>>m;
	for(i=1;i<=m;++i)
	{
		f>>t;
		if(t==1)
		{
			f>>x;
			++u;
			n++;
			a[n]=u;
			poz[u]=n;
			h[n]=x;
			urca(n);
		}
		else
		if(t==2)
		{
			f>>x;
			po=poz[x];
			sw(poz[x],n);
			h[n]=a[n]=poz[x]=0;
			n--;
			if(po<=n){
			urca(po);
			coboara(po);}
		}
		else
		g<<h[1]<<"\n";
	}
	return 0;
}