-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1647.cpp
More file actions
30 lines (28 loc) · 783 Bytes
/
1647.cpp
File metadata and controls
30 lines (28 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n, q;
cin >> n >> q;
vector<int> x(n);
for (int i = 0; i < n; i++)
cin >> x[i];
// sparse table for range minimum query
vector<vector<int>> st(n, vector<int>(20));
for (int i = 0; i < n; i++)
st[i][0] = x[i];
for (int j = 1; j < 20; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
while (q--)
{
int a, b;
cin >> a >> b;
a--, b--;
int k = 31 - __builtin_clz(b - a + 1);
// built-in function to find the position of the most significant bit
cout << min(st[a][k], st[b - (1 << k) + 1][k]) << "\n";
}
}