CSES - Shared codeLink to this code:
https://cses.fi/paste/4aac15235eb070655cc98b/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// #define int cpp_int
#define int long long
#define ull unsigned long long
#define EPS 1e-9
#define test_case \
int t; \
cin >> t; \
while (t--)
#define print_case cout << "Case " << ++cas << ": "
#define mod (int) 1e9+7
const int N = 1e5+7;
const int INF = INT64_MAX;
using namespace std;
int a[4*N], sol[4*N];
void make_seg_tree(int node, int b, int e) {
if(b==e) {
a[node] = sol[b]; return;
}
int mid = (b + e) >> 1, left = node * 2, right = (node * 2) + 1;
make_seg_tree(left, b, mid);
make_seg_tree(right, mid+1, e);
a[node] = min(a[left], a[right]);
}
void upd(int node, int b, int e, int ind, int value) {
if(ind > e or ind < b) return;
if(b == e and b == ind) {
a [node] = value;
return;
}
int l = node * 2, r =2 * node + 1;
int mid = (b + e) >> 1;
upd(l, b, mid, ind, value);
upd(r, mid+1, e, ind, value);
a[node] = min(a[l], a[r]);
}
int gen_output(int node, int b, int e, int i, int j) {
if(i>e or j<b) return INF;
if(b>=i and e<=j) return a[node];
int left = node * 2, right = (node * 2) + 1;
int mid = b + e >> 1;
int l = gen_output(left, b, mid, i, j);
int r = gen_output (right, mid+1, e, i, j);
return min(l, r);
}
void solve() {
int n, m; cin >> n >> m;
for(int i=1; i<=n; i++) cin >> sol[i];
make_seg_tree(1, 1, n);
while(m--) {
int i, j; cin >> i >> j;
cout << gen_output(1, 1, n, i, j)<< '\n';
}
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// test_case
solve(),
cout<<'\n';
return 0;
}