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. linux shell if语句

    #!/bin/bash read -p "please input Y/N" keyWord if [ "$keyWord" == "Y" ...

  2. SQL Server dbcc checkdb 做了什么。

    第一步: 读取系统元数据.读完这些数据后dbcc checkdb 就知道自己要检测的是一个怎样的数据库了.如果在这一步就出错了.dbcc 就直接出错 了.不会再运行下去. 第二步: 在dbcc che ...

  3. NFC 与点对点应用

    http://wenku.baidu.com/view/6a7623a28762caaedc33d426.html

  4. 跟我开发NSP(网上查询平台):如何选择开发项目

    我想通过一个真实的项目开发的全过程,记录一下开发过程的点点滴滴,记录一下过程中的前思后想.这个全过程包括,如何选择项目.如何分析项目.如何组织项目开发.如何设计开发流程.如何设计软件的总体架构.如何建 ...

  5. Unix/Linux环境C编程入门教程(34) 编程管理系统中的用户

    1.用户管理相关函数介绍 geteuid(取得有效的用户识别码) 相关函数 getuid,setreuid,setuid 表头文件 #include<unistd.h> #include& ...

  6. NYOJ 46-最少乘法次数(数论)

    题目地址:pid=46">NYOJ 46 思路:能够化成二进制来求解.结果是最高位的位数-1+最高位后面1的个数.比如:对于3.它的二进制代码为11,就是用这个最高位(2-1)加上后面 ...

  7. Office 2010 & SharePoint 2010 Service Pack 2现在可用啦

    Access 2010 Runtime SP2 KB2687444 32-bit 64-bit Duet Enterprise for Microsoft SharePoint and SAP SP2 ...

  8. ThreadLocal 在web环境下使用的边界问题

    ThreadLocal 相关分析,请查看http://wangxinchun.iteye.com/blog/1884228 另外一个必须要提的点是: ThreadLocal在线程池环境下的使用. 比如 ...

  9. [代码]Java后台推送消息到IOS前端

    PayLoad payLoad = new PayLoad(); payLoad.addAlert("test");    //手机端的提示消息 payLoad.addBadge( ...

  10. 【27前端】base标签带有href属性会让chrome里的svg元素url失效

    一个chrome的问题,但具体原因不明. 触发条件:chrome浏览器base标签里href属性有值的时候 触发问题:svg里面的元素如果有用url的滤镜和模糊,则会失效,在firefox里和IE10 ...