Pagini recente » Cod sursa (job #418172) | Cod sursa (job #2591588) | Cod sursa (job #2970337) | Cod sursa (job #1516088) | Cod sursa (job #954379)
Cod sursa(job #954379)
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;
template<class Type>
class Heap {
public:
Heap() {
size = index = 0;
heap.push_back(make_pair(Type(), -1));
}
Type Top() const {
return heap[1].first;
}
bool Empty() const {
return (size == 0);
}
int Size() const {
return size;
}
void Insert(const Type &object) {
heap.push_back(make_pair(object, index));
++size;
position.push_back(size);
++index;
Percolate(size);
}
void Erase(const int objectIndex) {
int x = position[objectIndex];
Swap(size, x);
heap.pop_back();
--size;
Sift(x);
}
private:
int size, index;
vector<pair<Type, int>> heap;
vector<int> position;
void Swap(const int x, const int y) {
swap(heap[x], heap[y]);
swap(position[heap[x].second], position[heap[y].second]);
}
void Percolate(const int x) {
int father = x / 2;
if (father > 0 && heap[x] < heap[father]) {
Swap(x, father);
Percolate(father);
}
}
void Sift(const int x) {
int son = 2 * x;
if (son + 1 <= size && heap[son + 1] < heap[son])
++son;
if (son <= size && heap[son] < heap[x]) {
Swap(x, son);
Sift(son);
}
}
};
int main() {
assert(freopen("heapuri.in", "r", stdin));
assert(freopen("heapuri.out", "w", stdout));
int NQ; assert(scanf("%d", &NQ) == 1);
Heap<int> heap;
for (; NQ > 0; --NQ) {
int type; assert(scanf("%d", &type) == 1);
if (type == 1) {
int value; assert(scanf("%d", &value) == 1);
heap.Insert(value);
}
if (type == 2) {
int index; assert(scanf("%d", &index) == 1);
heap.Erase(index - 1);
}
if (type == 3)
printf("%d\n", heap.Top());
}
return 0;
}