http://www.lydsy.com/JudgeOnline/problem.php?id=1636

(我是不会说我看不懂题的)

裸的rmq。。

#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;
int mx[N], n, q, a[N], mn[N];
void update(int x, int c) { for(; x<=n; x+=x&-x) mx[x]=max(c, mx[x]), mn[x]=min(c, mn[x]); }
int ask(int l, int r) {
int x=0, y=~0u>>1;
while(l<=r) {
x=max(x, a[r]); y=min(y, a[r]);
for(--r; r-l>=(r&-r); r-=r&-r)
x=max(mx[r], x), y=min(mn[r], y);
}
return x-y;
} int main() {
read(n); read(q);
CC(mn, 0x7f);
for1(i, 1, n) { read(a[i]); update(i, a[i]); }
int l, r;
while(q--) {
read(l); read(r);
printf("%d\n", ask(l, r));
}
return 0;
}

Description

For the daily milking, Farmer John's N cows (1 <= N <= 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height. Farmer John has made a list of Q (1 <= Q <= 200,000) potential groups of cows and their heights (1 <= height <= 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

* Line 1: Two space-separated integers, N and Q. * Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i * Lines N+2..N+Q+1: Two integers A and B (1 <= A <= B <= N), representing the range of cows from A to B inclusive.

Output

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

Sample Input

* Lines 1..Q: Each line contains a single integer that is a response
to a reply and indicates the difference in height between the
tallest and shortest cow in the range.

Sample Output

6
3
0

HINT

Source

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

  1. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  2. BZOJ 1636: [Usaco2007 Jan]Balanced Lineup

    noip要来了,刷点基础水题. 题意: RMQ,给你N个数,Q个询问,每次查询[l,r]内,最大值减最小值是多少. 写的ST. 代码: #include<iostream> #includ ...

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

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

  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. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

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

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

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

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

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

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

随机推荐

  1. linux cut 命令

    cut:以某种方式按照文件的行进行分割 参数列表: -b 按字节选取 忽略多字节字符边界,除非也指定了 -n 标志 -c 按字符选取 -d 自定义分隔符,默认为制表符. -f 与-d一起使用,指定显示 ...

  2. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  3. python--标准库 时间与日期 (time, datetime包)

    Python具有良好的时间和日期管理功能.实际上,计算机只会维护一个挂钟时间(wall clock time),这个时间是从某个固定时间起点到现在的时间间隔.时间起点的选择与计算机相关,但一台计算机的 ...

  4. C# params传递多个参数

    C#开发语言中 params 是关键字,可以指定在参数数目可变处采用参数的方法参数.在函数的参 数数目可变而执行的代码差异很小的时候很有用! params关键字表示函数的参数是可变个数的,即可变的方法 ...

  5. java同一个实体的复制

    import org.springframework.beans.BeanUtils; //将mon的值复制给monitorCommission;monitorCommission是实体Monitor ...

  6. Atitit.spring体系结构大总结

    Atitit.spring体系结构大总结 1. Srping mvc 1 2. Ioc 4 3. ApplicationContext在BeanFactory的基础上构建,区别 4 4. Aop 5 ...

  7. centos 安装部署ftp服务器

    0. 安装ftp yum install vsftpd 1. 添加ftp账户 useradd -d /home/test -g ftp -s /sbin/nologin test 命令的意思: 添加t ...

  8. python3+spark2.1+kafka0.8+sparkStreaming

    python代码: import time from pyspark import SparkContext from pyspark.streaming import StreamingContex ...

  9. SAP ERP 6.0 EHP7 SR2(WINDOWS MSSQL版)安装说明

    原文 by 枫竹丹青 ⋅ 1.安装准备 1.1.版本说明 本文是描述在一个Windows虚拟机.SQL Server数据库环境下,安装SAP ERP 6.0 EHP7 SR2服务器,安装完成虚拟机文件 ...

  10. 解决:Scanner输入数字总是莫名其妙多个0

    用BufferedInputStream解决 Scanner scanner = new Scanner(new BufferedInputStream(System.in)); System.out ...