题意
求区间最大与最小的差
RMQ算法讲解
https://blog.csdn.net/niushuai666/article/details/6624672
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=50010;
int maxsum[maxn][20],minsum[maxn][20];
int n;
void ST()
{
for(int j=1;j<20;j++)
for(int i=1;i<=n;i++)
if(i+(1 << j)-1<=n)
{
maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);
minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]); //1<<j = 1*2^j
}
}
int main()
{
int q;
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%d",&maxsum[i][0]);
minsum[i][0]=maxsum[i][0];
}
ST();
int a,b; //a为起点 b为终点
while(q--)
{
scanf("%d %d",&a,&b);
int k=(int)(log(b-a+1.0)/log(2.0));
int maxs=max(maxsum[a][k],maxsum[b-(1 << k)+1][k]);
int mins=min(minsum[a][k],minsum[b-(1 << k)+1][k]);
printf("%d\n",maxs-mins);
}
return 0;
}