http://poj.org/problem?id=3264
题意 rmq max min之差
模板:
#define _CRT_SECURE_NO_WARNINGS
#include<cmath>
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
const int maxn = 5e5 + ;
int cow[maxn], minc[maxn][], maxc[maxn][];
int n, q; void STinit() {
int LOG;
rep(i, , n)minc[i][] = maxc[i][] = cow[i];
LOG = int(log(1.0*n) / log(1.0 * ));
rep(i,,LOG)
per(j, n, ) {
maxc[j][i] = maxc[j][i - ];
if (j + ( << (i - )) <= n)
maxc[j][i] = max(maxc[j][i], maxc[j + ( << (i - ))][i - ]);
minc[j][i] = minc[j][i - ];
if (j + ( << (i - )) <= n)
minc[j][i] = min(minc[j][i], minc[j + ( << (i - ))][i - ]);
}
}
int RMQmax(int l,int r) {
int n = r - l + ;
int LOG = int(log(1.0*n) / log(1.0 * ));
return max(maxc[l][LOG], maxc[r -( << LOG)+][LOG]);
}
int RMQmin(int l, int r) {
int n = r - l + ;
int LOG = int(log(1.0*n) / log(1.0 * ));
return min(minc[l][LOG], minc[r -( << LOG)+][LOG]);
}
int main() {
cin >> n>>q; rep(i, , n)scanf("%d", &cow[i]);
STinit();
rep(i, , q) {
int l, r;
scanf("%d%d", &l, &r); printf("%d\n", RMQmax(l, r) - RMQmin(l, r));
}
cin >> n;
return ;
}