Cod sursa(job #755989)

Utilizator andreea29Iorga Andreea andreea29 Data 8 iunie 2012 15:25:26
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include<fstream>
#define INF 2000000
using namespace std;
int h[200010], t[200010], intr[200010], n, i, nr, c, op, x, aux;
int main()
{
	ifstream f("heapuri.in");
	ofstream g("heapuri.out");
	f>>n;
	nr=0;
	for (i=1; i<=n; ++i)
	{
		f>>op;
		if (op!=3)
		{
			f>>x;
			if (op==1)
			{
				nr++;
				h[nr]=x;
				c=nr;
				intr[nr]=c;
				t[nr]=c;
				while (h[c/2]>h[c] && c>1)
				{
					aux=h[c/2];
					h[c/2]=h[c];
					h[c]=aux;
					aux=t[c/2];
					t[c/2]=t[c];
					t[c]=aux;
					intr[t[c/2]]=c/2;
					intr[t[c]]=c;
					c=c/2;
				}
				h[nr+1]=INF;
			}
			else
			{
				h[intr[x]]=h[nr];
				nr--;
				h[nr+1]=INF;
				c=intr[x];
				while (2*c<=nr && (h[2*c]<h[c] || h[2*c+1]<h[c]))
				{
					if (h[2*c]<h[2*c+1] || 2*c+1>nr)
					{
						aux=h[2*c];
						h[2*c]=h[c];
						h[c]=aux;
						aux=t[2*c];
						t[2*c]=t[c];
						t[c]=aux;
						intr[t[2*c]]=2*c;
						intr[t[c]]=c;
						c=c*2;
					}
					else
						{
							aux=h[2*c+1];
							h[2*c+1]=h[c];
							h[c]=aux;
							aux=t[2*c+1];
							t[2*c+1]=t[c];
							t[c]=aux;
							intr[t[2*c+1]]=2*c+1;
							intr[t[c]]=c;
							c=c*2+1;
							
						}
				}
				
			}
		}
		else 
		{
			g<<h[1]<<'\n';
		}
	}	
	
	f.close();
	g.close();
	return 0;
}