Cod sursa(job #315157)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 14 mai 2009 16:19:07
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include<algorithm>
using namespace std;

#define DIM 200001

struct heap{
    int val,poz;};

int n,m,cnt,a[DIM];
heap h[DIM];

void swap(int x,int y){
    heap aux;

    a[h[x].poz]=y;
    a[h[y].poz]=x;
    aux=h[x];
    h[x]=h[y];
    h[y]=aux;}

void uph(int k){
	int aux;

	while(k>1){
	    aux=k>>1;
	    if(h[k].val<h[aux].val){
	        a[h[k].poz]=aux;
	        a[h[aux].poz]=k;
	        swap(k,aux);}
        else
            return;}}

void downh(int k){
    int aux;

    while(k<=cnt){
        if(k<<1<=cnt){
            aux=k<<1;
            if(k+1<=cnt&&h[k+1].val<h[k].val)
                ++k;}
    else
        return;
    if(h[aux].val<h[k].val){
        a[h[aux].poz]=k;
        a[h[k].poz]=aux;
        swap(k,aux);}
    else
        return;}}

void insert(int val){

    h[++n].val=val;
    h[n].poz=cnt;
    uph(n);}

void cut(int k){

	a[h[n].poz]=k;
    h[k]=h[n--];
    if(k>1&&h[k].val<h[k>>1].val)
        uph(k);
    else
        downh(k);}

void solve(){
	int i,poz,tip,val;

    scanf("%d",&m);
	for(i=0; i<m; ++i){
        scanf("%d",&tip);
        if(tip==1){
            scanf("%d",&val);
            ++cnt;
            insert(val);}
        else if(tip==2){
			scanf("%d",&poz);
			cut(a[poz]);}
        else
            printf("%d\n",h[1].val);}}

int main(){

    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);

    solve();
    return 0;}