Cod sursa(job #553008)

Utilizator alexandru93moraru alexandru sebastian alexandru93 Data 13 martie 2011 13:13:44
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include<fstream>
#define nmax 200010
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,l,nr,c,z;
int hp[nmax],poz[nmax],a[nmax];

void upheap(int nod){
    int aux;
    while(a[hp[nod]]<a[hp[nod/2]]){
        aux=hp[nod];
        hp[nod]=hp[nod/2];
        hp[nod/2]=aux;
        poz[hp[nod/2]]=nod/2;
        poz[hp[nod]]=nod;
        nod/=2;
    }
}

void downheap(int x){   
int aux, y = 0; 
while (x != y){        
y = x;          
if (y*2<=l && a[hp[x]]>a[hp[y*2]]) x = y*2;       
if (y*2+1<=l && a[hp[x]]>a[hp[y*2+1]]) x = y*2+1;        
aux = hp[x];        
hp[x] = hp[y];        
hp[y] = aux;       
poz[hp[x]] = x;       
poz[hp[y]] = y; 
} 
} 
int main(){
    f>>n;
    for(int i=1;i<=n;i++){
    f>>c;
    if(c==1){
	l++,nr++;
	f>>z;
	a[nr]=z;
	hp[l]=nr;
	poz[nr]=l;
	upheap(l);
    }
    if(c==2){
	f>>z;
	a[z]=-1;
	upheap(poz[z]);
	poz[hp[1]]=0;
	hp[1]=hp[l--];
	poz[hp[1]]=1;
	downheap(1);
    }
    if(c==3)
    g<<a[hp[1]]<<'\n';
    }
g.close();
return 0;
}