Cod sursa(job #779611)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 18 august 2012 11:28:58
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<stdio.h>
int n,nr,nr2;
int a[200010],heap[200010],poz[200010];
void push(int x)
{
	int aux;
	while (x/2&&a[heap[x]]<a[heap[x/2]])
	{
		aux=heap[x];
		heap[x]=heap[x/2];
		heap[x/2]=aux;
		poz[heap[x]]=x;
		poz[heap[x/2]]=x/2;
		x/=2;
	}
}
void pop(int x)
{
	int aux,y=0;
	while (x!=y)
	{
		y=x;
		if (y*2<=nr&&a[heap[x]]>a[heap[y*2]])
            x=y*2;
		if (y*2+1<=nr&&a[heap[x]]>a[heap[y*2+1]])
            x=y*2+1;
		aux=heap[x];
		heap[x]=heap[y];
		heap[y]=aux;
		poz[heap[x]]=x;
		poz[heap[y]]=y;
	}
}
int main()
{
	freopen("heapuri.in", "r", stdin);
	freopen("heapuri.out", "w", stdout);
	int i, x,val;
	scanf("%d ",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d ",&val);
		if (val<3)
			scanf("%d ",&x);
		if (val==1)
		{
			nr++;
			nr2++;
			a[nr2]=x;
			heap[nr]=nr2;
			poz[nr2]=nr;
			push(nr);
		}
		if (val==2)
		{
			a[x]=-1;
			push(poz[x]);
			poz[heap[1]]=0;
			heap[1]=heap[nr--];
			poz[heap[1]]=1;
			if (nr>1)
                pop(1);
		}
		if (val==3)
            printf("%d\n",a[heap[1]]);
	}
	return 0;
}