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

id=3264

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

PS:

百度百科RMQ:http://baike.baidu.com/view/1536346.htm?fr=aladdin

RMQ:http://blog.csdn.net/zztant/article/details/8535764

代码例如以下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
const int MAXN = 100117;
int n,query;
int num[MAXN]; int F_Min[MAXN][20],F_Max[MAXN][20]; void Init()
{
for(int i = 1; i <= n; i++)
{
F_Min[i][0] = F_Max[i][0] = num[i];
} for(int i = 1; (1<<i) <= n; i++) //按区间长度递增顺序递推
{
for(int j = 1; j+(1<<i)-1 <= n; j++) //区间起点
{
F_Max[j][i] = max(F_Max[j][i-1],F_Max[j+(1<<(i-1))][i-1]);
F_Min[j][i] = min(F_Min[j][i-1],F_Min[j+(1<<(i-1))][i-1]);
}
}
} int Query_max(int l,int r)
{
int k = (int)(log(double(r-l+1))/log((double)2));
return max(F_Max[l][k], F_Max[r-(1<<k)+1][k]);
} int Query_min(int l,int r)
{
int k = (int)(log(double(r-l+1))/log((double)2));
return min(F_Min[l][k], F_Min[r-(1<<k)+1][k]);
} int main()
{
int a,b;
scanf("%d %d",&n,&query);
for(int i = 1; i <= n; i++)
scanf("%d",&num[i]);
Init();
while(query--)
{
scanf("%d %d",&a,&b);
//printf("区间%d到%d的最大值为:%d\n",a,b,Query_max(a,b));
//printf("区间%d到%d的最小值为:%d\n",a,b,Query_min(a,b));
printf("%d\n",Query_max(a,b)-Query_min(a,b));
}
return 0;
}

POJ 3264 Balanced Lineup(RMQ_ST)的更多相关文章

  1. POJ 3264 Balanced Lineup(RMQ)

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

  2. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  3. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

  4. POJ 3264 Balanced Lineup(ST模板)

    链接:http://poj.org/problem?id=3264 题意:给n个数,求一段区间L,R的最大值 - 最小值,Q次询问 思路:ST表模板,预处理区间最值,O(1)复杂度询问 AC代码: # ...

  5. poj 3264 Balanced Lineup (线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42489   Accepted: 20000 ...

  6. poj 3264 Balanced Lineup(RMQ裸题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 43168   Accepted: 20276 ...

  7. POJ 题目3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39046   Accepted: 18291 ...

  8. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

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

随机推荐

  1. Android(java)学习笔记160:开发一个多界面的应用程序之清单文件

    清单文件的重要参数:     <intent-filter>             代表的应用程序的入口界面           <action android:name=&quo ...

  2. dom和bom是什么?

    DOM:文档对象模型,描述了处理网页内容的方法和接口.最根本对象是document(window.document). 由于DOM的操作对象是文档,所以DOM和浏览器没有直接关系. 部署在服务器上的文 ...

  3. 安装 Zend Studio 报错:0x80070666

    出现 0x80070666 报错时 查看日志文件,发现调用VC14(即:Microsoft Visual C++ 2015 Redistributable)时,出错返回0x666 先卸载原有的VC14 ...

  4. js div大小随意伸缩

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. jqury 延迟方法

    $("button").click(function(){    $("#div1").delay("slow").fadeIn();    ...

  6. 牛客noip前集训营(第一场)提高T1

    链接:https://www.nowcoder.com/acm/contest/172/A来源:牛客网 题目描述 小N得到了一个非常神奇的序列A.这个序列长度为N,下标从1开始.A的一个子区间对应一个 ...

  7. python基础(一)—— 核心数据类型

    Hello World程序 [root@mysql ~]# python3 Python 3.6.5 (default, Jul  8 2018, 11:41:23) [GCC 4.4.7 20120 ...

  8. python基础知识08-类定义、属性、初始化和析构

    1.类的定义 class 类 是独立存放变量(属性/方法)的一个空间. 每个实例都是一个独立的变量空间.不同实例之间的空间互相不可见. 一个实例的特征,就是属性. 定义在类中的私有属性也可以被子类继承 ...

  9. maven添加本地jar包到本地仓库

    1 进入jar包所在文件夹,进入cmd命令 2 执行命令 mvn install:install-file -Dfile=ojdbc6.jar -DgroupId=com.oracle -Dartif ...

  10. 有关OEP脱壳

    首先补充: OEP:(Original Entry Point),程序的入口点,软件加壳就是隐藏了OEP(或者用了假的OEP), 只要我们找到程序真正的OEP,就可以立刻脱壳. PUSHAD (压栈) ...