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排队 - 线段树的更多相关文章

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

    题意:链接 方法:线段树 解析: 题意即题解. 多次询问区间最大值与最小值的差.显然直接上线段树或者rmq维护区间最值就可以. 代码: #include <cstdio> #include ...

  2. bzoj1699[Usaco2007 Jan]Balanced Lineup排队*&bzoj1636[Usaco2007 Jan]Balanced Lineup*

    bzoj1699[Usaco2007 Jan]Balanced Lineup排队 bzoj1636[Usaco2007 Jan]Balanced Lineup 题意: 询问区间最大值减区间最小值的差. ...

  3. BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 56 ...

  4. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...

  5. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

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

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MB Description 每天,农夫 John ...

  7. [Usaco2007 Jan]Balanced Lineup排队

    [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...

  8. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  9. bzoj:1699;poj 3264: [Usaco2007 Jan]Balanced Lineup排队

    Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置 ...

随机推荐

  1. 存储设备的DDP功能详解

    http://blog.csdn.net/u013394982/article/details/18259015 DDP功能,即Dynamic Disk Pool,它是除了现有的RAID0,1,10, ...

  2. Noip知识点备考

    作为一个oier,适当的整理是有必要的.蒟蒻根据自己的理解,筛选出考noip应当掌握的知识点.可能后期还有解题思路和模板,先挖个坑慢慢补呗. 60级张炳琪Noip知识点总结 可能是本人比较弱,写的内容 ...

  3. relocation error: /usr/lib64/libc.so.6: symbol _dl_starting_up, version GLIBC_PRIVATE not defined in file ld-linux-x86-64.so.2 with link time reference 问题解决

    在建立一个错误的软连接到ld-linux-x86-64.so.2时,悲剧就这么发生了.此时大部分命令都不能使用,SSH当然也不能登录了.这个时候一定不要退出终端. 有人说那就把软连接复原吧,可是ln也 ...

  4. 并发工具类(三)控制并发线程的数量 Semphore

    前言   JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...

  5. python高亮显示输出

    知识内容: 1.高亮输出语法 2.高亮输出实例 前言: 在做购物车这道题时遇到了高亮显示输出某些内容的需求,于是就学了一下这方面的知识,以下是python高亮显示输出的使用方法: 购物车链接:  ht ...

  6. sparkcontext 和 sparkconf

    http://spark.apache.org/docs/latest/rdd-programming-guide.html The first thing a Spark program must ...

  7. leetcode122

    public class Solution { public int MaxProfit(int[] prices) { var list = new List<KeyValuePair< ...

  8. Java实现邮箱发送

  9. Spring MVC 确定目标方法POJO 类型参数

    1:确定一个Key 2. 在implicitMode 中存在Key 对应的对象, 若存在则作为参数传入 3. 在implicitMode 中不存在Key 对应的对象, 则检查当前@SessionAtr ...

  10. as3 文本竖排效果实现

    import flash.text.engine.TextBlock; import flash.text.engine.ElementFormat; import flash.text.engine ...