Cod sursa(job #862852)

Utilizator ioanapopaPopa Ioana ioanapopa Data 22 ianuarie 2013 23:14:48
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <cstdio>
#define MAX 200001
using namespace std;

int heap[MAX],v[MAX],p[MAX],n,m;
 
void swap(int&x,int&y)
{
	int z=x;
	x=y;
	y=z;
}
 
void push(int x)
{
    int t,f;
    n++;
	m++;
    heap[n]=x;
    p[m]=n;
    v[n]=m;
    f=n; 
	t=f/2;
    while(t!=0&&heap[f]<heap[t])
	{
        swap(heap[t],heap[f]);
        p[v[t]]=f;
        p[v[f]]=t;
        swap(v[t],v[f]);
        f=t; 
		t=f/2; 
	}
}
 
void pop(int x)
{
    int t,f;
    heap[p[x]]=heap[n];
    p[v[n]]=p[x];
    v[p[x]]=v[n];
    n--;
    t=p[x]; 
	f=t*2; 
	if(f+1<=n&&heap[f+1]<heap[f])
		f++;
    while(f<=n&&heap[t]>heap[f])
	{
        swap(heap[t],heap[f]);
        p[v[t]]=f;
        p[v[f]]=t;
        swap(v[t],v[f]);
        t=f; 
		f=t*2; 
		if(f+1<=n&&heap[f+1]<heap[f])
			f++;
	}
}
 
int main()
{
    int n,i,o,x;
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
        scanf("%d",&n);
        for(i=1;i<=n;i++)
		{
            scanf("%d",&o);
            if(o==1)
			{
				scanf("%d",&x); 
				push(x);
			} 
			if(o==2) 
			{
				scanf("%d",&x); 
				pop(x); 
			} 
			if(o==3)
				printf("%d\n",heap[1]); 
			
		}
}