POJ3264(KB7-G RMQ)

Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K

otal Submissions: 52651 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

 1 //2017-05-17
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <cmath>
 7
 8 using namespace std;
 9
10 const int N = 50005;
11 int a[N], DPmin[N][20], DPmax[N][20];
12
13 void init(int n)
14 {
15     for(int j = 1; j<=(int)log2(n); j++)
16       for(int i = 1; i<=n; i++){
17           DPmax[i][j] = DPmax[i][j-1];
18           DPmin[i][j] = DPmin[i][j-1];
19           if(i+(1<<j)-1 <= n){
20               DPmax[i][j] = max(DPmax[i][j-1], DPmax[i+(1<<(j-1))][j-1]);
21               DPmin[i][j] = min(DPmin[i][j-1], DPmin[i+(1<<(j-1))][j-1]);
22           }
23       }
24 }
25
26 int query(int l, int r)
27 {
28     int k = (int)log2(r-l+1);
29     return max(DPmax[l][k], DPmax[r-(1<<k)+1][k])-min(DPmin[l][k], DPmin[r-(1<<k)+1][k]);
30 }
31
32 int main()
33 {
34     int n, q;
35     while(scanf("%d%d", &n, &q)!=EOF)
36     {
37         for(int i = 1; i <= n; i++){
38             scanf("%d", &DPmax[i][0]);
39             DPmin[i][0] = DPmax[i][0];
40         }
41         init(n);
42         int l, r;
43         while(q--)
44         {
45             scanf("%d%d", &l, &r);
46             printf("%d\n", query(l, r));
47         }
48     }
49
50     return 0;
51 }
时间: 2024-07-29 07:46:29

POJ3264(KB7-G RMQ)的相关文章

POJ3264 Balanced Lineup RMQ 线段树

求区间内最大数和最小数的差,用两棵线段树,一个维护区间最大值,一个维护区间最小值. #include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algori

[POJ3264]Balanced Lineup(RMQ, ST算法)

题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的最值一定是这两个区间合并后的最值,这条性质决定了这个dp子问题的重叠.可以利用这个性质预处理出这张表,只不过步长是2的幂次. 查询的时候也是如此,但是未必会精准地选中两个区间,不要紧,因为两个区间重叠的部分也会被自动算在求最值的内部.这个时候如果算的是区间和的话,要减去这一部分.(区间和的话直接用前

浅谈【树】的数据生成

本人水平有限,题解不到为处,请多多谅解 本蒟蒻谢谢大家观看 如何让一份自己写的程序跑一遍随机造的数据,这时我们要用到 数据生成. 本文只介绍树的生成(我是不会告诉你我现在只会树) 题目:传送门 普通树的生成:(随机生成10个数据) 注意:最后第10个数据要手动调试,虽然我也不知道为什么. 以下上半部分是树,下半部分是题目要求. code: 1 #include<iostream> 2 #include<stdlib.h> 3 #include<time.h> 4 inl

(WAWAWAWAWAWAW) G. Periodic RMQ Problem

没有联通门 : Codeforces G. Periodic RMQ Problem /* Codeforces G. Periodic RMQ Problem MMP 什么动态开点线段树啊 ... YY了个非常可行的做法 码完后交一下发现RE了几个点.. 就思考哪里有问题 突然之间, 老泪纵横.. MMP 我写了这是个什么玩意啊 仔细想想我TM不就是写了个全空间的主席树吗....脑子有病啊.. 静言思之, 躬自悼矣..反是不思,亦已焉哉 不写啦! */ #include <cmath> #i

RMQ、POJ3264

这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh666666/article/details/80448284 https://www.cnblogs.com/kuangbin/p/3227420.html ----------------------------------------------------------------------

poj--3264 Balanced Lineup(裸的RMQ)

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 rang

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

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

G - Balanced Lineup - poj3264(区间查询)

题意:给你一组值,然后询问某个区间的最大值和最小值得差 分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了 ****************************************************************** #include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;#define Ls

RMQ //poj3264,poj3368

http://poj.org/problem?id=3264 #include<iostream> #include<cstdio> #include<string> #include<cmath> #include<cstring> #define M 1000000 + 50 using namespace std; int a[M]; int maxs[M][100]; int mins[M][100]; int QueryMax(int