Pagini recente » Cod sursa (job #1647142) | runda_ezoterica_1 | Cod sursa (job #2641355) | Cod sursa (job #794109) | Cod sursa (job #1094250)
#include <cstdio>
using namespace std;
const int NMAX = 200005;
int N, count, H, v[NMAX], heap[NMAX], pos[NMAX];
void swap_index( int a, int b ) {
int aux = heap[a];
heap[a] = heap[b];
heap[b] = aux;
pos[heap[a]] = a;
pos[heap[b]] = b;
}
void ins(int value) {
v[++count] = value;
heap[++H] = count;
pos[count] = H;
int index = pos[count];
while(index > 1 && v[heap[index >> 1]] > v[heap[index]]) {
swap_index(index, index >> 1);
index >>= 1;
}
}
void rem(int index) {
int save = pos[index];
swap_index(H--, save);
while( true ) {
int aux = save;
if(2 * save <= H && v[heap[2 * save]] < v[heap[aux]])
aux = 2 * save;
if(2 * save < H && v[heap[2*save+1]] < v[heap[aux]])
aux = 2 * save + 1;
if( aux != save ){
swap_index(aux, save);
aux = save;
} else
break;
}
}
void read() {
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
scanf("%i", &N);
H = 0;
int type, value;
for( int i = 1; i <= N; i++ ){
scanf("%i", &type);
switch(type){
case 1: scanf("%i", &value); ins(value); break;
case 2: scanf("%i", &value); rem(value); break;
case 3: printf("%i\n", v[heap[1]]); break;
}
}
}
int main() {
read();
return 0;
}