Problem I: 静态RMQ
📝 题目描述
静态 RMQ (Range Minimum Query) 【区间最值查询】
🔑 算法模块
ST表/可持久化线段树
💡 思路分析
🖥️ 代码实现
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 | #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#define endl "\n"
using namespace std;
class ST {
public:
ST(vector<int>& arr) : n(arr.size()), k(log2(n) + 1),
st(n, vector<int>(k)), lg2(n + 1) {
for (int i = 2; i <= n; i++) lg2[i] = lg2[i / 2] + 1;
for (int i = 0; i < n; i++) st[i][0] = arr[i];
for (int j = 1; j < k; 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]);
}
}
}
int query(int l, int r) {
int k = lg2[r - l + 1];
return min(st[l][k], st[r - (1 << k) + 1][k]);
}
private:
int n, k;
vector<vector<int>> st;
vector<int> lg2;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q, a, b;
cin >> n >> q;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
ST st(arr);
while (q--) {
cin >> a >> b;
cout << st.query(a - 1, b - 1) << endl;
}
return 0;
}
|
⏱️ 复杂度分析
📚 拓展