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.
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
思路:这题题意就是给你n个数让你求某个区间内最大值与最小值的差值,暴力肯定超时,我这里是线段树的写法,具体看注释......
1 #include<iostream>
2 #include<cstring>
3 #include<cmath>
4 #include<cstdio>
5 #include<algorithm>
6 #include<map>
7 #include<set>
8 #include<vector>
9 #include<queue>
10 using namespace std;
11 #define ll long long
12 const int mod=1e9+7;
13 const int inf=1e9+7;
14 const int maxn=50000+10;
15 int n,m;
16 int x,y;
17 int maxh,minh;//记录所查询区间最值
18 typedef struct
19 {
20 int left;
21 int right;
22 int weight;//这里只需要记录区间最值,权重可以不要,但是写起来复杂点,所以还是要了
23 int maxx;//这里是关键,每个树节点建立最大值和最小值标记
24 int minn;
25 } St;
26 St tree[4*maxn];
27 void build_tree(int k,int left,int right)//建树
28 {
29 tree[k].left=left;
30 tree[k].right=right;
31 if(left==right)//到达根节点
32 {
33 cin>>tree[k].weight;//输入权重
34 tree[k].maxx=tree[k].minn=tree[k].weight;//在根节点,最大值最小值都是他自己
35 return ;
36 }
37 int mid=(tree[k].left+tree[k].right)>>1;
38 build_tree(k<<1,left,mid);//左子树
39 build_tree(k<<1|1,mid+1,right);//右子树
40 tree[k].maxx=max(tree[k<<1].maxx,tree[k<<1|1].maxx);//这里是关键,在建树的这个过程把区间的两个最值标记,
41 tree[k].minn=min(tree[k<<1].minn,tree[k<<1|1].minn);//相当于状态合并,最后直接查询即可
42 //tree[k].weight=tree[k<<1].weight+tree[k<<1|1].weight;//权重的状态合并在这题没有意义,所以可以不用
43 }
44
45 void ask_qujian(int k)//区间查询两个最值
46 {
47 if(tree[k].left>=x&&tree[k].right<=y)//包括区间
48 {
49 maxh=max(maxh,tree[k].maxx);//刷新最值
50 minh=min(minh,tree[k].minn);
51 return ;
52 }
53 int mid=(tree[k].left+tree[k].right)>>1;
54 if(x<=mid)//向左子树查询
55 ask_qujian(k<<1);
56 if(y>mid)//向右子树查询
57 ask_qujian(k<<1|1);
58 }
59
60 int main()
61 {
62 ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
63 cin>>n>>m;//n个点,m个操作
64
65 build_tree(1,1,n);//建树
66
67 for(int i=0;i<m;i++)
68 {
69 cin>>x>>y;//输入查询区间
70 maxh=-inf;//初始化最值
71 minh=inf;
72 ask_qujian(1);//区间查询两个最值
73 cout<<maxh-minh<<endl;//输出
74 }
75
76 return 0;
77 }