BZOJ 1699 Usaco Balanced lineup

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1699 Usaco Balanced lineup相关的知识,希望对你有一定的参考价值。

线段树区间最大最小值模板题目

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4  
 5 #define lson(x) ((x)<<1)
 6 #define rson(x) ((x)<<1|1)
 7  
 8 using std::max;
 9 using std::min;
10  
11 const int maxn=55555;
12 const int INF=0x3f3f3f3f;
13  
14 int Max[maxn<<2],Min[maxn<<2];
15 int num[maxn];
16  
17 void pushup(int x){
18     Max[x] = max(Max[lson(x)],Max[rson(x)]);
19     Min[x] = min(Min[lson(x)],Min[rson(x)]);
20 }
21  
22 void Build(int x,int L,int R){
23     if(L==R){
24         Max[x]=Min[x]=num[L];
25         return;
26     }
27     int mid = (L+R)>>1;
28     Build(lson(x),L,mid);
29     Build(rson(x),mid+1,R);
30     pushup(x);
31 }
32  
33 int queryMin(int x,int L,int R,int l,int r){
34     if(l<=L && R<=r) return Min[x];
35     int mid = (L+R)>>1;
36     int ans = INF;
37     if(l<=mid) ans = min(ans,queryMin(lson(x),L,mid,l,r));
38     if(r>mid) ans = min(ans,queryMin(rson(x),mid+1,R,l,r));
39     return ans;
40 }
41  
42 int queryMax(int x,int L,int R,int l,int r){
43     if(l<=L && R<=r) return Max[x];
44     int mid = (L+R>>1);
45     int ans = 0;
46     if(l<=mid) ans = max(ans,queryMax(lson(x),L,mid,l,r));
47     if(r>mid) ans = max(ans,queryMax(rson(x),mid+1,R,l,r));
48     return ans;
49 }
50  
51 int main(){
52     int n,q;
53     scanf("%d%d",&n,&q);
54     for(int i=1;i<=n;i++) scanf("%d",&num[i]);
55     Build(1,1,n);
56     for(int i=1;i<=q;i++){
57         int x,y;
58         scanf("%d%d",&x,&y);
59         int MAX = queryMax(1,1,n,x,y);
60         int MIN = queryMin(1,1,n,x,y);
61         printf("%d\n",MAX-MIN);
62     }
63     return 0;
64 }

 

以上是关于BZOJ 1699 Usaco Balanced lineup的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

bzoj1699/USACO2007Balanced Lineup排队——RMQ问题

[BZOJ] 1636: [Usaco2007 Jan]Balanced Lineup

[bzoj 1636][Usaco2007 Jan]Balanced Lineup

[BZOJ1637][Usaco2007 Mar]Balanced Lineup