POJ3264 Balanced Lineup

Description

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers,  N and  Q
Lines 2.. N+1: Line  i+1 contains a single integer that is the height of cow  i 
Lines  N+2.. N+ Q+1: Two integers  A and  B (1 ≤  A ≤  B ≤  N), representing the range of cows from  A to  B inclusive.

Output

Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

【题解】
大意是给定Q个数A1,A2,...,AQ,多次求任意区间[i,j]中最大数和最小数的差。用线段树,每个节点记录该节点表示的区间内的最大和最小值。

【代码】
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int INF = 0x7fffffff;
 6 
 7 struct Node
 8 {
 9     int L, R;
10     int minV, maxV;
11     Node() {
12         L = R = -1;
13         minV = INF;
14         maxV = -INF;
15     }
16     int Mid(){
17         return (L + R) >> 1;
18     }
19 }Tree[200010];
20 
21 int minV = INF;
22 int maxV = -INF;
23 
24 void Build(int root, int l, int r) 
25 {
26     int mid;
27 
28     Tree[root].L = l;
29     Tree[root].R = r;
30 
31     if (l != r) {
32         mid = Tree[root].Mid();
33         Build(2 * root + 1, l, mid);
34         Build(2 * root + 2, mid + 1, r);
35     }
36 }
37 
38 void Insert(int root, int no, int height)
39 {
40     if (Tree[root].L == Tree[root].R) {
41         Tree[root].maxV = Tree[root].minV = height;
42         return;
43     }
44 
45     Tree[root].minV = min(height, Tree[root].minV);
46     Tree[root].maxV = max(height, Tree[root].maxV);
47 
48     if (no <= Tree[root].Mid()) Insert(2 * root + 1, no, height);
49     else Insert(2 * root + 2, no, height);
50 }
51 
52 void Query(int root, int l, int r)
53 {
54     int mid = Tree[root].Mid();
55 
56     if (Tree[root].maxV <= maxV && Tree[root].minV >= minV)
57         return;
58     if (Tree[root].L == l && Tree[root].R == r) {
59         minV = min(minV, Tree[root].minV);
60         maxV = max(maxV, Tree[root].maxV);
61         return;
62     }
63     if (r <= mid) Query(2 * root + 1, l, r);
64     else if (l > mid) Query(2 * root + 2, l, r);
65     else {
66         Query(2 * root + 1, l, mid);
67         Query(2 * root + 2, mid + 1, r);
68     }
69 }
70 
71 int main()
72 {
73     int N, Q, h, A, B;
74 
75     cin >> N >> Q;
76     Build(0, 1, N);
77 
78     for (int i = 1; i <= N; i++) {
79         cin >> h;
80         Insert(0, i, h);
81     }
82 
83     for (int i = 0; i < Q; i++) {
84         cin >> A >> B;
85         minV = INF;
86         maxV = -INF;
87         Query(0, A, B);
88         printf("%d\n", maxV - minV);
89     }
90 
91     //system("pause");
92     return 0;
93 }

猜你喜欢

转载自www.cnblogs.com/Jeffrey-Y/p/10224706.html