Pagini recente » Cod sursa (job #2520525) | Rating Daria Culac (daru06) | Cod sursa (job #1592652) | Cod sursa (job #758649) | Cod sursa (job #2840939)
#include <fstream>
using namespace std;
const int N = 400001;
int heap [ N ], v [ N ], pozitie [ N ];
int main( ) {
ifstream fin ( "heapuri.in" );
ofstream fout ( "heapuri.out" );
int n, a, i, k = 1, h = 1, x;
fin >> n;
for ( i = 0; i < n; i++ ){
fin >> a;
if ( a == 1 ){
fin >> v [ k ];
x = h;
heap [ x ] = k;
while ( v [ heap [ x ] ] < v [ heap [ x / 2 ] ] && x > 0 ){
heap [ x ] = heap [ x / 2 ];
heap [ x / 2 ] = k;
pozitie [ heap [ x ] ] = x;
pozitie [ heap [ x / 2 ] ] = x / 2;
x = x / 2;
}
k++;
h++;
}
else if ( a == 2 ){
fin >> x;
a = pozitie [ x ];
heap [ a ] = heap [ h - 1 ];
pozitie [ heap [ a ] ] = a;
h--;
while ( 2 * a < h ){
if ( heap [ 2 * a ] < heap [ 2 * a + 1 ] && x > heap [ 2 * a ] ){
x = heap [ a ];
heap [ a ] = heap [ 2 * a ];
heap [ 2 * a ] = x;
pozitie [ heap [ a ] ] = a;
pozitie [ heap [ 2 * a ] ] = 2 * a;
}
else if ( heap [ 2 * a ] > heap [ 2 * a + 1 ] && x > heap [ 2 * a + 1 ] ){
x = heap [ a ];
heap [ a ] = heap [ 2 * a + 1 ];
heap [ 2 * a + 1 ] = x;
pozitie [ heap [ a ] ] = a;
pozitie [ heap [ 2 * a + 1 ] ] = 2 * a + 1;
a++;
}
a = a * 2;
}
}
else
fout << v [ heap [ 1 ] ] << "\n";
}
return 0;
}