70TLE求条
查看原帖
70TLE求条
652856
MaxPAN楼主2024/8/10 11:36
#include <iostream>
#include <algorithm>
using namespace std;

int n,q,l,r,a[100005],st[100005][22],lg[100005];

inline void init(){
	lg[1] = 0;
    for(int i = 2;i <= n; i++) lg[i] = lg[i>>1] + 1;
// 	for(int i = 1;i <= n; i++) st[i][0] = a[i];
	for(int j = 1;j <= 17; j++)
		for(int i = 1;i <= n; i++){
			st[i][j] = st[i][j-1];
			if(i + (1 << j - 1) <= n) st[i][j] = max(st[i][j],st[i+(1<<j-1)][j-1]);
		}
}

int rmq(int l,int r){
	int k = 0;
	k = lg[r-l+1];
//	cout << k << " " << l << " " << r << " " << st[l][k] << " " << st[r-(1<<k)+1][k] << endl;
	return max(st[l][k],st[r-(1<<k)+1][k]);
}

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;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	n = read();
	q = read();
	for(int i = 1;i <= n; i++) st[i][0] = read();
	init();
//	for(int i = 1;i <= n; i++){
//		for(int j = 0;j <= 3; j++) cout << st[i][j] << " ";
//		cout << endl;
//	}
	while(q--){
		l = read(),r = read();
		cout << rmq(l,r) << endl;
	}
	
	return 0;
}

70TLE

2024/8/10 11:36
加载中...