Cod sursa(job #846464)

Utilizator test_13testing test_13 Data 2 ianuarie 2013 11:38:07
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <deque>
using namespace std;
#define Max 200001

int p[Max],n,k;
struct heap{ int k,c; }h[Max];

void push_heap(int val)
{
	k++; 
	n++;
	p[k]=n;
	h[n].k=k;
	h[n].c=val;
	int t,f;
	f=n; t=f/2;
	while(t>0 && h[f].c < h[t].c)
	{
		swap(h[t],h[f]);
		swap(p[h[t].k],p[h[f].k]);
		f=t; t=f/2;
	}
}

void pop_heap(int k)
{
	k=p[k];
	h[k]=h[n--];
	int t,f;
	t=k; f=2*t;
	if(f+1<=n && h[f+1].c<h[f].c)f++;
	while(f<=n && h[f].c < h[t].c)
	{
		swap(h[t],h[f]);
		swap(p[h[t].k],p[h[f].k]);
		t=f; f=2*t;
		if(f+1<=n && h[f+1].c<h[f].c)f++;
	}
}

int main()
{
	int m,op,x;
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
		scanf("%d",&m);
		for(int i=1;i<=m;i++)
		{
			scanf("%d",&op);
			switch(op)
			{
			case 1: scanf("%d",&x); push_heap(x); break;
			case 2: scanf("%d",&x); pop_heap(x); break;
			case 3: printf("%d\n",h[1].c);
			}
		}
	return 0;
}