Cod sursa(job #779709)

Utilizator crushackPopescu Silviu crushack Data 18 august 2012 16:23:02
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <stdio.h>
#include <algorithm>
#define NMax 200010
using namespace std;

const char IN[]="heapuri.in",OUT[]="heapuri.out";

int N,L,Len;
bool b[NMax];
int H[NMax] , v[NMax];

inline bool cmp(int x,int y){
	return v[x]<v[y];
}

void remake(int x){
	int l=2*x,r=l+1,Min=x;

	if (l<=L && cmp(H[l],H[Min])) Min=l;
	if (r<=L && cmp(H[r],H[Min])) Min=r;

	if (Min!=x)
	{
		swap(H[Min],H[x]);
		remake(Min);
	}
}

void pop(){
	swap(H[1],H[L]);--L;
	remake(1);
}

void push(int x){
	H[++L]=x;
	for (int i=L;i>1 && cmp(H[i],H[i>>1]);i>>=1)
		swap(H[i],H[i>>1]);
}

int main()
{
	int c,x;
	freopen(IN,"r",stdin);
	scanf("%d",&N);
	freopen(OUT,"w",stdout);
	while (N--)
	{
		scanf("%d",&c);
		while (L>0 && !b[H[1]]) pop();
		if (c==1){
			scanf("%d",&v[++Len]);
			push(Len);b[Len]=true;
		}
		else if (c==2) {
			scanf("%d",&x);
			b[x]=false;
		}
		else{
			printf("%d\n",v[H[1]]);
		}
	}
	fclose(stdout);fclose(stdin);
	return 0;
}