BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队 - 线段树
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;
const int inf = ~0U >> ; int MAX[N << ], MIN[N << ], a[N], q, n; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= '';c = getchar()) X = X * + c - '';
return X * p;
} void print(int x) {
if(x < ) putchar('-'), x = -x;
if(x >= ) print(x / );
putchar(x % + '');
} void update(int nd) {
MIN[nd] = min(MIN[lson], MIN[rson]);
MAX[nd] = max(MAX[lson], MAX[rson]);
} void build(int l, int r, int nd) {
if(l == r) {
MIN[nd] = MAX[nd] = a[l];
return;
}
int mid = (l + r) >> ;
build(l, mid, lson);
build(mid + , r, rson);
update(nd);
} int query_max(int L, int R, int l, int r, int nd) {
if(L <= l && r <= R) return MAX[nd];
int mid = (l + r) >> , tmp = ;
if(L <= mid) tmp = max(tmp, query_max(L, R, l, mid, lson));
if(mid < R) tmp = max(tmp, query_max(L, R, mid + , r, rson));
return tmp;
} int query_min(int L, int R, int l, int r, int nd) {
if(L <= l && r <= R) return MIN[nd];
int mid = (l + r) >> , tmp = inf;
if(L <= mid) tmp = min(tmp, query_min(L, R, l, mid, lson));
if(mid < R) tmp = min(tmp, query_min(L, R, mid + , r, rson));
return tmp;
} int main()
{
n = rd; q = rd;
for(int i = ; i <= n; ++i) a[i] = rd;
build(, n, );
for(int i = ; i <= q; ++i) {
int l = rd, r = rd;
int mx = query_max(l, r, , n, ), mn = query_min(l, r, , n, );
print(mx - mn);
putchar('\n');
}
}
BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队 - 线段树的更多相关文章
- BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树
题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...
- bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*
bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...
- BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 933 Solved: 56 ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )
RMQ.. ------------------------------------------------------------------------------- #include<cs ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块
1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Description 每天,农夫 John ...
- [Usaco2007 Jan]Balanced Lineup排队
[Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...
- bzoj:1699;poj 3264: [Usaco2007 Jan]Balanced Lineup排队
Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置 ...
随机推荐
- 两个栈实现队列 Python实现
# coding=utf-8 MAX_LENGTH = 100 SUCCESS = 1 FAIL = 0 ERROR = -1 class Queue(object): stack_fir = Non ...
- 一种思路,隐藏input标签,通过label关联
<label class="btn btn-default btn-file">上传图片 <input hidden type="file" ...
- 解决报错 Page directive: illegal to have multiple occurrences of contentType with different values (old:
转自:https://blog.csdn.net/dorothy1224/article/details/78064288
- 笔记本 T450的鼠标经常不灵
T450的鼠标经常不灵,鼠标总感觉有延迟. 换了鼠标也是这样. 有人反应说是USB断电, 使用的是省电模式,在设备管理>鼠标>电源选项>节电模式 勾去掉就可以了, 但是我的节点模式是 ...
- Haskell语言学习笔记(50)Extra
Extra 安装 extra 模块. $ cabal install extra Installed extra-1.6 Prelude> :m +Extra Prelude Extra> ...
- 趣味编程:CPS风格代码(C#,F#版)
CPS风格代码(C#版) using System; namespace fp { class CPS { static int add(int x, int y) => x + y; stat ...
- java public project default private
- 安装 neo4j 在 .../bin 目录下使用 ./neo4j 没反应 和 从csv 导入数据到neo4j
可以使用 /bin/sh ./neo4j start 如果提示:./neo4j: 28: set: Illegal option -o pipefail 那么 ubuntu”set Illegal o ...
- spark性能调优 数据倾斜 内存不足 oom解决办法
[重要] Spark性能调优——扩展篇 : http://blog.csdn.net/zdy0_2004/article/details/51705043
- SMO算法(转)
作者:[已重置]链接:https://www.zhihu.com/question/40546280/answer/88539689来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...