| Task: | Dynamic Range Minimum Queries |
| Sender: | aalto26dh_037 |
| Submission time: | 2026-09-20 20:26:08 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.48 s | details |
Code
#include <iostream>
#include <vector>
#include <ranges>
#include <math.h>
using namespace std;
int parent(int node) {
return node / 2;
}
int left(int node) {
return 2 * node;
}
int right(int node) {
return 2 * node + 1;
}
int getMin(int a, int b, vector<int>& arr) {
int n = arr.size() / 2;
a += n; b += n;
int minVal = 1e9+1;
while (a <= b) {
if (a%2 == 1) {
minVal = min(minVal, arr[a]);
a++;
}
if (b%2 == 0) {
minVal = min(minVal, arr[b]);
b--;
}
a = parent(a);
b = parent(b);
}
return minVal;
}
void setVal(int ind, int val, vector<int>& arr) {
int n = arr.size() / 2;
ind += n;
arr[ind] = val;
for (ind /= 2; ind >= 1; ind /= 2) {
arr[ind] = min(arr[left(ind)], arr[right(ind)]);
}
}
int main() {
int n, q;
cin >> n >> q;
int elems = 1 << (int)ceil(log2(n));
vector<int> tree(2*elems);
for (int i=0; i<n; i++) {
cin >> tree[i + elems];
}
for (int i=n; i<elems; i++) {
tree[i + elems] = 1e9+1;
}
for (int i=elems-1; i>0; i--) {
tree[i] = min(tree[left(i)], tree[right(i)]);
}
int op, i1, i2;
for (int i=0; i<q; i++) {
cin >> op >> i1 >> i2;
if (op == 1) {
setVal(i1-1, i2, tree);
}
else if (op == 2) {
cout << getMin(i1-1, i2-1, tree) << endl;
}
}
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 ... |
