Pagini recente » Cod sursa (job #2076004) | Cod sursa (job #2552574) | Cod sursa (job #2648966) | Cod sursa (job #111435) | Cod sursa (job #1192840)
#include<fstream>
#include<iostream>
using namespace std;
fstream in ( "heapuri.in" , ios::in ),
out( "heapuri.out", ios::out);
int h[200000], poz[200000], v[200000], n, nh, c;
void adauga(int p);
void sterge(int p);
void urca(int p);
void coboara(int p);
void schimba(int p1, int p2);
int main(){
int x, k;
in >> n;
for( int i=0 ; i<n; i++ ){
in >> x;
if( x == 1 ){
c++;
in >> v[c];
adauga( c );
}
if( x == 2 ){
in >> k;
sterge( poz[k] );
}
if( x == 3 ){
out << v[h[1]] <<'\n';
}
/*
cout << x << ":\t";
for(int j=1; j<=nh; j++){
cout<< v[h[j]] <<' ';
}
cout << '\n';
*/
}
return 0;
}
void adauga(int p){
h[ ++nh ] = p;
poz[ p ] = nh;
urca( nh );
}
void sterge(int p){
schimba( nh, p );
nh--;
urca( p );
coboara( p );
}
void urca(int p){
while( p>1 && v[h[p]] < v[h[p/2]] ){
schimba( p, p/2 );
p/=2;
}
}
void coboara(int p){
int fs = 2*p, fd = 2*p+1, bun = p;
if( fs <= nh && v[h[fs]] < v[h[bun]] ){
bun = fs;
}
if( fd <= nh && v[h[fd]] < v[h[bun]] ){
bun = fs;
}
if( bun!=p ){
schimba( bun, p );
coboara( bun );
}
}
void schimba(int p1, int p2){
int aux = h[p1];
h[p1] = h[p2];
h[p2] = aux;
poz[h[p1]] = p1;
poz[h[p2]] = p2;
}