Cod sursa(job #1204425)

Utilizator IonMosnoiIon Mosnoi IonMosnoi Data 2 iulie 2014 21:57:31
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream o("heapuri.out");

int n,hp=0,poz[210000],h[200020],a[200200];

void urca(int x){
	 int tata = poz[x]/2, fiu = poz[x];
	 while(a[h[fiu]]<a[h[tata]] && tata != 0){
	 	swap(poz[h[tata]],poz[h[fiu]]);
	 	swap(h[tata],h[fiu]);
	 	fiu=tata;
	 	tata =tata/2;
	 }
}

void adauga(int x){
	hp++;
	poz[hp]=hp;
	h[hp]=hp;
	urca(x);
}

void coboara(int x){
//o<<x;	
	 int tata = poz[x],fiu = poz[x]*2;
	 while(fiu<=hp){
	 	if(a[h[fiu]] < a[h[tata]]){
	 		if(a[h[fiu]]>a[h[fiu+1]] && fiu+1<=hp)fiu++;
	 		swap(poz[h[tata]],poz[h[fiu]]);
		 	swap(h[tata],h[fiu]);
		 	tata=fiu;
		 	fiu = fiu*2;
	 	}else fiu = hp+1;
	 }
}

void sterge(int x){
	
   	 poz[h[hp]]= poz[x];
   	 swap(h[hp],h[poz[x]]);
	 hp--;
	 if(a[h[poz[x]]]<a[h[poz[x]/2]] && poz[x]!=1)urca(h[poz[x]]);
	 else coboara(h[poz[x]]);
}

int main(){
	f>>n;int x,y,j=0;
	for(int i=1;i<=n;i++){
		f>>x;
		if(x==1){
			f>>a[++j];		
		    adauga(j);
		}else if(x==2){
			f>>y;sterge(y);
		}else o<<a[h[1]]<<"\n";
		
	}
	
	
}