题目链接: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. 6.1.2Linux下Socket编程

    tcp简单实验 server.c #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include ...

  2. routeProvider

    In a previous post about testing I mentioned that route resolves can make authoring unit tests for a ...

  3. JSON初步

    1.什么是JSON (1)JSON(Java Script Object Notation)是一种轻量级的数据交换语言, 以文本字符串为基础,且易于让人阅读 XML就是一个重量级的数据交换语言 (2) ...

  4. 关键字const

    const关键字常和指针一起使用. 1,const给读代码的人传达非常有用的信息.比如一个函数的参数是const char *,你在调用这个函数时就可以放心地传给它char *或const char ...

  5. JS获取上传文件的名称、格式、大小

    <input id="File1" type="file" onchange="checkFile(this)" /> 方式一) ...

  6. SharePoint 2013 企业搜索架构示例

    博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,我们来看一下企业中对于不同规模SharePoint搜索的场的架构是什么样的. 对于搜索场的规模, ...

  7. JS学习第一课

    1.js 按照编写顺序执行 2.输出使用document.write. 3.申明数组 var array = [1,2,3,5] ;  var arrStr = ["sgsg",& ...

  8. Ubuntu 14.10 下安装navicat

    1 下载navicat,网址http://www.navicat.com.cn/download,我下载的是navicat111_premium_cs.tar.gz 2 解压到合适的位置 3 进入解压 ...

  9. Note_Master-Detail Application(iOS template)_04_ YJYMasterViewController.h

    //YJYMasterViewController.h #import <UIKit/UIKit.h> @classYJYDetailViewController; #import < ...

  10. 在windows上搭建ftp服务

    在控制面板->程序和功能->打开或关闭Windows功能中开启ftp和IIS信息服务管理器 在控制面板->管理工具中打开Internet信息服务管理器->添加ftp站点 建好之 ...