跳到正文

算法·理论

【模板】ST 表 & RMQ 问题

Chenhaorui88约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年4月11日
更新
2026年4月11日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
int f[100086][20],lg[100001];
int main(){
	cin.tie(0)->sync_with_stdio(0);
	int n,m;
	cin>>n>>m;
	for(int i = 1;i<=n;i++)
		cin>>f[i][0];
	for (int i=2;i<=n;i++)
		lg[i]=lg[i/2]+1;
	for(int j = 1;j<=lg[n];j++)
		for(int i = 1;i<=n-(1<<j)+1;i++)
			f[i][j] = max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
	for(int i = 1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		int l = lg[y-x+1];
		printf("%d\n",max(f[x][l],f[y-(1<<l)+1][l]));
	}

}
原文