Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 34140   Accepted: 16044
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 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

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

 
题解:线段树,向上更新区间最值
AC代码:
 #include <cstdio>
#include <cstring> #define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b) //宏定义提高效率 const int LEN = ; struct Seg
{
int left, right;
int ma, mi;
}seg[LEN*]; void buildt(int l, int r, int step)
{
seg[step].left = l;
seg[step].right = r;
seg[step].ma = ;
seg[step].mi = 0x7fffffff;
if (l == r)
return;
int mid = (l + r)>>;
buildt(l, mid, step<<);
buildt(mid+, r, step<<|);
} void pushup(int step) //向上更新
{
seg[step].ma = MAX(seg[step<<].ma, seg[step<<|].ma);
seg[step].mi = MIN(seg[step<<].mi, seg[step<<|].mi);
} void update(int l, int r, int height, int step)
{
if (l == seg[step].left && r == seg[step].right){
seg[step].mi = height;
seg[step].ma = height;
return;
}
if (seg[step].left == seg[step].right)
return;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
update(l, r, height, step<<);
else if (l > mid)
update(l, r, height, step<<|);
else{
update(l, mid, height, step<<);
update(mid+, r, height, step<<|);
}
pushup(step); //递归中更新完下一个节点后向上更新
} int queryma(int l, int r, int step) //求区间最大值
{
if (l == seg[step].left && r == seg[step].right){
return seg[step].ma;
}
if (seg[step].left == seg[step].right)
return ;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
return queryma(l, r, step<<);
else if (l > mid)
return queryma(l, r, step<<|);
else{
int a = queryma(l, mid, step<<);
int b = queryma(mid+, r, step<<|); //防止使用宏定义时多次调用queryma,先调用得到返回值,再比较返回值
return MAX(a, b);
}
} int querymi(int l, int r, int step) //求区间最小值
{
if (l == seg[step].left && r == seg[step].right){
return seg[step].mi;
}
if (seg[step].left == seg[step].right)
return 0x7fffffff;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
return querymi(l, r, step<<);
else if (l > mid)
return querymi(l, r, step<<|);
else{
int a = querymi(l, mid, step<<);
int b = querymi(mid+, r, step<<|); //同上
return MIN(a, b);
}
} int main()
{
int n, q;
scanf("%d %d", &n, &q);
buildt(, n, );
for(int i = ; i <= n; i++){
int t;
scanf("%d", &t);
update(i, i, t, );
}
for(int i = ; i < q; i++){
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", queryma(a, b, ) - querymi(a, b, ));
}
return ;
}

【POJ】3264 Balanced Lineup ——线段树 区间最值的更多相关文章

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

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

  2. POJ 3264 Balanced Lineup 线段树RMQ

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

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

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

  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. BZOJ-1699 Balanced Lineup 线段树区间最大差值

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

  8. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  9. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

随机推荐

  1. 如何将Oracle安装为Linux服务

    方法一:使用oracle自带的启动和关闭脚本 1. oracle用户修改/etc/oratab 文件: $ vi /etc/oratab orcl:/oracle/app/product/10.2.0 ...

  2. Javascript数组操作方法

    1.shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] ...

  3. matlab---边缘之sobel简单实例

    最近在项目中需要做一些图像边缘检测的工作,但是由于之前没接触过图像处理的相关知识,所以只得 在matlab里面对一些图像处理函数挨个挨个的试着用.在用的过程中在慢慢的明白了一些简单的图像处 理方法. ...

  4. Object-C自定义对象NSLog输入信息

    http://blog.cnrainbird.com/index.php/2012/07/19/object-c_zi_ding_yi_dui_xiang_nslog_shu_ru_you_yong_ ...

  5. #include <boost/shared_array.hpp>

    共享数组 共享数组的行为类型于共享指针.关键不同在于共享数组在析构时,默认使用delete[]操作符来释放所含的对象.因为这个操作符只能用于数组对象,共享数组必须通过动态分配的数组的地址来初始化.共享 ...

  6. Win8开发疑问与解答

    疑问:怎样获取开发者许可证 打开VS2012时,怎么在没有取得开发者许可证之前,屏蔽/跳过弹出的窗体“获取Windows8开发者许可证 你需要具有开发者许可证才能开发适用于......” 打开Blen ...

  7. 不管ACM是不是屠龙之技

    有一个目标,每天早上起床能让你保持斗志满满..找到自己的战场和归属. 这件事本身就是很难得的...是不是 ACM 并不重要. 你现在能从其他事情上获得这种体验么? -xiaodao

  8. asp.net application

    Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象.不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对 ...

  9. RDLC报表系列(六) 多图表-折线图和柱状图

    美好的一天开始了,这篇是RDLC系列的最后一篇文章,我的小项目也已经release,正在测试中. 1.新建demo3.aspx和demo3.rdlc文件 2.往rdlc文件中拖一个图标控件,在弹出的窗 ...

  10. Java日志管理

    首页 资讯 精华 论坛 问答 博客 专栏 群组 更多 ▼ 您还未登录 ! 登录 注册 JavaCrazyer的ItEye(codewu.com)技术博客   博客 微博 相册 收藏 留言 关于我   ...