POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)

http://poj.org/problem?id=3264

Time Limit: 5000MS     Memory Limit: 65536K

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

Source

 
 
题意:
一个农夫有N头牛,每头牛的高度不同,我们需要找出区间[a,b]中最高的牛和最低的牛的高度差。
 
题解:
明显是区间最值问题,线段树和RMQ
 
线段树做法(耗时2.2s):
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <map>
11 #include <math.h>
12 const int INF=0x3f3f3f3f;
13 typedef long long LL;
14 const int mod=1e9+7;
15 //const double PI=acos(-1);
16 const int maxn=1e5+10;
17 using namespace std;
18 //ios::sync_with_stdio(false);
19 //    cin.tie(NULL);
20 
21 int n,q;
22 struct node
23 {
24     int l;
25     int r;
26     int MAX;
27     int MIN;
28 }SegTree[50005<<2];
29 
30 void PushUp(int rt)
31 {
32     SegTree[rt].MAX=max(SegTree[rt<<1].MAX,SegTree[rt<<1|1].MAX);
33     SegTree[rt].MIN=min(SegTree[rt<<1].MIN,SegTree[rt<<1|1].MIN);
34 }
35 
36 void Build(int l,int r,int rt)
37 {
38     SegTree[rt].l=l;
39     SegTree[rt].r=r;
40     if(l==r)
41     {
42         scanf("%d",&SegTree[rt].MAX);
43         SegTree[rt].MIN=SegTree[rt].MAX;
44         return;
45     }
46     int mid=(l+r)>>1;
47     Build(l,mid,rt<<1);
48     Build(mid+1,r,rt<<1|1);
49     PushUp(rt);
50 }
51 
52 int Query_MAX(int L,int R,int rt)
53 {
54     int l=SegTree[rt].l;
55     int r=SegTree[rt].r;
56     if(L<=l&&R>=r)//一次也没有被涂过
57     {
58         return SegTree[rt].MAX;
59     }
60     int MAX=0;
61     int mid=(l+r)>>1;
62     if(L<=mid)
63         MAX=max(MAX,Query_MAX(L,R,rt<<1));
64     if(R>mid)
65         MAX=max(MAX,Query_MAX(L,R,rt<<1|1));
66     return MAX;
67 }
68 
69 int Query_MIN(int L,int R,int rt)
70 {
71     int l=SegTree[rt].l;
72     int r=SegTree[rt].r;
73     if(L<=l&&R>=r)//一次也没有被涂过
74     {
75         return SegTree[rt].MIN;
76     }
77     int MIN=INF;
78     int mid=(l+r)>>1;
79     if(L<=mid)
80         MIN=min(MIN,Query_MIN(L,R,rt<<1));
81     if(R>mid)
82         MIN=min(MIN,Query_MIN(L,R,rt<<1|1));
83     return MIN;
84 }
85 
86 int main()
87 {
88     scanf("%d %d",&n,&q);
89     Build(1,n,1);
90     for(int i=1;i<=q;i++)
91     {
92         int a,b;
93         scanf("%d %d",&a,&b);
94         printf("%d\n",Query_MAX(a,b,1)-Query_MIN(a,b,1));
95     }
96     return 0;
97 }
RMQ做法:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<climits>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8  
 9 const int N = 50005;
10 int FMAX[N][20], FMIN[N][20];
11  
12 void RMQ(int n)
13 {
14     for(int j = 1; j != 20; ++j)
15     {
16         for(int i = 1; i <= n; ++i)
17         {
18             if(i + (1 << j) - 1 <= n)
19             {
20                 FMAX[i][j] = max(FMAX[i][j - 1], FMAX[i + (1 << (j - 1))][j - 1]);
21                 FMIN[i][j] = min(FMIN[i][j - 1], FMIN[i + (1 << (j - 1))][j - 1]);
22             }
23         }
24     }
25 }
26  
27 int main()
28 {
29     int num, query;
30     int a, b;
31     while(scanf("%d %d", &num, &query) != EOF)
32     {
33         for(int i = 1; i <= num; ++i)
34         {
35             scanf("%d", &FMAX[i][0]);
36             FMIN[i][0] = FMAX[i][0];
37         }
38         RMQ(num);
39         while(query--)
40         {
41             scanf("%d%d", &a, &b);
42             int k = (int)(log(b - a + 1.0) / log(2.0));
43             int maxsum = max(FMAX[a][k], FMAX[b - (1 << k) + 1][k]);
44             int minsum = min(FMIN[a][k], FMIN[b - (1 << k) + 1][k]);
45             printf("%d\n", maxsum - minsum);
46         }
47     }
48     return 0;
49 }
 
最后推荐一篇写的挺好的一篇博客: https://blog.csdn.net/weixin_43272781/article/details/83443310
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11406808.html