| Task: | Dynamic Range Minimum Queries |
| Sender: | aalto26dh_017 |
| Submission time: | 2026-09-18 16:01:55 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.26 s | details |
Code
#include <bits/stdc++.h>
template <typename T, typename Combine>
class SegmentTree {
public:
SegmentTree(
const std::vector<T>& values, T identity, Combine combineOp) : n(values.size()), identity(std::move(identity)), combine(std::move(combineOp)), tree(2 * n, this->identity) {
if (n == 0) {
throw std::invalid_argument("Tree must contain values");
}
for (std::size_t i = 0; i < n; i++) {
tree[n + i] = values[i];
}
for (std::size_t i = n; i-- > 1;) {
tree[i] = combine(tree[2*i], tree[2*i+1]);
}
}
void update(std::size_t pos, const T& value) {
if (pos >= n) {
throw std::out_of_range("Invalid position");
}
pos += n;
tree[pos] = value;
while (pos > 1) {
pos /= 2;
tree[pos] = combine(tree[2 * pos], tree[2 * pos + 1]);
}
}
T query(std::size_t left, std::size_t right) const {
if (left > right || right >= n) {
throw std::out_of_range("Invalid range");
}
left += n;
right += n;
T resultLeft = identity;
T resultRight = identity;
while (left <= right) {
if (left %2 == 1) {
resultLeft = combine(resultLeft, tree[left++]);
}
if (right %2 == 0) {
resultRight = combine(tree[right--], resultRight);
}
left /= 2;
right /= 2;
}
return combine(resultLeft, resultRight);
}
private:
std::size_t n;
T identity;
Combine combine;
std::vector<T> tree;
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::vector<long> values (n);
for (long& value : values) {
std::cin >> value;
}
SegmentTree tree(values, std::numeric_limits<long>::max(), [](const long a, const long b){return std::min(a, b);});
for (int i = 0; i < q; i++) {
int type;
std::cin >> type;
if (type == 1) {
int k;
long u;
std::cin >> k >> u;
tree.update(k - 1, u);
} else if (type == 2) {
int a, b;
std::cin >> a >> b;
std::cout << tree.query(a - 1, b - 1) << std::endl;
} else {
throw std::invalid_argument("Unsupported operation");
}
}
return 0;
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 8 80 7 6 4 6 2 9 4 8 2 1 1 2 1 2 2 1 3 ... |
| correct output |
|---|
| 7 6 4 4 2 ... |
| user output |
|---|
| 7 6 4 4 2 ... |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 200000 200000 398739055 65343131 699208332 3... |
| correct output |
|---|
| 28609 129890 20378 20378 311522 ... |
| user output |
|---|
| 28609 129890 20378 20378 311522 ... |
