You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1009 B
42 lines
1009 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
int n, m;
|
|
|
|
const int N = 200010, M = 18;
|
|
int w[N];
|
|
int f[N][M];
|
|
// 预处理
|
|
void rmq() {
|
|
for (int j = 0; j < M; j++) // 注意是j在外层,类似于区间DP,长度在外层
|
|
for (int i = 1; i + (1 << j) - 1 <= n; i++) // i(行)在内层
|
|
if (j == 0) // base case 边界值
|
|
f[i][j] = w[i];
|
|
else
|
|
f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
|
|
}
|
|
|
|
// 查询区间最大值
|
|
int query(int l, int r) {
|
|
int len = r - l + 1;
|
|
int k = log2(len);
|
|
return max(f[l][k], f[r - (1 << k) + 1][k]);
|
|
}
|
|
|
|
int main() {
|
|
// 加快读入
|
|
ios::sync_with_stdio(false), cin.tie(0);
|
|
cin >> n;
|
|
for (int i = 1; i <= n; i++) cin >> w[i];
|
|
|
|
rmq(); // ST表初始化
|
|
|
|
cin >> m;
|
|
while (m--) {
|
|
int l, r;
|
|
cin >> l >> r;
|
|
printf("%d\n", query(l, r));
|
|
}
|
|
return 0;
|
|
} |