Cod sursa(job #631434)

Utilizator tomaAndrei Toma toma Data 8 noiembrie 2011 00:57:37
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include<stdio.h>
#include<vector>
#include<algorithm>
#define Nmax 200005

using namespace std;

int P,Poz,x,N,i,j,M,type,nr;
vector<int> H(Nmax),poz(Nmax),a(Nmax);

void heap_up(int nod)
{
	while ( (nod>>1) && (a[H[nod>>1]]>a[H[nod]]) )
	{
		swap(H[nod],H[nod>>1]);
		swap(poz[H[nod]],poz[H[nod>>1]]);
		nod>>=1;
	}
}

void heap_down(int nod)
{
	while (nod<<1<=N)
	{
		if ( ((nod<<1)+1<=N) && (a[H[(nod<<1)+1]]<a[H[nod<<1]]) ) Poz=(nod<<1)+1;
			else Poz=nod<<1;
		if (a[H[Poz]]>a[H[nod]]) return;
		
		swap(H[nod],H[Poz]);
		swap(poz[H[nod]],poz[H[Poz]]);
		nod=Poz;
	}
}
inline void swapp(int a,int b)
{
    int c;
    c=H[a];
    H[a]=H[b];
    H[b]=c;
    poz[H[a]]=a;
    poz[H[b]]=b;
}

int main()
{
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	
	scanf("%d",&M);
	for (i=1;i<=M;++i)
	{
		scanf("%d",&type);
		if (type==1)
		{
			scanf("%d",&x);
			a[++nr]=x;
			H[++N]=nr;
			poz[nr]=N;
			heap_up(N);
		}
		else if (type==2)
		{
			scanf("%d",&x);
			P=poz[x];
			
			swap(H[P],H[N]);
			swap(poz[H[P]],poz[H[N]]);
			
			heap_down(P);
		}
		else if (type==3)
			printf("%d\n",a[H[1]]);
	}
}