Cod sursa(job #987771)

Utilizator Anca_PaneaPanea Anca Anca_Panea Data 21 august 2013 14:26:20
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
using namespace std;
#include<fstream>
ifstream eu("heapuri.in");
ofstream tu("heapuri.out");
# define Nmax 200005
int Heap[Nmax],Poz[Nmax],N,M,A[Nmax],i;

void Sift(int X)
{
	int Son=X<<1;
	if(Son+1<=N&&A[Heap[Son+1]]<A[Heap[Son]])
		++Son;
	if(Son<=N&&A[Heap[Son]]<A[Heap[X]])
	{
		swap(Heap[X],Heap[Son]);
		swap(Poz[Heap[X]],Poz[Heap[Son]]);
		Sift(Son);
	}
}

void Percolate(int X)
{
	int Father=X>>1;
	if(Father>0&&A[Heap[X]]<A[Heap[Father]])
	{
		swap(Heap[X],Heap[Father]);
		swap(Poz[Heap[X]],Poz[Heap[Father]]);
		Percolate(Father);
	}
}

int main()
{
	eu>>M;
	while(M--)
	{
		int type;int val;
		eu>>type;
		if(type!=3)
			eu>>val;
		if(type==1)
		{
			A[++i]=val;
			Heap[++N]=i;
			Poz[i]=N;
			Percolate(N);
		}
		if(type==2)
		{
			Heap[Poz[val]]=Heap[N];
			Poz[Heap[N]]=Poz[val];
			--N;
			Sift(Poz[val]);
		}
		if(type==3)
			tu<<A[Heap[1]]<<"\n";
	}
	
}