题目链接:http://poj.org/problem?id=3264
一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值。
线段树维护当前节点下属叶节点的两个最值,查询后作差即可。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 #define lson l, m, rt << 1 23 #define rson m + 1, r, rt << 1 | 1 24 typedef pair<int, int> pii; 25 const int maxn = 50050; 26 int n, q, a, b; 27 pii node[maxn<<2]; 28 29 void pushUP(int rt) { 30 int ll = min(node[rt<<1].first, node[rt<<1|1].first); 31 int rr = max(node[rt<<1].second, node[rt<<1|1].second); 32 node[rt] = pii(ll, rr); 33 } 34 35 void build(int l, int r, int rt) { 36 if(l == r) { 37 scanf("%d", &node[rt].first); 38 node[rt].second = node[rt].first; 39 return; 40 } 41 int m = (l + r) >> 1; 42 build(lson); 43 build(rson); 44 pushUP(rt); 45 } 46 47 int querymax(int L, int R, int l, int r, int rt) { 48 if(L <= l && r <= R) { 49 return node[rt].second; 50 } 51 int m = (l + r) >> 1; 52 int ans = -1; 53 if(L <= m) ans = max(ans, querymax(L, R, lson)); 54 if(m < R) ans = max(ans, querymax(L, R, rson)); 55 return ans; 56 } 57 58 int querymin(int L, int R, int l, int r, int rt) { 59 if(L <= l && r <= R) { 60 return node[rt].first; 61 } 62 int m = (l + r) >> 1; 63 int ans = 0x7f7f7f; 64 if(L <= m) ans = min(ans, querymin(L, R, lson)); 65 if(m < R) ans = min(ans, querymin(L, R, rson)); 66 return ans; 67 } 68 69 int main() { 70 // freopen("in", "r", stdin); 71 while(~scanf("%d %d", &n, &q)) { 72 build(1, n, 1); 73 while(q--) { 74 scanf("%d %d", &a, &b); 75 int ll = querymin(a, b, 1, n, 1); 76 int rr = querymax(a, b, 1, n, 1); 77 printf("%d ", rr - ll); 78 } 79 } 80 return 0; 81 }