Cod sursa(job #653547)

Utilizator johnny2008Diaconu Ion johnny2008 Data 28 decembrie 2011 12:17:06
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>
#include<iostream>
using namespace std;
int n, l, nr;
int a[200010], heap[200010], poz[200010];
void insereaza(int x)
{
	int aux;
	while (x>>1 && a[heap[x]]<a[heap[x>>1]])
	{
		aux=heap[x];
		heap[x]=heap[x>>1];
		heap[x>>1] = aux;
		poz[heap[x]] = x;
		poz[heap[x>>1]] = x>>1;
		x=x>>1;
	}
}
void sterge(int x)
{
	int aux, y = 0;
	while (x != y)
	{
		y = x;
		if ((y<<1)<=l && a[heap[x]]>a[heap[y<<1]]) 
			x=y<<1;
		if ((y<<1)+1<=l && a[heap[x]]>a[heap[(y<<1)+1]]) 
			x=(y<<1)+1;
		aux = heap[x];
		heap[x] = heap[y];
		heap[y] = aux;
		poz[heap[x]] = x;
		poz[heap[y]] = y;
	}
}

int main()
{
	ifstream f("heapuri.in");
	ofstream g("heapuri.out");
	f>>n;
	int i,x,lol;
	for (i=1; i<=n; i++)
	{
		for(int j=1;j<=l;j++){
			cout<<a[heap[j]]<<" ";
		}
		cout<<"\n";
		f>>x;
		if(x<3)
			f>>lol;
		if (x==1)
		{
			l++;
			nr++;
			a[nr] = lol;
			heap[l] = nr;
			poz[nr] = l;
			insereaza(l);
		}
		if (x==2)
		{
			a[lol] = -1;
			insereaza(poz[lol]);
			poz[heap[1]] = 0;
			heap[1] = heap[l--];
			poz[heap[1]] = 1;
			if (l>1) 
				sterge(1);
		}
		if (x==3)
			g<<a[heap[1]]<<"\n";
	}
	return 0;
}