Cod sursa(job #1061318)

Utilizator donydony2009FMI - Donisan George donydony2009 Data 19 decembrie 2013 16:16:46
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include<fstream>
using namespace std;
int heap[200001],pos[200001],S[200010],n,NR;

int Up(int poz)
{
	while(S[heap[poz/2]]>S[heap[poz]]&&poz>1)
	{
		swap(heap[poz/2],heap[poz]);
		swap(pos[heap[poz/2]],pos[heap[poz]]);
		poz/=2;
	}
	return poz;
}
int insert(int val)
{
	heap[++n]=NR;
	pos[heap[n]]=n;
	return Up(n);
}
int Min(void)
{
	return S[heap[1]];
}
int down(int poz)
{
	int poz1;
	if (poz>=n)
		return 0;
	poz1=poz*2;
	if(poz1>n)
		return 0;
	if(poz1+1<=n&&S[heap[poz1+1]]<S[heap[poz1]])
		poz1++;
	if(S[heap[poz]]>S[heap[poz1]])
	{
		swap(heap[poz],heap[poz1]);
		swap(pos[heap[poz]],pos[heap[poz1]]);
		down(poz1);
	}
}
void deletePos(int poz)
{
	swap(heap[n--],heap[poz]);
	pos[heap[poz]]=poz;
	if(poz<=n)
	{
	Up(poz);
	down(poz);
	}
}
int main(void)
{
	ifstream f("heapuri.in");
	ofstream g("heapuri.out");
	int x,y,N,i,s,j;
	NR=0;
	f>>N;
	for(i=1;i<=N;i++)
	{
		f>>x;
		if(x==1)
		{
			f>>y;
			S[++NR]=y;
			insert(y);
		}
		if(x==2)
		{
			f>>y;
			S[heap[pos[y]]]=-1;
			deletePos(pos[y]);
		}
		if(x==3)
			g<<Min()<<"\n";
	}
}