标签:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxa = 50005;
int rmp_max[maxa][100];
int rmp_min[maxa][100];
int log(int n){
int cnt = 0;
while(n){
cnt ++;
n /= 2;
}
return cnt - 1;
}
int main(){
int n, m;
while(scanf("%d%d", &n ,&m)!=EOF){
for(int i = 0;i < n; i++){
scanf("%d", &rmp_max[i][0]);
rmp_min[i][0] = rmp_max[i][0];
}
int l = log(n);
for(int i = 1; i <= l; i++){
for(int j = 0; j+(1<<(i-1)) < n; j++){
rmp_max[j][i] = max(rmp_max[j][i-1], rmp_max[j+(1<<(i-1))][i-1]);
rmp_min[j][i] = min(rmp_min[j][i-1], rmp_min[j+(1<<(i-1))][i-1]);
}
}
while(m--){
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
int j = log(b-a+1);
int maxn = max(rmp_max[a][j], rmp_max[b-(1<<j)+1][j]);
int minn = min(rmp_min[a][j], rmp_min[b-(1<<j)+1][j]);
printf("%d\n", maxn-minn);
}
}
}
标签:
原文地址:http://www.cnblogs.com/icodefive/p/4318661.html