Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 38942   Accepted: 18247
Case Time Limit: 2000MS

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 andQ.

Lines 2..N+1: Line i+1 contains a single integer that is the height of cowi

Lines N+2..N+Q+1: Two integers A and B (1 ≤A
BN), representing the range of cows from A toB inclusive.

Output

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 Input

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

Sample Output

6
3
0

Source

USACO 2007 January Silver



题目大意:在一定区间内给定一些数。要求求出在某一区间内最大值和最小值的差。

线段树的题目。对于这道题目,既然是求最大值和最小值的差,那么必定要在区间里面存放最大值和最小值。同一时候这道题目仅仅是单纯的要求查询区间内的差值,不须要进行更新。

#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b? a:b
#define INF 99999999
#define N 50005
struct tree{
int l,r,maxi,mini;
int mid(){
return l+r>>1;
}
}tree[N<<2];
int ma=-INF,mi=INF;
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].maxi=-INF;
tree[root].mini=INF; //初始化最大最小值
if(l==r){ return;
}
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
}
void update(int i,int z,int root)
{ if(tree[root].l==tree[root].r){
tree[root].mini=tree[root].maxi=z;
return;
}
tree[root].maxi=max(tree[root].maxi,z);
tree[root].mini=min(tree[root].mini,z); //每次都更新最大和最小值
if(i<=tree[root].mid())update(i,z,root<<1); //这里将i下面的节点所有更新。 而i与mid 是有关系的。
else update(i,z,root<<1|1);
}
void Query(int l,int r,int root)
{
if(tree[root].mini>=mi&&tree[root].maxi<=ma)return;
if(l==tree[root].l&&r==tree[root].r){
mi=min(mi,tree[root].mini);
ma=max(ma,tree[root].maxi);
return;
}
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid){
Query(l,r,root<<1);
}
else if(l>mid){
Query(l,r,root<<1|1);
}
else {
Query(l,mid,root<<1);
Query(mid+1,r,root<<1|1);
}
return ;
}
int main()
{
int n,Q,cow[200005],a,b;
int i,j,k;
while(scanf("%d%d",&n,&Q)!=EOF)
{
build(1,n,1);
for(i=1;i<=n;i++)
{
scanf("%d",&cow[i]);
update(i,cow[i],1); //对于第i个数字进行插入
} while(Q--)
{
scanf("%d%d",&a,&b);
ma=-INF;
mi=INF;
Query(a,b,1);
printf("%d\n",ma-mi);
}
}
return 0;
}

poj 3246 Balanced Lineup(线段树)的更多相关文章

  1. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  2. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  3. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  4. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  5. POJ 3264 Balanced Lineup (线段树)

    Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...

  6. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  7. Poj 3246 Balanced Lineup(线段树基础)

    依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...

  8. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  9. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

随机推荐

  1. Hibernate连接MySQL

    1 下载hibernate-3.6.0 Final.zip到任意目录,解压缩后得到hibernate目录 2 下载slf4j-1.7.13.zip到任意目录,解压缩后得到slf4j-1.7.13 3  ...

  2. SqlBulkCopy-从 bcp 客户端收到一个对 colid 1 无效的列长度

    经过研究,问题是因为tatatable中的colid列1的数据字段长度超过了数据表中定义的字段长度. 解决方法就是将数据库该字段的长度增大问题就解决了.

  3. 微信公众平台开发小记(ASP.NET)

    微信的好东西,提供了很大的平台去发挥,公司最近推出微信公众账号,也接触了一些东西, 最终决定用asp.net来开发服务端程序. 微信公众平台的API很简单,利用XML来规范格式,并且所有的数据都在CD ...

  4. Solidworks如何等比例缩小放大模型

    比如初始化的模型,笔记本长度只有120mm,实际上应该是3倍左右   右击特征,勾选模具工具,然后可以发现多出来一个页面   点击比例缩放,选中要缩放的特征,设置比例,然后打钩   可以发现已经缩放到 ...

  5. python gevent使用例子

    python gevent使用例子 from gevent.pool import Pool POOL_SIZE = 100 def process(func, param1_list, param2 ...

  6. JMeter 八:录制脚本--使用Jmeter自带的代理服务器

    参考:http://jmeter.apache.org/usermanual/jmeter_proxy_step_by_step.pdf http://jmeter.apache.org/userma ...

  7. [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读

    ---------------------------------------------------------------------------------------------------- ...

  8. html 里面的 role 属性是什么意思

    role="button" role是什么意思? html 里面的 role 属性是什么意义和用途 使用role属性告诉辅助设备(如屏幕阅读器)这个元素所扮演的角色,属于WAI-A ...

  9. http keep-alive 解释

    1.概念 keep-alive示例: keep-alive模式(又称持久连接.连接重用)时,keep-alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,keep-alive功 ...

  10. Java基础——线程总结

    Java基础--线程总结 一.线程是什么? 线程:一个程序里不同的运行路径. 二.怎样创建线程? 两种方法创建线程: 第一种 (1)定义详细功能类实现Runnable接口,能够多次调用而实现数据共享 ...