Pagini recente » Cod sursa (job #611969) | Cod sursa (job #2376013) | Cod sursa (job #219555) | Cod sursa (job #1120034) | Cod sursa (job #3139104)
#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];
void move_up(int index) {
while (index > 0 && heap[index].first < heap[index/2].first) {
swap(position[heap[index].second], position[heap[index / 2].second]);
swap(heap[index], heap[index / 2]);
index /= 2;
}
}
void move_down(int index) {
pair<int,int> aux_pair;
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 ) {
swap(position[heap[index].second], position[ heap[index * 2 + 1].second]);
swap(heap[index], heap[index * 2 + 1]);
index = index * 2 + 1;
}
else if (index * 2 < heap_size
&& heap[index].first > heap[index * 2].first ) {
swap(position[heap[index].second], position[heap[index * 2].second]);
swap(heap[index], heap[index * 2]);
index = index * 2;
}
else {
break;
}
}
}
void insert_heap(int value) {
entered ++;
heap[heap_size] = {value, entered};
int index = heap_size;
heap_size ++;
position[entered] = index;
move_up(index);
}
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;
}
}
fin.close();
fout.close();
return 0;
}