Pagini recente » Cod sursa (job #2587307) | Cod sursa (job #2917018) | Cod sursa (job #2901012) | Cod sursa (job #2841305) | Cod sursa (job #3139070)
#include <fstream>
#include <array>
#include <vector>
using Array = std::array<int, 200011>;
Array heap, values, position;
int father(int node) { return node >> 1; }
int leftSon(int node) { return node << 1; }
int rightSon(int node) { return leftSon(node) | 1; }
void push(int node) {
int fatherNode = father(node);
while (node > 1 && values[heap[node]] < values[heap[fatherNode]]) {
std::swap(heap[node], heap[fatherNode]);
position[heap[node]] = node;
position[heap[fatherNode]] = fatherNode;
node = fatherNode;
fatherNode = father(node);
}
}
void pop(int node, const int heapSize) {
while (leftSon(node) <= heapSize) {
int chosen = values[heap[leftSon(node)]] <= values[heap[rightSon(node)]] ? leftSon(node) : rightSon(node);
std::swap(heap[node], heap[chosen]);
position[heap[node]] = node;
position[heap[chosen]] = chosen;
node = chosen;
}
}
int main() {
std::ifstream in{"heapuri.in"};
std::ofstream out{"heapuri.out"};
int N;
int HeapSize = 0;
int NumElements = 0;
in >> N;
for (int i = 0; i < N; ++i) {
int op;
in >> op;
if (op == 3) {
out << values[heap[1]] << '\n';
} else {
int value;
in >> value;
if (op == 1) {
++HeapSize, ++NumElements;
values[NumElements] = value;
heap[HeapSize] = NumElements ;
position[NumElements ] = HeapSize ;
push(HeapSize);
} else {
values[value] = -1;
push(position[value]);
heap[1] = heap[HeapSize--];
position[heap[1]] = 1;
if (HeapSize > 1) {
pop(1, HeapSize);
}
}
}
}
return 0;
}