1699: [Usaco2007 Jan]Balanced Lineup排队

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

——分隔符——

传说中的线段树水题,维护最大值和最小值。

代码:

#include<cstdio>
using namespace std;
struct Btree{
int left;
int right;
int num;
int max;
int min;
};
const int Maxn=50000;
Btree tree[Maxn*4+1]; int height[Maxn+1]; int n,q; inline int remax(int a,int b){
if (a>b) return a;
return b;
} inline int remin(int a,int b){
if (a<b) return a;
return b;
} void build(int x,int left,int right){
tree[x].left=left;
tree[x].right=right;
if (left==right){
tree[x].min=tree[x].max=tree[x].num=height[left];
}else{
int mid=(left+right)/2;
build(x*2,left,mid);
build(x*2+1,mid+1,right);
tree[x].max=remax(tree[x*2].max,tree[x*2+1].max);
tree[x].min=remin(tree[x*2].min,tree[x*2+1].min);
}
} int queryMax(int x,int left,int right){
if (left<=tree[x].left && tree[x].right<=right){
return tree[x].max;
}else{
int mid=(tree[x].left+tree[x].right)/2;
int maxNum=-1;
if (left<=mid) maxNum=remax(maxNum,queryMax(x*2,left,right));
if (right>=mid+1) maxNum=remax(maxNum,queryMax(x*2+1,left,right));
return maxNum;
}
} int queryMin(int x,int left,int right){
if (left<=tree[x].left && tree[x].right<=right){
return tree[x].min;
}else{
int mid=(tree[x].left+tree[x].right)/2;
int minNum=2147483647;
if (left<=mid) minNum=remin(minNum,queryMin(x*2,left,right));
if (right>=mid+1) minNum=remin(minNum,queryMin(x*2+1,left,right));
return minNum;
}
} int main(){
scanf("%d%d",&n,&q);
for (int i=1;i<=n;i++){
scanf("%d",&height[i]);
}
build(1,1,n);
for (;q--;){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",queryMax(1,l,r)-queryMin(1,l,r));
} return 0;
}

BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队的更多相关文章

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

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

  2. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队 分块

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

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

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

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

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

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

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

  6. 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...

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

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

  8. BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

    1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 56 ...

  9. [Usaco2007 Jan]Balanced Lineup排队

    [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Des ...

随机推荐

  1. 简单使用SimpleCursorAdapter

    http://my.oschina.net/javaeye/blog/14846 果使用Sqlite,建议和ContentProvider结合使用.这样数据库的生命周期就不用自己管了.然后,如果要在比 ...

  2. ASP.NET之电子商务系统开发-1(数据列表)

    一.前言 首先声明的是,这是我第一个与别人合作的.net项目,另一个人做的是后台管理,我做的前台,这是一个电子商务的系统,主要实现的功能是查看商品以及购物功能. 二.开始 首先看一下我截取的项目部分商 ...

  3. Objective-c 内存管理

    与 C 有一点类似,oc  需要使用 alloc 方法申请内存.不同的是,c 直接调用 free 函数来释放内存,而 oc 并不直接调用 dealloc 来释放.整个  oc 都使用对象引用,而且每一 ...

  4. 不直接用NSLog

    公司中不直接使用NSLog,而是利用宏定义自己的打印函数,将该打印函数写在项目的.pch文件中.调试的时候往往用到好多打印,但发布的时候确不需要.(一下是在公司中的一些处理) 自定义NSLog 一,固 ...

  5. 删除Lb重复的数,用La输出(顺序表)

    #include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCRMENT=10; type ...

  6. JavaEE学习之类加载器

    类装载子系统 在JAVA虚拟机中,负责查找并装载类型的那部分被称为类装载子系统. JAVA虚拟机有两种类装载器:启动类装载器和用户自定义类装载器.前者是JAVA虚拟机实现的一部分,后者则是Java程序 ...

  7. HDU1712-ACboy needs your help

    描述: ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profi ...

  8. nodebeginer

    最近对node开始感兴趣,知乎上朴灵推荐入门书籍,goddy翻译的node beginner. 貌似大家对深入浅出node.js评价都不错,以后可以考虑入手看看. 一口气看完了node beginne ...

  9. jbpmAPI-6

    第六章流程. 6.1. What is BPMN 2.0 业务流程模型和符号(BPMN)2.0规范是OMG规范,不仅定义了一个标准的业务流程的图形化表述(如BPMN 1. x),但现在还包括执行语义定 ...

  10. Aone新拉分支

    1.进入Aone新建项目 2.测试人员填huyangjun和husong 3.进入后拉分支 4.弄个日常普通环境 5.吧环境跑起,绑定Host就可以