【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)
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;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005, oo=~0u>>1;
int mx[N], mn[N], n, q, a[N];
void update(int x, int c) { for(; x<=n; x+=x&-x) mx[x]=max(mx[x], c), mn[x]=min(mn[x], c); }
int ask(int l, int r) {
int x=-oo, y=oo;
while(l<=r) {
if(r-l+1>=(r&-r)) {
y=min(y, mn[r]);
x=max(x, mx[r]);
r-=r&-r;
}
else {
y=min(y, a[r]);
x=max(x, a[r]);
--r;
}
}
return x-y;
} int main() {
read(n); read(q);
int x, y;
CC(mx, -1); CC(mn, 0x7f);
for1(i, 1, n) { read(a[i]); update(i, a[i]); }
for1(i, 1, q) {
read(x); read(y);
printf("%d\n", ask(x, y));
}
return 0;
}
Description
每 天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 注意: 在最大数据上, 输入和输出将占用大部分运行时间.
Input
* 第一行: N 和 Q. * 第2..N+1行: 第i+1行是第i头牛的身高.
* 第N+2..N+Q+1行: 两个整数, A 和 B (1 <= A <= B <= N), 表示从A到B的所有牛.
Output
*第1..Q行: 所有询问的回答 (最高和最低的牛的身高差), 每行一个.
Sample Input
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
3
0
HINT
Source
【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)的更多相关文章
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )
RMQ.. ------------------------------------------------------------------------------- #include<cs ...
- BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树
题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...
- 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Description 每天,农夫 John ...
- ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...
- 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队
[算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...
- BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队 - 线段树
description 查询区间最大和最小 题解 线段树 愉悦身心啊 代码 #include<cstring> #include<cstdio> #include<alg ...
随机推荐
- 算法笔记_033:十六进制转八进制(Java)
目录 1 问题描述 2 解决方案 2.1 注意问题 2.2 具体实现代码 1 问题描述 具体问题描述 给定n个十六进制正整数,输出它们对应的八进制数. 输入格式 输入的第一行为一个正整数n (1& ...
- Python 正则表达式学习摘要及资料
来源:Michael_翔_ 摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会 ...
- model中设置默认值时 ,使用 lambda 与否的差别以及datetime的默认值方法
'date': '2013-01-01' #固定值 'date': time.strftime('%Y-%m-%d') #启动时候的值 'date': lambda *a: time.strfti ...
- OE context 传参数
来自:http://shine-it.net/index.php/topic,16360.0.html 有个需求想many2one字段关联显示的value在各个模块显示不同的值. 如果直接该rel_n ...
- java 资源文件的读取
Java将配置文件当作一种资源(resource)来处理,并且提供了两个类来读取这些资源,一个是Class类,另一个是ClassLoader类. gradle 项目 项目目录结构 用Class类加载 ...
- OFBiz:添加实体栏位
如何添加实体栏位?这里演示为PostalAddress添加planet栏位.打开applications/party/entitydef/entitymodel.xml,找到PostalAddress ...
- 中文latex参考文献格式
中文latex参考文献格式 原来英文: \begin{thebibliography}{1} \bibitem{Ben-Shimon2015RecSys} D.~Ben-Shimon, A.~Tsik ...
- SectionIndexer中的getSectionForPosition()与getPositionForSection()解惑
大家在做字母索引的时候常常会用到SectionIndexer这个类,里面有2个重要的方法 1. getSectionForPosition()通过该项的位置,获得所在分类组的索引号 2. ge ...
- 用string存取二进制数据
STL的string很强大,用起来也感觉很舒服,这段时间在代码中涉及到了用string存取二进制数据的问题,这里记录一下,以供以后参考. 首先提一下STL中string的参考资料:http://www ...
- Jquery实现无刷新DropDownList联动
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...