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 + 10; int cow[maxn], minc[maxn][20], maxc[maxn][20]; int n, q; void STinit() { int LOG; rep(i, 1, n)minc[i][0] = maxc[i][0] = cow[i]; LOG = int(log(1.0*n) / log(1.0 * 2)); rep(i,1,LOG) per(j, n, 1) { maxc[j][i] = maxc[j][i - 1]; if (j + (1 << (i - 1)) <= n) maxc[j][i] = max(maxc[j][i], maxc[j + (1 << (i - 1))][i - 1]); minc[j][i] = minc[j][i - 1]; if (j + (1 << (i - 1)) <= n) minc[j][i] = min(minc[j][i], minc[j + (1 << (i - 1))][i - 1]); } } int RMQmax(int l,int r) { int n = r - l + 1; int LOG = int(log(1.0*n) / log(1.0 * 2)); return max(maxc[l][LOG], maxc[r -( 1 << LOG)+1][LOG]); } int RMQmin(int l, int r) { int n = r - l + 1; int LOG = int(log(1.0*n) / log(1.0 * 2)); return min(minc[l][LOG], minc[r -( 1 << LOG)+1][LOG]); } int main() { cin >> n>>q; rep(i, 1, n)scanf("%d", &cow[i]); STinit(); rep(i, 1, q) { int l, r; scanf("%d%d", &l, &r); printf("%d ", RMQmax(l, r) - RMQmin(l, r)); } cin >> n; return 0; }