CSES - Shared codeLink to this code:
https://cses.fi/paste/e2ba5886552dabab38838a/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using ll = long long;
int const nmax = 200000;
int const lgmax = 20;
int v[5 + nmax];
int rmq[1 + lgmax][5 + nmax];
int lg(int number) {
return 31 - __builtin_clz(number);
}
void precompute(int n) {
for(int i = 1;i <= n; i++)
rmq[0][i] = v[i];
for(int h = 1; h <= lgmax; h++)
for(int i = 1;i <= n - (1 << h) + 1; i++)
rmq[h][i] = std::min(rmq[h - 1][i], rmq[h - 1][i + (1 << (h - 1))]);
}
int extract(int x, int y) {
int h = lg(y - x + 1);
return std::min(rmq[h][x], rmq[h][y - (1 << h) + 1]);
}
int main() {
int n, q;
std::cin >> n >> q;
for(int i = 1;i <= n; i++)
std::cin >> v[i];
precompute(n);
for(int i = 1; i <= q; i++) {
int x, y;
std::cin >> x >> y;
std::cout << extract(x, y) << '\n';
}
return 0;
}