Pagini recente » Cod sursa (job #1942958) | Cod sursa (job #2805319) | Cod sursa (job #2571098) | Cod sursa (job #2922234) | Cod sursa (job #1259295)
#include<fstream>
using namespace std;
ifstream fin( "heapuri.in" );
ofstream fout( "heapuri.out" );
const int nmax = 200000;
const int inf = 1 << 30;
int w, hw, v[ nmax + 10 ], p[ nmax + 10 ], h[ 2 * nmax ];
void insert_elem() {
int aux, poz;
h[ hw ++ ] = w;
p[ w ] = hw - 1;
poz = hw - 1;
while ( 1 < poz && v[ h[ poz / 2 ] ] > v[ h[ poz ] ] ) {
p[ h[ poz / 2 ] ] = poz;
p[ h[ poz ] ] = poz / 2;
aux = h[ poz / 2 ];
h[ poz / 2 ] = h[ poz ];
h[ poz ] = aux;
poz /= 2;
}
}
void erase_elem( int x ) {
int aux, poz = p[ x ];
v[ x ] = inf;
while ( 2 * poz <= hw ) {
if ( 2 * poz + 1 >= hw || v[ h[ 2 * poz ] ] < v[ h[ 2 * poz + 1 ] ] ) {
p[ h[ 2 * poz ] ] = poz;
p[ h[ poz ] ] = 2 * poz;
aux = h[ poz ];
h[ poz ] = h[ 2 * poz ];
h[ 2 * poz ] = aux;
poz = 2 * poz;
} else {
p[ h[ 2 * poz + 1 ] ] = poz;
p[ h[ poz ] ] = 2 * poz + 1;
aux = h[ poz ];
h[ poz ] = h[ 2 * poz + 1 ];
h[ 2 * poz + 1 ] = aux;
poz = 2 * poz + 1;
}
}
}
int main() {
int n, t, x;
fin >> n;
w = hw = 1;
for( int i = 0; i < n; ++ i ) {
fin >> t;
if ( t == 1 ) {
fin >> v[ w ];
insert_elem();
++ w;
} else if ( t == 2 ) {
fin >> x;
erase_elem( x );
} else {
fout << v[ h[ 1 ] ] << "\n";
}
}
fin.close();
fout.close();
return 0;
}