社区讨论

7分求调(帮助必须关)

P3865【模板】ST 表 & RMQ 问题参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mhjh4jt2
此快照首次捕获于
2025/11/04 02:28
4 个月前
此快照最后确认于
2025/11/04 02:28
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define ll long long
const int N=1e6+5;
int n,q,x,y;
int st[N][25];
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
int main(){
	n=read();
	q=read();
	for(int i=1;i<=n;i++){
		st[i][0]=read();
	}
	for(int j=1;j<=20;j++){
		for(int i=1;i<=n-(i<<1)+1;i++){
			int x=i+(1<<(j-1));
			st[i][j]=max(st[i][j-1],st[x][j-1]);
		}
	}
	while(q--){
		x=read();
		y=read();
		int j=log2(y-x+1);
		cout<<max(st[x][j],st[y-(1<<j)+1][j])<<"\n";
	}
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...