题目链接:http://poj.org/problem?id=3264

Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 47515   Accepted: 22314
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 题目大意:给定一个数组,然后给一个区间[a,b],输出从a到b中的最大值和最小值的差。
RMQ 和 线段树 均可
RMQ代码:
 #include <stdio.h>
#define MAX(a,b) (a>b ? a:b)
#define MIN(a,b) (a>b ? b:a)
#define N 50010
int a[N],ma[N][],mi[N][]; void ST(int n)
{
int i,j;
for(i=;i<=n;i++)
mi[i][]=ma[i][]=a[i]; for (j = ; (<<j) <= n; j ++)
for (i = ; i + (<<j)- <= n; i ++)
{
ma[i][j]=MAX(ma[i][j-],ma[i+(<<(j-))][j-]);
mi[i][j]=MIN(mi[i][j-],mi[i+(<<(j-))][j-]);
}
} int rmq(int a,int b)
{
int k = ;
while((<<(k+)) <= b-a+) k++;
return MAX(ma[a][k],ma[b-(<<k)+][k])-MIN(mi[a][k],mi[b-(<<k)+][k]);
} int main()
{
int n,i,q,x,y;
while(scanf("%d %d",&n,&q)!=-)
{
for(i=;i<=n;i++)
scanf("%d",a+i);
ST(n);
for(i=;i<=q;i++)
{
scanf("%d %d",&x,&y);
printf("%d\n",rmq(x,y));
}
}
return ;
}
 

POJ 3264 区间最大最小值Sparse_Table算法的更多相关文章

  1. poj 3264 区间最大最小值 RMQ问题之Sparse_Table算法

    Balanced Lineup Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , %I64u Java ...

  2. POJ 3264 Balanced Lineup RMQ ST算法

    题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...

  3. POJ 3171 区间覆盖最小值&&线段树优化dp

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4715   Accepted: 1590 D ...

  4. 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分析:标 ...

  5. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  6. poj 3264 Balanced Lineup (RMQ)

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

  7. POJ 3264 Balanced Lineup(RMQ)

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

  8. Poj 3264 Balanced Lineup RMQ模板

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

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

随机推荐

  1. IOC(控制反转和依赖注入)之Autofac

    Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上也是很高的.于是,今天抽空研究了下它.下载地址:http://co ...

  2. mac 安装memcached服务

    使用homebrew安装,homebrew安装方法http://brew.sh/ 安装memcached服务 brew install memcached 配置开机启动(用brew安装之后下面会提示怎 ...

  3. JDBC 1

    Java 中的数据存储技术 在Java中,数据库存取技术可分为如下几类: JDBC直接访问数据库 JDO技术 第三方O/R工具,如Hibernate, ibatis 等 JDBC是java访问数据库的 ...

  4. 数据结构-Hash表

    实现: #ifndef SEPARATE_CHAINING_H #define SEPARATE_CHAINING_H #include <vector> #include <lis ...

  5. FCKEditor文件上传提示信息的汉化

    FCKEditor文件上传提示信息的汉化在FCKeditor中,虽然可以自动监测客户端语言,但是仍有小部分信息未能得到汉化.例如上传图片.Flash时,上传成功和上传失败的对话框提示信息均为英文,只要 ...

  6. HTTP状态代码

    1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101 (切换协议) 请求 ...

  7. maven的简单安装与配置

    什么是Maven? Maven可以被理解成"知识的积累",也可以被翻译为"专家".它是一个项目管理工具. 它的主要服务即源于java平台的项目构建.依赖管理和项 ...

  8. SharePoint 2013 开发——工作流架构

    博客地址:http://blog.csdn.net/FoxDave SharePoint 2013的工作流较之前有了不同,第一次真正地作为独立的服务的概念推出了.这意味着SharePoint工作流不再 ...

  9. 使用HttpOnly提升Cookie安全性

        在介绍HttpOnly之前,我想跟大家聊聊Cookie及XSS. 随着B/S的普及,我们平时上网都是依赖于http协议完成,而Http是无状态的,即同一个会话的连续两个请求互相不了解,他们由最 ...

  10. type safe printf

    在书里看到的,摘录如下: #include <iostream> #include <stdexcept> template<class T> struct is_ ...