Cod sursa(job #656159)

Utilizator informatician28Andrei Dinu informatician28 Data 4 ianuarie 2012 00:41:47
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include<fstream> 
#include<algorithm> 
#define NMAX 200001
using namespace std; 

ifstream in("heapuri.in"); 
ofstream out("heapuri.out"); 

int N, Value[NMAX], Pos[NMAX], Heap[NMAX], Heap_size=0; 
inline void Swap(int X, int Y) 
{
	swap(Pos[Heap[X]],Pos[Heap[Y]]);
    swap(Heap[X],Heap[Y]);	
}	
void Sift(int X) 
{
	int Son=(X<<1); 
	if(Son+1 <= Heap_size and Value[Heap[Son+1]]< Value[Heap[Son]]) 
		Son++; 
	
	if(Son <= Heap_size and Value[Heap[Son]] < Value[Heap[X]]) 
	{
		Swap(X, Son); 
		Sift(Son); 
	}
}
void Percolate(int X) 
{
	int Father = (X>>1); 
	if(Father > 0 and Value[Heap[X]] < Value[Heap[Father]]) 
	{
		Swap(X,Father); 
		Percolate(Father); 
	}
}	
void Insert(int X) 
{
	Heap[++Heap_size] = X; 
	Pos[X] = Heap_size; 
	Percolate(Pos[X]); 
}
void Delete(int X) 
{
	Swap(X, Heap_size); 
	Pos[Heap[Heap_size]]=0; 
	Heap[Heap_size]=0;
    --Heap_size;	
	Sift(X);
}
int main() 
{
	int M, cod,x; 
	
	in>>M; 
	for(int i = 1; i <= M; i++) 
	{
		in >> cod; 
		if(cod == 1)
		{
			++N; 
			in >> Value[N];
			Insert(N); 
		}
		
		else if(cod == 2) 
		{
			in >> x; 
			Delete(Pos[x]); 
		}
		
		else 
			out << Value[Heap[1]] <<'\n'; 
	}
}