ST 表学习笔记与总结

ST 表学习笔记与总结

ST 表

定义/作用

img

什么是 可重复贡献问题

img

ST 算法流程

img
img

模板提

luogu P3865 【模板】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]));
	}
}
posted @ 2023-07-12 22:15  Mingrui_Yang  阅读(13)  评论(0编辑  收藏  举报