Pagini recente » Cod sursa (job #73790) | Cod sursa (job #335692) | Cod sursa (job #572124) | Cod sursa (job #1714659) | Cod sursa (job #2792291)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct Heap {
vector<int> h{0}, entry{0}, pos{0};
void swapNodes(int u, int v) {
swap(h[u], h[v]);
swap(pos[entry[u]], pos[entry[v]]);
swap(entry[u], entry[v]);
}
void Insert(int x) {
entry.emplace_back(pos.size());
pos.emplace_back(h.size());
h.emplace_back(x);
int v = h.size() - 1;
while (1 < v && h[v] < h[v >> 1]) {
swapNodes(v, v >> 1);
v >>= 1;
}
}
void Erase(int k) {
int v = pos[k];
swapNodes(v, h.size() - 1);
h.pop_back();
entry.pop_back();
while ((v << 1) < (int)h.size()) {
int son = v << 1;
if ((son | 1) < (int)h.size() && h[son | 1] < h[son]) {
son |= 1;
}
if (h[v] <= h[son]) {
break;
}
swapNodes(v, son);
v = son;
}
}
void EraseMin() {
}
int getMin() {
return h[1];
}
};
void TestCase() {
int n;
fin >> n;
Heap h;
for (int i = 1; i <= n; ++i) {
int op;
fin >> op;
if (op <= 2) {
int k;
fin >> k;
if (op == 1) {
h.Insert(k);
} else {
h.Erase(k);
}
} else {
fout << h.getMin() << '\n';
}
}
}
int main() {
int tests = 1;
for (int tc = 1; tc <= tests; ++tc) {
TestCase();
}
fin.close();
fout.close();
return 0;
}