Pagini recente » Cod sursa (job #1707169) | Cod sursa (job #329511) | Cod sursa (job #384304) | Cod sursa (job #567057) | Cod sursa (job #1075860)
#include <cstdio>
using namespace std;
const int NMAX = 200005;
int N, H, A;
int heap[NMAX], position[NMAX], array[NMAX];
inline void swap(int a, int b) {
int change = heap[a];
heap[a] = heap[b];
heap[b] = change;
position[heap[a]] = a;
position[heap[b]] = b;
}
inline void up_heapify(int index) {
while( index > 1 && array[heap[index]] < array[heap[index >> 1]] ) {
swap(index, index >> 1);
index >>= 1;
}
}
inline void insert(int elem) {
array[++A] = elem;
heap[++H] = A;
position[A]= H;
up_heapify(H);
}
void down_heapify(int index) {
while( true ) {
int aux = index;
if( (index << 1) <= H && array[heap[index << 1]] < array[heap[aux]] )
aux = index << 1;
if( (index << 1) < H && array[heap[(index << 1) + 1]] < array[heap[aux]])
aux = (index << 1) + 1;
if( aux != index )
swap(aux, index);
else
break;
}
}
inline void remove(int index) {
swap(index, H);
position[heap[H]] = -1;
H--;
down_heapify(index);
}
void read() {
freopen( "heapuri.in", "r", stdin );
freopen( "heapuri.out", "w", stdout );
scanf("%i", &N);
int type, n;
for(int i = 0; i < N; i++) {
scanf("%i", &type);
switch(type) {
case(1): scanf("%i", &n); insert(n); break;
case(2): scanf("%i", &n); remove(position[n]); break;
case(3): printf("%i\n", array[heap[1]]); break;
}
}
}
int main() {
read();
return 0;
}