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. 手动脱UPX 壳实战

    作者:Fly2015 Windows平台的加壳软件还是比較多的,因此有非常多人对于PC软件的脱壳乐此不彼,本人菜鸟一枚,也学习一下PC的脱壳.要对软件进行脱壳.首先第一步就是 查壳.然后才是 脱壳. ...

  2. sql通过某个字段名找到数据库中对应的表

    sql通过某个字段名找到数据库中对应的表 SELECT sb.name FROM syscolumns s JOIN sysobjects sb ON s.id=sb.id WHERE s.name= ...

  3. (数据库)DBCP连接池配置参数说明

    <!-- 数据源1 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicData ...

  4. javascript设计模式:构造器模式学习一

    javascript 设计模式1.简介javascript是一种弱类型语言,不过类可以通过函数模拟出来最常见的实现方法如下:function Car(model){ this.model = mode ...

  5. web站点,同一个浏览器只能登陆一个用户的原因(cookie不能跨浏览器)

    我的web站点,比如  http://ip/testsite/default.aspx, 当我在我的机器上,用chrome打开,用账号user1登陆,那么当我再新开个tab,再打开这个web站点,这时 ...

  6. 调用Android自带日历功能

    Android手机配备有一个内置的日历应用程序.第三方应用程序可以利用日历内容提供商接口读取用户的日历信息和安排在日历新的事件.这个日历可以直接同步用户的谷歌日历. 不幸的是,没有文档和Android ...

  7. sed正则

    sed -i 's/[A-Za-z0-9]*\.sdongpo\.com/group110.sdongpo.com/g' test.js

  8. PySpark的DataFrame处理方法

    转:https://blog.csdn.net/weimingyu945/article/details/77981884 感谢! ---------------------------------- ...

  9. IT行业面试指导 计算机行业面试技巧 面试技巧

    简历篇 简历是你的的第一张脸,简历写的是否合理,是否吸引人,将决定你能否赢得宝贵的面试机会,迈出第一步! l  姓名,性别,学历,居住地,工作年限,邮箱,手机号 l  填“现居住地”,不要填成“户籍所 ...

  10. 使用 SSHFS 挂载远程的 Linux 文件系统及目录

    1. 安装 sudo apt-get install sshfs 2. 创建 SSHFS 挂载目录 sudo mkdir /mnt/cong 3.使用 SSHFS 挂载远程的文件系统 sudo ssh ...