Cod sursa(job #715373)

Utilizator ms-ninjacristescu liviu ms-ninja Data 17 martie 2012 08:41:42
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
using namespace std;
#define dim 150005
int v[dim], heap[dim], pos[dim],L;

void push(int x)
{
	while( x/2>0 && v[heap[x]]<v[heap[x/2]])
	{
		swap(heap[x],heap[x/2]);
		
		pos[heap[x]]=x;
		pos[heap[x/2]]=x/2;
		x/=2;
	}
}

void pop(int x)
{
	int y=0;
	
	while(x!=y)
	{
		y=x;
		if(y*2<=L && v[heap[x]]>v[heap[y*2]])
			x=y*2;
		if(y*2+1<=L && v[heap[x]]>v[heap[y*2+1]])
			x=y*2+1;
	
		swap(heap[x],heap[y]);
		pos[heap[x]]=x;
		pos[heap[y]]=y;
	}
}

int main()
{
	ifstream fin("heapuri.in");
	ofstream fout("heapuri.out");
	int t, x,nr=0,tip;
	fin>>t;
	
	for(;t;--t)
	{
		fin>>tip;
		
		if(tip<3)
			fin>>x;
		
		if(tip==3)
			fout<<v[heap[1]] <<'\n';			
		else
		if(tip==1)
		{
			++nr;
			++L;
			v[nr]=x;
			heap[L]=nr;
			pos[nr]=L;
			push(L);
		}
		else
		if(tip==2)
		{
			v[x]=-1;
			push(pos[x]);
			pos[heap[1]]=0;
			heap[1]=heap[L--];
			pos[heap[1]]=1;
			
			if(L>1)
				pop(1);
		}
			
	}
	
	return 0;
}