ST 表学习笔记与总结
ST 表
定义/作用
什么是 可重复贡献问题
ST 算法流程
模板提
我的代码
#include <iostream>
using namespace std;
const int N = 1e5 + 10, logN = 21;
int n, m;
int f[N][logN], logn[N];
int main() {
scanf("%d%d", &n, &m);
logn[0] = -1;
for (int i = 1; i <= n; i ++ ) scanf("%d", &f[i][0]), logn[i] = logn[i / 2] + 1;
for (int j = 1; j <= logN; 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]);
int l, r;
while (m -- ) {
scanf("%d%d", &l, &r);
int s = logn[r - l + 1];
printf("%d\n", max(f[l][s], f[r - (1 << s) + 1][s]));
}
}