Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 34522   Accepted: 16224
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

经ysj一说我也准备噜线段树了 那天下午他来给我讲了一下线段树。先敲个模板再说。。

题意是找某个区间的最大值和最小值的差值。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define LL long long
using namespace std;
const int INF=1<<27;
const int maxn=200010;
LL minn[maxn],maxx[maxn];
void update(LL root,LL l,LL r,LL p,LL v)//单点更新
{
if(l==r) maxx[root]=v;minn[root]=v;
if(l<r)
{
LL mid=(l+r)/2;
if(p<=mid) update(root*2,l,mid,p,v);
else update(root*2+1,mid+1,r,p,v);
maxx[root]=max(maxx[root*2],maxx[root*2+1]);
minn[root]=min(minn[root*2],minn[root*2+1]);
}
}
LL query_min(LL root,LL l,LL r,LL ql,LL qr)
{
LL mid=(l+r)/2,ans=INF;
if(ql<=l&&qr>=r) return minn[root];
if(ql<=mid) ans=min(ans,query_min(root*2,l,mid,ql,qr));
if(qr>mid) ans=min(ans,query_min(root*2+1,mid+1,r,ql,qr));
return ans;
}
LL query_max(LL root,LL l,LL r,LL ql,LL qr)
{
LL mid=(l+r)/2,ans=-INF;
if(ql<=l&&qr>=r) return maxx[root];
if(ql<=mid) ans=max(ans,query_max(root*2,l,mid,ql,qr));
if(qr>mid) ans=max(ans,query_max(root*2+1,mid+1,r,ql,qr));
return ans;
}
int main()
{
int N,Q,i,v;
while(~scanf("%lld%lld",&N,&Q))
{
for(i=1;i<=N;i++)
{
scanf("%lld",&v);
update(1,1,N,i,v);
}
while(Q--)
{
int ql,qr;
scanf("%lld%lld",&ql,&qr);
printf("%lld\n",query_max(1,1,N,ql,qr)-query_min(1,1,N,ql,qr));
}
}
return 0;
}

POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)的更多相关文章

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

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

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

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

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

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

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

  5. POJ 3264 Balanced Lineup 线段树RMQ

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

  6. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

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

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

  8. POJ - 2828 Buy Tickets (段树单点更新)

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  9. Poj 3264 Balanced Lineup RMQ模板

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

  10. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 【Demo 0008】标签控制器

    本章学习要点:       1.  了解标签控制器基础知识;       2.  掌握标签控制器层次结构;       3.  掌握标签控制器基本用法;       4.  掌握自定义标签控制器:   ...

  2. Processing_百度百科

    Processing_百度百科 Processing

  3. 如何配置Git支持大小写敏感和修改文件名中大小写字母呢?(转)

    1. 在新建代码文件时,不注意把文件名应该小小写搞错了2. 文件已经push到远程了3. 在windows下面将文件名字改为全小写 改好后,在Git中没有任何反应,使用git status时,如果遇到 ...

  4. git for windows (又名 msysgit)如何记住用户名和密码

    创建存储用户名密码的文件 在home文件夹,一般是 C:\Documents and Settings\Administrator 下建立文件 .git-credentials (windows下不允 ...

  5. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException

    1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -he ...

  6. About VirtualBoxImages.com

    About VirtualBoxImages.com | VirtualBoxImages.com About VirtualBoxImages.com About: VirtualBoxImages ...

  7. mongodb时间戳转换成格式化时间戳

    db.pay_order.find({"id":"5332336532"},{"tradeNo":true,"status&quo ...

  8. YUV格式具体解释

    YUV是指亮度參量和色度參量分开表示的像素格式,而这样分开的优点就是不但能够避免相互干扰,还能够减少色度的採样率而不会对图像质量影响太大.YUV是一个比較笼统地说法,针对它的详细排列方式,能够分为非常 ...

  9. 因特网的IP协议是不可靠无连接的,那为什么当初不直接把它设计为可靠的?

    因特网使用的IP协议是无连接的,因此其传输是不可靠的. 这样easy使人们感到因特网非常不可靠,那为什么当初不直接把它设计为可靠的? 先打一个例如.邮局寄送的平信非常像无连接的IP数据报.每封平信可能 ...

  10. nginx做下载限速

    nginx做下载限速-szszszsz-ChinaUnix博客 nginx做下载限速 2009-12-25 14:34:57 分类: 系统运维 nginx做下载服务器,在性能上满足需求.自带limit ...