#include<cstdio>
#include<algorithm>
#define MAXN 1000005
using namespace std;
//换底公式 loga(b) = logc(b) / logc(a)
int F[20][MAXN];
int log[MAXN];
int N,M;
int main(){
log[0]=0;
log[1]=0;
scanf("%d %d",&N,&M);
for(register int i=2;i<=N;++i)log[i]=log[i>>1]+1;
for(register int i=1;i<=N;++i){
scanf("%d",&F[0][i]);
}
for(register int j=1;j<=17;++j)
for(register int i=1;i<=N;++i)
F[j][i] = max(F[j-1][i],F[j-1][i+(1<<(j-1))]);
int l,r;
for(register int i=1;i<=M;++i){
scanf("%d %d",&l,&r);
int k = log[r-l+1];
printf("%d\n",max(F[k][l],F[k][r-(1<<k)+1]));
}
return 0;
}
原文地址:https://www.cnblogs.com/Neworld2002/p/8462059.html
时间: 2024-10-09 17:41:07