Cod sursa(job #1155799)

Utilizator iarbaCrestez Paul iarba Data 27 martie 2014 10:46:45
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <cstdio>
using namespace std;
int a[200001],heap[200001],ph[200001],caz,n,m,t,poz;
void change(int n1, int n2)
{
	int aux=heap[n1];
	heap[n1]=heap[n2];
	heap[n2]=aux;
	ph[heap[n1]]=n1;
	ph[heap[n2]]=n2;
}
void push(int nod)
{
	int x=nod;
	while((x/2)&&(a[heap[x/2]]>a[heap[x]])){
		change(x,x/2);
										   }
}
void pop(int nod)
{
	int x=nod,y=0;
	while(x!=y){
		y=x;
		if((y*2<=m)&&(a[heap[x]]>a[heap[y*2]])){x=y*2;}
		if((y*2+1<=m)&&(a[heap[x]]>a[heap[y*2+1]])){x=y*2+1;}
		change(x,y);
			   }
}
int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	scanf("%ld",&t);
	while(t--){
		scanf("%ld",&caz);
		if(caz==1){
			scanf("%ld",&a[++n]);m++;
			heap[m]=n;ph[n]=m;
			push(n);
				  }
		if(caz==2){
			scanf("%ld",&poz);
			a[poz]=-1;
			push(ph[poz]);
			ph[heap[1]]=0;
			heap[1]=heap[m--];
			ph[heap[1]]=1;
			pop(1);
				  }
		if(caz==3){
			printf("%ld\n",a[heap[1]]);
				  }
			  }
return 0;
}