Balanced Lineup(最简单的线段树题目)

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 33389   Accepted: 15665
Case Time Limit: 2000MS

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <sstream>
16 using namespace std;
17 typedef long long LL;
18 const int INF=0x5fffffff;
19 const double EXP=1e-6;
20 const int MS=50005;
21 
22 int minv,maxv;
23 struct node
24 {
25       int l,r,maxv,minv;
26       int mid()
27       {
28             return (l+r)/2;
29       }
30 }nodes[4*MS];
31 
32 void creat(int root,int l,int r)
33 {
34       nodes[root].l=l;
35       nodes[root].r=r;
36       nodes[root].minv=INF;
37       nodes[root].maxv=-INF;
38       if(l==r)
39             return ;
40       creat(root<<1,l,(l+r)/2);
41       creat(root<<1|1,(l+r)/2+1,r);
42 }
43 
44 void updata(int root,int pos,int value)
45 {
46       if(nodes[root].l==nodes[root].r)
47       {
48             nodes[root].minv=nodes[root].maxv=value;
49             return ;
50       }
51       nodes[root].minv=min(nodes[root].minv,value);
52       nodes[root].maxv=max(nodes[root].maxv,value);
53       if(pos<=nodes[root].mid())
54             updata(root<<1,pos,value);
55       else
56             updata(root<<1|1,pos,value);
57 }
58 
59 void query(int root,int l,int r)   //注意这里的minv,maxv使用的全局变量,
60 {                                               //使用引用也可以
61       if(nodes[root].minv>=minv&&nodes[root].maxv<=maxv)
62             return ;    //   剪枝
63       if(nodes[root].l>=l&&nodes[root].r<=r)
64       {
65             minv=min(minv,nodes[root].minv);
66             maxv=max(maxv,nodes[root].maxv);
67             return ;
68       }
69       if(l<=nodes[root].mid())
70             query(root<<1,l,r);
71       if(r>nodes[root].mid())
72             query(root<<1|1,l,r);
73 }
74 
75 int main()
76 {
77       int N,Q,x,y;
78       scanf("%d%d",&N,&Q);
79       creat(1,1,N);
80       for(int i=1;i<=N;i++)
81       {
82             scanf("%d",&x);
83             updata(1,i,x);
84       }
85       while(Q--)
86       {
87             scanf("%d%d",&x,&y);
88             minv=INF;
89             maxv=-INF;
90             query(1,x,y);
91             printf("%d\n",maxv-minv);
92       }
93       return 0;
94 }

 






posted @ 2014-07-22 16:22  daydaycode  阅读(234)  评论(0编辑  收藏  举报