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. linux 配置 mail server

    一.配置yum安装工具 ①  进入yum目录 [root@bj ~]# cd /etc/yum.repos.d ②  配置yum.repo [root@bj yum.repos.d]# cprhel- ...

  2. Indy的TCPServer到底能支持多少个连接

    最近一个项目,最开始使用IdTcpServer,在大压力测试的时候,只连接了800个多一点的客户端(每个客户端连接上之后每秒钟发送一个几十字节的报文,服务器应答).但是持续的时间不会超过10分钟,服务 ...

  3. Button UI Kit CSS3美丽Buttonbutton

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  4. 五、Linux/UNIX操作命令积累【cp、mv、cat、grep、ps】

    在使用Linux/UNIX下,常常会使用文本界面去设置系统或操作系统,作者本人在工作的过程也在不断接触这方面的命令,所以为此特酝酿.准备.開始了本文的编写.本文主要记录自己平时遇到的一些Linux/U ...

  5. Xamarin 安装教程 支持Visual Studio 2013

    本文的前提是你已经正确的安装了VS 2013. 本文的全部步骤在Win7 Ultimate 64系统上測试通过.支持VS 2013,我用的版本号是VS 2013 update2. 安装 1.      ...

  6. Latin1的所有字符编码

    ISO-8859-1 (ISO Latin 1) Character Encoding Contents The characters at a glance Character codes and ...

  7. win7+vs2008+opencv

    1.下载安装VS2008,然后直接下载opencv的windows的安装版, 2.把opencv解压出来,我的路径为:D:\Program\opencv 3.配置PATH:电脑--属性--高级系统设置 ...

  8. ThinkPHP 的模型使用详细介绍--模型的核心(七)

    原文:ThinkPHP 的模型使用详细介绍--模型的核心(七) 注意:本节是ThinkPhp框架对数据操作的核心处理部分 大家还是在这里看清楚可以将其剪切放到代码编辑器中查看 本章节给大家着重介绍模型 ...

  9. grep与正则表达式,grep、egrep和fgrep

    grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串. ...

  10. swfupload组件后台获取中文文件名称乱码的问题解决

       问题描写叙述:用swfupload上传文件,含有中文名称的文件上传会报错,我用的是获取FileItem对象,用FileItem对象的getName()方法获取文件名会乱码,试着用request. ...