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 }
时间: 2024-10-24 19:37:06

BZOJ 1699 Usaco Balanced lineup的相关文章

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

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的

[bzoj 1699] [Usaco2007 Jan]Balanced Lineup排队

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1699 [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1443  Solved: 920[Submit][Status][Discuss] Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一

bzoj 1637: [Usaco2007 Mar]Balanced Lineup

1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000)和种族(0或1). 一直以来 Farmer John 总是喜欢做一些非凡的事,当然这次照相也不例外.他只给一部分牛照相,并且这一组牛的阵容必须是"

[BZOJ] 1636: [Usaco2007 Jan]Balanced Lineup

1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 909  Solved: 644[Submit][Status][Discuss] Description For the daily milking, Farmer John's N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer

BZOJ 1637: [Usaco2007 Mar]Balanced Lineup( sort + 前缀和 )

将 0 变为 -1 , 则只需找区间和为 0 , 即前缀和相同的最长区间 , 记录一下每个前缀和出现的最早和最晚的位置 , 比较一下就 OK 了 ------------------------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostr

[POJ] 3264 Balanced Lineup [ST算法]

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 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 Joh

poj 3246 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 38942   Accepted: 18247 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 Joh

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 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 Joh

poj3264 Balanced Lineup 2011-12-20

Balanced Lineup Time Limit: 5000MSMemory Limit: 65536K Total Submissions: 20569Accepted: 9552 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 decid