Cod sursa(job #781012)

Utilizator BarracudaFMI-Alex Dobrin Barracuda Data 22 august 2012 23:23:23
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include<fstream>
#define lim 200002
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int op,V[lim],i,x,Pos[lim],H[3*lim],N,n;
inline int father(int nod){
	return nod/2;
}
inline int rs(int nod){
	return 2*nod+1;
}
inline int ls(int nod){
	return 2*nod;
}
void percolate(int k){
	while(k>1  && V[H[k]]<V[H[k/2]]){
		swap(Pos[H[k]],Pos[H[k/2]]);
		swap(H[k],H[k/2]);
		k/=2;
	}
}
void sift(int nod){
	int son=nod;
	if(V[H[ls(nod)]]<V[H[son]] &&ls(nod)<=N)
		son=ls(nod);
	if(rs(nod)<=N && V[H[rs(nod)]]<V[H[son]])
		son=rs(nod);
	if(son!=nod){
		swap(Pos[H[son]],Pos[H[nod]]);
		swap(H[nod],H[son]);
		sift(son);
	}
}
void cut(int x){
	swap(Pos[H[x]],Pos[H[N]]);
	swap(H[x],H[N]);
	--N;
	sift(x);
}
void add(int x){
	H[++N]=x;
	Pos[x]=N;
	percolate(Pos[x]);
}
int main (){
	f>>n;
	N=0;
	i=0;
	for(;n;n--){
		f>>op;
	    if(op!=3){
		    if(op==1){
			    f>>V[++i];
			    add(i);
		    }
		    else{
			   f>>x;
			    cut(Pos[x]);
		    }
	    }
	    else{
		    g<<V[H[1]]<<"\n";
	    }
	}
	return 0;
}