Cod sursa(job #852679)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 11 ianuarie 2013 16:33:03
Problema Heapuri Scor 40
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

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

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

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

int main(int argv,char *args[])
{
	OpenFiles(argv==0);
	// start
	int m,op,x;
	scanf("%d",&m);
	for(int i=1;i<=m;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].val); break;
		}
	}
	return 0;
}