#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5;
int n,q,max_f[N][20],min_f[N][20],a[50000];
void build(){
for(int i=1;i<=n;i++){
max_f[i][0]=a[i];
min_f[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
max_f[i][j]=max(max_f[i][j-1],max_f[i+(1<<j-1)][j-1]);
min_f[i][j]=min(min_f[i][j-1],min_f[i+(1<<j-1)][j-1]);
}
}
}
int query(int L,int R){
int k=log2(R-L+1);
int maxn=max(max_f[L][k],max_f[R-(1<<k)+1][k]);
int minn=min(min_f[L][k],min_f[R-(1<<k)+1][k]);
return maxn-minn;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
}
build();
for(int i=1;i<=q;i++){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r));
}
return 0;
}
平衡的阵容
最新推荐文章于 2024-08-14 11:57:07 发布