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. Visual Studio自动添加头部注释

    VS2013 自动添加头部注释 1.找到VS2013的安装目录 下文以安装目录 C:\Program Files (x86)\Microsoft Visual Studio 12.0 为例 2.修改C ...

  2. 通过python给mysql建表

    一.python连接mysql from sqlalchemy import create_engine # 数据库数据 HOSTNAME = '127.0.0.1' # linux本地 PORT = ...

  3. 框架之Tornado(简单介绍)

    引言 回想Django的部署方式 以Django为代表的python web应用部署时采用wsgi协议与服务器对接(被服务器托管),而这类服务器通常都是基于多线程的,也就是说每一个网络请求服务器都会有 ...

  4. HTML5 Canvas ( 径向渐变, 升级版的星空 ) fillStyle, createRadialGradient

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. delphi 编译生成ipa文件 adhoc步骤

    找IPA文件 开发模式ipa文件和发布模式ipa文件,路径不同. http://www.itnose.net/detail/6101808.html 一.开发模式Development 不需要真机,可 ...

  6. Spring boot @PropertySource, @ImportResource, @Bean

    @PropertySource:加载指定的配置文件 /** * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉SpringBoot将本类 ...

  7. as3 air 获取文件夹下的所有文件

    private function getFile(directory:File) { var files:Array = directory.getDirectoryListing(); for(va ...

  8. Mysql日期时间Extract函数介绍

    MySQL日期时间Extract函数的优点在于可以选取日期时间的各个部分,从年一直到微秒,让我们对MySQL日期时间的处理更为轻松. MySQL 日期时间 Extract(选取)函数.1. 选取日期时 ...

  9. Nginx打开目录浏览功能(autoindex)以及常见问题解决方案

    Nginx默认是不允许列出整个目录的.如需此功能,打开nginx.conf文件,在location server 或 http段中加入autoindex on;另外两个参数最好也加上去: autoin ...

  10. Mysql 5.8安装报错

    1 mysql安装过程中报下面这个错 解决方法:只要将目录改成反斜杠即可. 2 修改密码错误:ERROR 1064 (42000): You have an error in your SQL syn ...