Cod sursa(job #852777)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 11 ianuarie 2013 18:40:37
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <cassert>
using namespace std;

#define PRO "heapuri"
void OpenFiles(int EVAL)
{
	if(EVAL)
	{
		char input[100] = PRO, output[100] = PRO;
		freopen(strcat(input, ".in"),"r",stdin);
		freopen(strcat(output,".out"),"w",stdout);
	} else
	{
		freopen("test.in","r",stdin);
		//freopen("test.out","w",stdout);
	}
}

#define MAX 200001
#define INF 0xffffff

int n,pos[MAX],nr,k;
struct heap{ int x,k; }h[MAX];

void push(int x)
{
	int t,f;
	nr++,k++;
	h[nr].x = x;
	h[nr].k = k;
	pos[k] = nr;
	f = nr;
	t = f/2;
	while(t > 0 && h[f].x < h[t].x)
	{
		swap(h[t],h[f]);
		swap(pos[h[t].k],pos[h[f].k]);
		f = t;
		t = f/2;
	}
}

void pop(int k)
{
	int t,f;
	k = pos[k];
	h[k] = h[nr--];
	pos[h[k].k] = k;
	t = k;
	f = 2 * t;
	if(f+1 <= nr && h[f+1].x < h[f].x)f++;
	while( f<=nr && h[f].x < h[t].x )
	{
		swap(h[t],h[f]);
		swap(pos[h[t].k],pos[h[f].k]);
		t = f;
		f = 2 * t;
		if(f+1 <= nr && h[f+1].x < h[f].x )f++;
	}
}

int main(int argv,char *args[])
{
	OpenFiles(argv==0);
	// start
	int op,x;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&op);
			switch(op){
			case 1: scanf("%d",&x); push(x); break;
			case 2: scanf("%d",&x); pop(x); break;
			case 3: printf("%d\n",h[1].x); break;
			}
		}
	return 0;
}