Cod sursa(job #407112)

Utilizator SzabiVajda Szabolcs Szabi Data 2 martie 2010 04:43:47
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <stdio.h>
#define MAX 200001
typedef long long tipus;

tipus h[MAX],a[MAX],poz[MAX],n=0,l,nn;


tipus apa(tipus x){return x/2;}
tipus jfia(tipus x){return 2*x+1;}
tipus bfia(tipus x){return 2*x;}

void perc(tipus x){
tipus key=h[x],temp=a[h[x]];

while((x>1)&&(a[h[apa(x)]]>temp)){
h[x]=h[apa(x)];
poz[h[x]]=x;
x=apa(x);
}

h[x]=key;
poz[h[x]]=x;
}

void sift(tipus x){
tipus fiu,temp;

do{
fiu=0;

if(bfia(x)<=l){
fiu=bfia(x);

if((jfia(x)<=l)&&(a[h[jfia(x)]]<a[h[bfia(x)]])){
	fiu=jfia(x);}
if(a[h[x]]<=a[h[fiu]]){fiu=0;}
}

if(fiu>0){
temp=h[x];
h[x]=h[fiu];
h[fiu]=temp;
poz[h[x]]=x;
poz[h[fiu]]=fiu;
x=fiu;
}

}while(fiu>0);

}


void betesz(tipus x){
n++;l++;
a[n]=x;
h[l]=n;
poz[n]=l;
perc(l);

}

void torol(tipus x){
h[x]=h[l];
l--;

if((x>1)&&(a[h[x]]<a[h[apa(x)]])){
	perc(x);}else{
		sift(x);}

}


int main(){

	freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
tipus i,temp1,temp2;
scanf("%lld",&nn);

for(i=1;i<=nn;i++){
scanf("%lld",&temp1);
if(temp1==3){printf("%lld\n",a[h[1]]);}else{
scanf("%lld",&temp2);

if(temp1==2){torol(poz[temp2]);}else{ betesz(temp2);}

}
}

	return 0;}