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. 回文数猜想(hd1282)

    回文数猜想 Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数.任取一个正整数,如果不是回文数,将该数与他的倒序 ...

  2. 正式学习React(一) 开始学习之前必读

    为什么要加这个必读!因为webpack本身是基于node环境的, 里面会涉及很多路径问题,我们可能对paths怎么写!webpack又是怎么找到这些paths的很迷惑. 本文是我已经写完正式学习Rea ...

  3. 如何:对 Web 窗体使用路由

    配置用于路由的 ASP.NET 网站项目 1. 在应用程序的 Web.config 文件中,将 ASP.NET 路由程序集添加到 assemblies 元素,如下面的示例所示: <add ass ...

  4. PHP数组排序函数array_multisort()函数详解

    这个函数因为用到了,并且在网上找了半天终于找到了一个写的通俗易懂的文章,在这里分享给大家. 原文链接:http://blog.163.com/lgh_2002/blog/static/44017526 ...

  5. MFC 遍历FTP服务器目录相关

    CInternetSession* pSession; pSession = new CInternetSession;  //构造新的连接 CFtpConnection* pFtpCon; pFtp ...

  6. js设计模式

    http://www.csdn.net/article/2011-09-02/303983 阐明JavaScript设计模式.CSDN研发频道对此文进行了整理选取部分内容,供开发者学习.参考. 内容如 ...

  7. :gAudit

    http://www.doc88.com/p-0794369847693.html http://baike.baidu.com/link?url=pcOUfBpILuEAPFrBSsSU-6Vzg3 ...

  8. logstash grok正则调试

    logstash 正则调试: nginx 配置: log_format main '$remote_addr [$time_local] "$request" '; logstas ...

  9. 2.6. Statistical Models, Supervised Learning and Function Approximation

    Statical model regression $y_i=f_{\theta}(x_i)+\epsilon_i,E(\epsilon)=0$ 1.$\epsilon\sim N(0,\sigma^ ...

  10. UVA10869 - Brownie Points II(线段树)

    UVA10869 - Brownie Points II(线段树) 题目链接 题目大意:平面上有n个点,Stan和Ollie在玩游戏,游戏规则是:Stan先画一条竖直的线作为y轴,条件是必需要经过这个 ...