POJ3264Balanced Lineup线段树模板题查询区间内最大值和最小值之差

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

题意:

给出n和q,给出n头奶牛的身高,q次询问,每次询问给出区间a、b,求出区间内的最大值和最小值之差

这里注意一下给出的样例:(解释一下输出)

给出的样例为1、7、3、4、2、5,表示区间1、2、3、4、5、6

思路:

线段树的模板题,求出区间内的最大值和最小值之差也就是查询已经建立好的线段树的最大值和最小值之差

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stdlib.h>
 8 typedef long long ll;
 9 using namespace std;
10 const int N=50020;
11 
12 //需要开到四倍空间
13 int a[4*N];//max
14 int b[4*N];//min
15 
16 //每个父节点记录的是它下面的两个节点的最大值
17 void build(int L,int R,int i)
18 {
19     if(L==R)
20     {
21         scanf("%d",&a[i]);
22         b[i]=a[i];
23         return;
24     }
25     int mid=(L+R)>>1;
26     build(L,mid,i<<1);
27     build(mid+1,R,i<<1|1);
28     a[i]=max(a[i<<1],a[i<<1|1]);
29     b[i]=min(b[i<<1],b[i<<1|1]);
30     //pushup(i)////每次传的时候把根节点也往下去寻找最大值
31     ////比较其左右两个节点的大小,取最大值
32     //看题目给的需要求什么
33 }
34 
35 
36 //query(aa,bb,1,n,1)
37 int querymax(int left,int right,int L,int R,int i)//a求区间最小值
38 {
39     if(left<=L&&right>=R)
40         return a[i];
41     int mid=(L+R)>>1;
42     int ans=-1;
43     if(left<=mid)
44         ans=max(ans,querymax(left,right,L,mid,i<<1));
45     // else
46     if(right>mid)
47         ans=max(ans,querymax(left,right,mid+1,R,i<<1|1));
48     return ans;
49 }
50 
51 //query(aa,bb,1,n,1)
52 int querymin(int left,int right,int L,int R,int i)//b求区间最大值
53 {
54     if(left<=L&&R<=right)
55         return b[i];
56     int mid=(L+R)>>1;
57     int ans=0x3f3f3f3f;
58     if(left<=mid)
59         ans=min(ans,querymin(left,right,L,mid,i<<1));
60     // else
61     if(right>mid)
62         ans=min(ans,querymin(left,right,mid+1,R,i<<1|1));
63     return ans;
64 }
65 
66 int main()
67 {
68     int n,m;
69     while(~scanf("%d %d",&n,&m))
70     {
71    //     memset(a,0,sizeof(a));
72         build(1,n,1);//传入最左端点,最右端点,根节点进行建树
73         //建树的过程中输入每一个节点
74         for(int i=0; i<m; i++)
75         {
76             int aa,bb;
77             scanf("%d %d",&aa,&bb);
78             int kk=querymax(aa,bb,1,n,1)-querymin(aa,bb,1,n,1);
79             printf("%d\n",kk);
80         }
81     }
82     return 0;
83 }

树状数组:

这个代码思路看注释的话好理解,但是代码不好理解

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 using namespace std;
 5 
 6 int a[50020],maxx[50020],minn[50020];
 7 
 8 int lowbit(int x)
 9 {
10     return x&(-x);
11 }
12 
13 int w(int L,int R)
14 {
15     int min1=a[R];
16     int max1=a[R];
17     while(L!=R)
18     {
19         for(R--; R-lowbit(R)>=L; R=R-lowbit(R))
20         {
21             min1=min(min1,minn[R]);
22             max1=max(max1,maxx[R]);
23         }
24         max1=max(max1,a[R]);
25         min1=min(min1,a[R]);
26 
27     }
28     return max1-min1;
29 
30 }
31 
32 int main()
33 {
34     std::ios::sync_with_stdio(false);
35     int n,q;
36     cin>>n>>q;
37     for(int i=1; i<=n; i++)
38     {
39         cin>>a[i];
40 //        update(i,a[i]);
41         maxx[i]=minn[i]=a[i];
42         for(int j=1; j<lowbit(i); j*=2)
43         {
44             maxx[i]=max(maxx[i],maxx[i-j]);
45             minn[i]=min(minn[i],minn[i-j]);
46         }
47     }
48     for(int i=0; i<q; i++)
49     {
50         int aa,bb;
51         cin>>aa>>bb;
52         cout<<w(aa,bb)<<endl;
53     }
54     return 0;
55 }
View Code

这个代码也A了,可以参考这个,类似树状数组的原模板,好理解,细节上变动一点就可以了

原文地址:https://www.cnblogs.com/OFSHK/p/11291759.html