思路:区间RMQ,本质是动态规划
代码语言:javascript复制#include<bits/stdc .h>
using namespace std;
const int N=2e5 10,M=20;
int n,m,a[N],f[N][M];
void init(){
for(int j=0;j<M;j ){
for(int i=1;i (1<<j)-1<=n;i ){
if(!j)f[i][j]=a[i];
else f[i][j]=max(f[i (1<<j-1)][j-1],f[i][j-1]);
}
}
}
int query(int x,int y){
int len=y-x 1,k=log(len)/log(2);
return max(f[x][k],f[y-(1<<k) 1][k]);
}
int main(){
cin>>n;
for(int i=1;i<=n;i )cin>>a[i];
init();
cin>>m;
while(m--){
int x,y;
cin>>x>>y;
cout<<query(x,y)<<endl;
}
}