#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const ll MAXN=1e18;
const int MOD=1e6;
struct ST{
int el[100050][21],n;
void init(){
for(int i=1;i<=20;++i){
for(int j=1;j+(1<<i)-1<=n;++j){
el[j][i]=max(el[j][i-1],el[j+(1<<(i-1))][i-1]);
}
}
}
int query(int l,int r){
int len=log2(r-l+1);
return max(el[l][len],el[r-(1<<len)+1][len]);
}
}st;
int main(){
IOS;
int m;
cin>>st.n>>m;
for(int i=1;i<=st.n;++i){
cin>>st.el[i][0];
}
st.init();
int r,l;
while(m--){
cin>>l>>r;
cout<<st.query(l,r)<<‘\n‘;
}
return 0;
}
原文:https://www.cnblogs.com/Flowyuan-Foreverworld/p/14643876.html