Cod sursa(job #1236610)

Utilizator DenisacheDenis Ehorovici Denisache Data 2 octombrie 2014 11:11:00
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <limits.h>
using namespace std;
struct el
{
	int H[200005],A[200005],L,nr,poz[200005];
	void inline push(int x)
	{
		while (A[H[x]]<A[H[x>>1]] && x>>1)
		{
			swap(H[x],H[x>>1]);
			poz[H[x]]=x;
			poz[H[x>>1]]=x>>1;
			x>>=1;
		}
	}
	void inline pop(int x)
	{
		int y=0,z;
		while (x!=y)
		{
			y=x,z=x<<1;
			if (z<=L && A[H[z]]<A[H[x]]) x=z;
			if (z+1<=L && A[H[z+1]]<A[H[x]]) x=z+1;
			swap(H[x],H[y]);
			poz[H[x]]=x;
			poz[H[y]]=y;
		}
	}
    void insert(int x)
    {
    	A[++nr]=x;
    	H[++L]=nr;
    	poz[nr]=L;
    	push(L);
    }
    void erase(int x)
    {
    	A[x]=-1;
    	push(poz[x]);
    	poz[H[1]]=0;
    	H[1]=H[L--];
    	poz[H[1]]=1;
    	if (L>1) pop(1);
    }
    int top(){return A[H[1]];}
}s;
int N;
int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	scanf("%d",&N);
	while (N--)
	{
		int op,x;
		scanf("%d",&op);
		if (op==1)
		{
			scanf("%d",&x);
			s.insert(x);
		}
		else if (op==2)
		{
			scanf("%d",&x);
			s.erase(x);
		}
		else printf("%d\n",s.top());
	}
}