题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 50010 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #de…
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000).…
RMQ.. ------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream>   #define rep( i , n ) for( int i = 0 ; i < n ; ++i ) #define clr( x…
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) 个可能的牛的…
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; const int N=100005; int n,q,b[N],mn[N][20],mx[N][20]; int read() { int r=0,f=1; char p=getchar(); while(p>'9'||p<'0') { if(p=='-') f=-1; p=getchar();…
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring> #include<iostream> #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) using namespace std; ,max_log=,maxlog=,inf=<<…
description 查询区间最大和最小 题解 线段树 愉悦身心啊 代码 #include<cstring> #include<cstdio> #include<algorithm> #define rd read() #define lson nd << 1 #define rson nd << 1 | 1 using namespace std; const int N = 1e5; ; ], MIN[N << ], a[N],…
[算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ; ]; int n,q,a[maxn]; int read_t;char c; int read() { read_t=; while(!isdigit(c=getchar())); +c-';}while(isdigit(c=getchar())); return read_t; } void buil…
http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> using namespace std; #defi…
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 568[Submit][Status] Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. Jo…
[Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q &l…
bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差.序列大小≤50000 题解: RMQ问题.注意log2区间长度可先递推好,这样可以保证询问O(1). 代码: #include <cstdio> #include <cstring> #include <algorithm> #define inc(i,j,k) for(i…
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线段树裸题... 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 o…
Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 注意: 在最大数据上, 输入和…
http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> using namespace std; #define re…
很明显是一道RMQ问题,倍增法,维护一下区域的最大/小值就行了. var n,i,j,q,f,t,times:longint; hmin,hmax:array[..,..] of longint; function min(a,b:longint):longint; begin if a>b then exit(b) else exit(a); end; function max(a,b:longint):longint; begin if a>b then exit(a) else exit…
noip要来了,刷点基础水题. 题意: RMQ,给你N个数,Q个询问,每次查询[l,r]内,最大值减最小值是多少. 写的ST. 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> //by zrt //problem: using namespace std; typedef long long LL; const int inf(0x3f3f3f3f);…
1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[Submit][Status] 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 deci…
Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最小值,然后做差就好了,基本上就是线段树模板题 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int i…
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 总是喜欢做一些非凡的事,当然这次照相也不例外.他只给一部分牛照相,并且这一组牛的阵容必须是"…
将 0 变为 -1 , 则只需找区间和为 0 , 即前缀和相同的最长区间 , 记录一下每个前缀和出现的最早和最晚的位置 , 比较一下就 OK 了 ------------------------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostr…
题目描述 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 注意: 在最大数据上, 输入和输出将占用大部…
Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 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…
Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42489   Accepted: 20000 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…
Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 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…
不要被线段树这个名字和其长长的代码吓到. D - Balanced Lineup 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 thin…
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…
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 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 de…
Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32778   Accepted: 15425 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…
个人心得:线段树就是将一段序列拆分为一个个单独的节点,不过每俩个节点又可以联系在一起,所以就能很好的结合,比如这一题, 每次插入的时候都将这一段区间的最大最小值更新,就能大大减少时间. 这个线段树建立是以数组的,根节点为0,后面每次都是父节点*2+1/2. 这题简单的教会了我如何创建线段树,以及一些简单的线段树操作,还要继续加深. For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the…