TLE玄关(标准 ST表)
查看原帖
TLE玄关(标准 ST表)
1373205
dg114514楼主2024/12/30 16:24

rt

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[100005];
class SpareTable{
	private:
		int st[100010][25];
		int lg[100010];
	public:
		void build(int* a,int n){
			st[1][0]=a[1];
			for(int i=2;i<=n;i++)
				st[i][0]=a[i],lg[i]=lg[i>>1]+1;
			for(int j=1;j<=lg[n];j++)
				for(int i=1;i+(1<<j)-1<=n;i++)
					st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
		}
		inline int query(int l,int r){
			int k=lg[r-l+1];
			return max(st[l][k],st[r-(1<<k)+1][k]);
		}
}st;
signed main(){
	ios::sync_with_stdio(false);
	int n,q,x,op,y;
	cin>>n>>q;
	for(int i=1;i<=n;i++)cin>>a[i];
	st.build(a,n);
	while(q--) cin>>x>>y,cout<<st.query(x,y)<<"\n";
}
2024/12/30 16:24
加载中...