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

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

HINT

Source

【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)的更多相关文章

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

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

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

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

  3. 【BZOJ】1636: [Usaco2007 Jan]Balanced Lineup(rmq+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1636 (我是不会说我看不懂题的) 裸的rmq.. #include <cstdio> # ...

  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排队 分块

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

  6. ST表 || RMQ问题 || BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队 || Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    题面:P2880 [USACO07JAN]平衡的阵容Balanced Lineup 题解: ST表板子 代码: #include<cstdio> #include<cstring&g ...

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

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

  8. 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队

    [算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...

  9. BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队 - 线段树

    description 查询区间最大和最小 题解 线段树 愉悦身心啊 代码 #include<cstring> #include<cstdio> #include<alg ...

随机推荐

  1. java程序设计题库

    选择题 答题要求:单选题,每题只有一个正确答案,选择正确给分,不正确不给分. 1. 下面(   A    )数据类型可用于main()方法中传递的参数 A.String   B.Integer   C ...

  2. 亲热接触Redis-第二天(Redis Sentinel)

    简单介绍 经过上次轻松搭建了一个Redis的环境并用Java代码调通后.这次我们要来看看Redis的一些坑以及Redis2.8以后带来的一个新的特性即支持高可用特性功能的Sentinel(哨兵). R ...

  3. 【BIRT】01_在win10上安装BIRT

    环境:windows 10 64位 安装文件:链接:https://pan.baidu.com/s/1vYGbB0D1QeQ923oIIdoI9Q 密码:qcde 说明:安装文件也可以自己去官网下载, ...

  4. PHP LDAP class for Active Directory

    A class for PHP to talk to Active Directory through LDAP.http://sourceforge.net/projects/adldap/

  5. 【LeetCode】113. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  6. NuGet学习笔记(2) 使用图形化界面打包自己的类库[转]

    http://www.cnblogs.com/lzrabbit/archive/2012/05/01/2477607.html 上文NuGet学习笔记(1) 初识NuGet及快速安装使用说到NuGet ...

  7. 和求余运算巧妙结合的jns指令

    .text:004A78B1  and eax, 80000001h.text:004A78B6  jns short loc_4A78BD.text:004A78B8 dec eax.text:00 ...

  8. Jquery 大纲

    1,核心 核心函数 对象訪问 数据缓存 队列控制 插件机制 多库共存 2.选择器 基本选择器 层级 基本 内容 可见性 属性 子元素 表单 表单对象属性 3.文档处理 内部插入 外部插入 包裹 替换 ...

  9. Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript.

    Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript. 1. 现象::主程序卡住无反应,多行任务不往下执行 1 2. 原因::使用j ...

  10. vue2.0项目的环境配置以及有哪些的坑

    npm run dev项目运行:$ git pull origin master 更新代码项目的提交代码git push -u origin master 这里就开始准备提交到网络上了 项目的地址gi ...