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. angularjs探秘<一>认识angularjs

    首先聊聊angularjs是啥. 首先AngularJS 是一个 JavaScript 框架.(PS:其实就是外部引用的js文件) 所以AngularJS的使用依然是外部引用js文件. 附上引用地址 ...

  2. K-means算法(理论+opencv实现)

    写在前面:之前想分类图像的时候有看过k-means算法,当时一知半解的去使用,不懂原理不懂使用规则...显然最后失败了,然后看了<机器学习>这本书对k-means算法有了理论的认识,现在通 ...

  3. apache伪静态失败,但是phpinfo显示有rewrite的时候考虑的情况

    大家知道除了加载loadmodule后 还需要修改http.conf 使apache支持.htaccess 允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All ...

  4. MPI Hello World

    ▶<并行程序设计导论>第三章(用 MPI 进行分布式内存编程)的第一个程序样例. ●  代码 #include <stdio.h> #include <string.h& ...

  5. 0_Simple__simpleMPI

    MPI 的简单使用 ▶ 源代码.主机根结点生成随机数组,发布副本到各结点(例子用孩子使用了一个结点),分别使用 GPU 求平方根并求和,然后根结点使用 MPI 回收各节点的计算结果,规约求和后除以数组 ...

  6. Hive启动异常

    [root@host ~]# hivewhich: no hbase in (/root/app/apache-maven-3.5.2/bin:/usr/local/sbin:/usr/local/b ...

  7. leetcode29

    class Solution { public int divide(int dividend, int divisor) { if (dividend == Integer.MIN_VALUE &a ...

  8. can't load package the specified module could not be found

    can't load package the specified module could not be found 用 Dependency Walker 2.2Dependency Walker ...

  9. SpringMvc 获取ApplicationContext

    有时,我们不通过Controller层进入Service层,比如同步数据,任务,以及文件上传共通Handler对文件处理后保存数据等都会由一个非Controller类调用Service. 这时候如果n ...

  10. Mysql 知识(2)

    1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一 ...