Pagini recente » Cod sursa (job #1631011) | Cod sursa (job #1928549) | Cod sursa (job #946394) | Cod sursa (job #2985562) | Cod sursa (job #3139105)
#include <iostream>
#include <fstream>
using namespace std;
int heap_size = 0;
int entered = 0;
pair<int,int> heap[200002]; //<value, entered pos>
int position[200002];
int swapping(int index, int new_index) {
swap(position[heap[index].second], position[heap[new_index].second]);
swap(heap[index], heap[new_index]);
return new_index;
}
void move_up(int index) {
while (index > 0 && heap[index].first < heap[index/2].first) {
index = swapping(index, index / 2);
}
}
void move_down(int index) {
while (true) {
if (index * 2 + 1 < heap_size
&& heap[index].first > heap[index * 2 + 1].first
&& heap[index * 2 + 1].first <= heap[2 * index].first ) {
index = swapping(index, index * 2 + 1);
continue;
}
if (index * 2 < heap_size
&& heap[index].first > heap[index * 2].first ) {
index = swapping(index, index * 2);
continue;
}
break;
}
}
void insert_heap(int value) {
entered ++;
heap_size ++;
heap[heap_size - 1] = {value, entered};
position[entered] = heap_size - 1;
move_up(heap_size - 1);
}
void remove_heap(int item) {
heap_size --;
int index = position[item];
heap[index] = heap[heap_size];
position[heap[index].second] = index;
move_up(index);
move_down(index);
}
int main() {
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n, op, arg;
fin >> n;
for (int i = 0; i < n; i++) {
fin >> op;
if (op == 1) {
fin >> arg;
insert_heap(arg);
}
else if (op == 2) {
fin >> arg;
remove_heap(arg);
}
else {
fout << heap[0].first << endl;
}
}
return 0;
}