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

USACO 2007 January Silver

简单的区间求最值。

Problem: 3264

User: ksq2013

Memory: 1736K

Time: 3829MS

Language: G++

Result: Accepted

#include<cstdio>
#include<iostream>
using namespace std;
int n,q,maxnum[201000],minnum[201000];
void build(int s,int t,int k)
{
if(!(s^t)){
scanf("%d",&maxnum[k]);
minnum[k]=maxnum[k];
return;
}int m=(s+t)>>1;
build(s,m,k<<1);
build(m+1,t,k<<1|1);
maxnum[k]=max(maxnum[k<<1],maxnum[k<<1|1]);
minnum[k]=min(minnum[k<<1],minnum[k<<1|1]);
}
int query1(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return maxnum[k];
int m=(s+t)>>1,res=0;
if(l<=m)res=query1(s,m,k<<1,l,r);
if(m<r)res=max(res,query1(m+1,t,k<<1|1,l,r));
return res;
}
int query2(int s,int t,int k,int l,int r)
{
if(l<=s&&t<=r)return minnum[k];
int m=(s+t)>>1,res=1000100;
if(l<=m)res=query2(s,m,k<<1,l,r);
if(m<r)res=min(res,query2(m+1,t,k<<1|1,l,r));
return res;
}
int main()
{
scanf("%d%d",&n,&q);
build(1,n,1);
for(;q;q--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query1(1,n,1,a,b)-query2(1,n,1,a,b));
}
return 0;
}

poj 3264的更多相关文章

  1. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  2. POJ 3264 Balanced Lineup(RMQ)

    点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...

  3. POJ 3264 RMQ裸题

    POJ 3264 题意:n个数,问a[i]与a[j]间最大值与最小值之差. 总结:看了博客,记下了模板,但有些地方还是不太理解. #include<iostream> #include&l ...

  4. poj 3264 & poj 3468(线段树)

    poj 3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 求任一区间的最大值和最小值的差 #include<io ...

  5. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  6. poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】

    题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...

  7. Poj 3264 Balanced Lineup RMQ模板

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

  8. poj 3264(线段树)

    http://poj.org/problem?id=3264 初学线段可以做的水题,也是线段树的基础运用.也是我的第一个线段树的题. 题意:在区间范围内的最大值减去最小值 思路:线段树记录下每个区间内 ...

  9. POJ——3264线段树

    题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...

  10. POJ 3264 区间最大最小值Sparse_Table算法

    题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total ...

随机推荐

  1. SharePoint 2013 Designer 自定义操作菜单

    众所周知,我们在SharePoint的二次开发中,经常会添加ECB菜单或者Ribbon菜单,通常我们会采取Feature的方式去添加一个Xml,或者采取JavaScript的方式,当然,除此之外,还可 ...

  2. Understanding the Uncertain Geographic Context Problem

    "The areal units (zonal objects) used in many geographical studies are arbitrary, modifiable, a ...

  3. umeng 渠道统计 android

    1.配置AndroidManifest.xml,添加权限 <uses-permission android:name="android.permission.ACCESS_NETWOR ...

  4. Windows 7 安装.net framework 4.0 失败,错误HRESULT 0xc8000222解决办法

    今天在客服那里发现一个比较奇怪的错误,在客服机子上安装.NET Framework4.0时,出现如下错误:HRESULT 0xc8000222 百度了下原因,原来是win7自动更新造成的.原文网址:h ...

  5. 谈谈用ASP.NET开发的大型网站有哪些架构方式(成本)

    在上篇文章里(http://www.cnblogs.com/ms0017/archive/2011/07/26/2117676.html),列举了国内外用ASP.NET开发的大型网站有哪些.最后提到了 ...

  6. 让vc2010的项目在vc2012也能直接使用,而不必修改PlatformToolSet

    在Visual Studio 2010新建的项目到2012里打开会要求修改PlatformToolset的值,从v100改为v110.如果这个项目需要进版本管理(VCS,如git, svn),这将造成 ...

  7. Apache的Order Allow,Deny 详解

    Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是: O ...

  8. spring filter过滤器

    1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...

  9. 创建mysql存储过程,调用 及删除

    //创建表 create table test ( aid ) primary key auto_increment , nickname ), addtime ) ) //查看表结构 show co ...

  10. [SqlServer]创建链接服务器

    把一个数据库中数据表中的内容,从一个SQL SERVER服务器 导出到另一个SQL Server服务器 不同服务器数据库之间的数据操作 --创建链接服务器  exec sp_addlinkedserv ...