P3865 【模板】ST 表
利用倍增
f[i][j]表示,范围[i,i+2^(j)-1]内的最大值是多少
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int f[N][22];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> f[i][0];
for (int j = 1; j <= 20; j++) {
for (int i = 1; i+(1<<j)-1 <= n; i++) {
f[i][j] = max(f[i][j - 1], f[i + (1 << (j-1))][j - 1]);//注意<<的优先级
}
}
while (m--) {
int l, r;
cin >> l >> r;
int k = log2(r - l + 1);
cout << max(f[l][k], f[r - (1 << k) + 1][k])<<'\n';
}
return 0;
}