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

------------------------------------------------------------------------------------------------------------

这里介绍一维的,二维看上面第三篇博客

1)关于RMQ和ST

         简单来说,RMQ(Range Minimum/Maximum Query),即区间最值查询,是一个查询!而ST(Sparse Table)。。。就是打表

2)dp表

       RMQ说到底就是打这个表。我们开一个二维 dp[i][j],它表示的是从第 i 个数开始算,长度为 2^j 这个区间的最值,例如 1, 2, 3, 4, 5,那么 dp[2][2] 表示的是在区间 2, 3, 4, 5, 这 2^2 个数的最值。因为是 2 的次方,所以这些数的个数一定为偶数。

       这里我们设题目给的数为 a[i]

       1、dp[i][0] = a[0];

       2、因为数为偶数,所以每个长度都可以分成两半,长度都为 2^(j - 1), 一个从 i 开始,到 i + 2^(j - 1) - 1 结束(i 自己也算一个长度),另一个从  i + 2^(j - 1) 开始,即 dp[i][j] = max(dp[i][j - 1], dp[i + (1 << j)][j - 1])

3)查询

  例如查询 1, 2, 3, 4, 5 我们可以查找区间 1, 2, 3, 4 和区间 2, 3, 4, 5 的最值。即以长度 j 为标准,查询区间为 r - l,长度为 r - l + 1,就让 j <= r - l + 1,并使 j 最大就可以,这样只要求出 j ,就可以算 ans = max(dp[l][j], dp[r - (1 << j) + 1][j])

----------------------------------------------------------------------------------------------------

下面题目

POJ3264   http://poj.org/problem?id=3264

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
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <iomanip>
 6 #include <string>
 7 #include <sstream>
 8 #include <algorithm>
 9 #include <stack>
10 #include <queue>
11 #include <set>
12 #include <map>
13 
14 using namespace std;
15 
16 typedef long long LL;
17 const int INF = 0x3f3f3f3f;
18 const int MAXN = 1005;
19 const int MOD = 1e9 + 7;
20 
21 #define MemI(x) memset(x, -1, sizeof(x))
22 #define Mem0(x) memset(x, 0, sizeof(x))
23 #define MemM(x) memset(x, 0x3f, sizeof(x));
24 
25 int dp_max[50005][20], dp_min[50005][20];
26 int n, m;
27 void ST()
28 {
29     int i, j;
30     for(i = 1;i <= n;++i)
31     {
32         cin >> dp_max[i][0];
33         dp_min[i][0] = dp_max[i][0];
34     }
35     //这里循环手动模拟就懂
36     for(j = 1;(1 << j) <= n;++j)
37         for(i = 1;i + (1 << j) - 1 <= n;++i)
38         {
39             //之前这里没注意长度是 j - 1,WA 了
40             dp_max[i][j] = max(dp_max[i][j - 1], dp_max[i + (1 << (j - 1))][j - 1]);
41             dp_min[i][j] = min(dp_min[i][j - 1], dp_min[i + (1 << (j - 1))][j - 1]);
42         }
43 }
44 
45 int RMQ(int l, int r)
46 {
47     int k = 0;
48     while((1 << (k + 1)) <= r - l + 1)
49         k++;
50     int a = max(dp_max[l][k], dp_max[r - (1 << k) + 1][k]);
51     int b = min(dp_min[l][k], dp_min[r - (1 << k) + 1][k]);
52 //    cout << a << " " << b << endl;
53     return a - b;
54 }
55 
56 int main()
57 {
58     Mem0(dp_max);
59     MemM(dp_min);
60     cin >> n >> m;
61     ST();
62     int a, b;
63     while(m--)
64     {
65         cin >> a >> b;
66         cout << RMQ(a, b) << endl;
67     }
68     return 0;
69 }
现在所有的不幸都是以前不努力造成的。。。
原文地址:https://www.cnblogs.com/shuizhidao/p/9356327.html