这里说几篇博客,建议从上到下看

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
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MOD = 1e9 + ; #define MemI(x) memset(x, -1, sizeof(x))
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x)); int dp_max[][], dp_min[][];
int n, m;
void ST()
{
int i, j;
for(i = ;i <= n;++i)
{
cin >> dp_max[i][];
dp_min[i][] = dp_max[i][];
}
//这里循环手动模拟就懂
for(j = ;( << j) <= n;++j)
for(i = ;i + ( << j) - <= n;++i)
{
//之前这里没注意长度是 j - 1,WA 了
dp_max[i][j] = max(dp_max[i][j - ], dp_max[i + ( << (j - ))][j - ]);
dp_min[i][j] = min(dp_min[i][j - ], dp_min[i + ( << (j - ))][j - ]);
}
} int RMQ(int l, int r)
{
int k = ;
while(( << (k + )) <= r - l + )
k++;
int a = max(dp_max[l][k], dp_max[r - ( << k) + ][k]);
int b = min(dp_min[l][k], dp_min[r - ( << k) + ][k]);
// cout << a << " " << b << endl;
return a - b;
} int main()
{
Mem0(dp_max);
MemM(dp_min);
cin >> n >> m;
ST();
int a, b;
while(m--)
{
cin >> a >> b;
cout << RMQ(a, b) << endl;
}
return ;
}

RMQ、POJ3264的更多相关文章

  1. RMQ、ST表

    ST表 \(\text{ST}\) 表是用于解决可重复贡献问题的数据结构. 可重复贡献问题:区间按位和.区间按位或.区间 \(\gcd\) .区间最大.区间最小等满足结合律且可重复统计的问题. 模板预 ...

  2. 【听说是线段树】bzoj1012 [JSOI2008]最大数maxnumber

    一眼看题目吓了一跳:这TM不就是单调队列吗,200000又怎样,大不了我二分嘛 系统提示:成功开启 手残模式 开始瞎写: #include <cstdio> ]; ]; int m,mod ...

  3. ACM训练计划建议(写给本校acmer,欢迎围观和指正)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

  4. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  5. [HDU 1806] Frequent values

    Frequent values Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. BZOJ3730 震波 和 BZOJ4372 烁烁的游戏

    "震波"题意 F.A.Qs Home Discuss ProblemSet Status Ranklist Contest 入门OJ ModifyUser  autoint Log ...

  7. ACM训练计划建议(转)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

  8. LCA算法笔记

    LCA,最近公共祖先,实现有多种不同的方法,在树上的问题中有着广泛的应用,比如说树上的最短路之类. LCA的实现方法有很多,比如RMQ.树链剖分等. 今天来讲其中实现较为简单的三种算法: RMQ+时间 ...

  9. 【转】Senior Data Structure · 浅谈线段树(Segment Tree)

    本文章转自洛谷 原作者: _皎月半洒花 一.简介线段树 ps: _此处以询问区间和为例.实际上线段树可以处理很多符合结合律的操作.(比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[ ...

随机推荐

  1. php手机号正则

    preg_match("/^1[34578]{1}\d{9}$/", $phone)

  2. 解决IE与FF 中 input focus 光标移动在最后的方案

    只要把input元素的id传进来即可 function moveCursor(id)  { var id = document.getElementById(id); id.focus(); var  ...

  3. Mybaties核心配置文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC &q ...

  4. MVC异常过滤器在三种作用范围下的执行顺序

    对于一般过滤器(即:除了IExceptionFilter ),当同时在Controller和Action中都设置了同一个过滤器后(例如IActionFilter),执行顺序一般是由外到里,即“全局”- ...

  5. C# How To Read .xlsx Excel File With 3 Lines Of Code

    Download Excel.zip - 9.7 KB Download ExcelDLL.zip - 3.7 KB Introduction We produce professional busi ...

  6. Diameter协议摘要

    ---------选择同学整理文档 1.   协议概述 Diameter协议主要为应用程序提供认证.鉴权.计费框架,即AAA,并支持本地AAA和漫游场景下的AAA. 1.1.  特点介绍 以前的AAA ...

  7. 个人项目:wc程序(java)

    Github项目地址:https://github.com/jat0824/wc.git 项目相关要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行 ...

  8. 【单例模式】Singleton pattern

    前言:有很多时候,在一个生命周期中我们只要一个对象就可以了,比如:线程池,缓存,对话框,日志,显卡驱动等等.如果造出多个实例,就会导致许多问题产生,例如:程序的行为异常.资源使用过量,或者说不一致的结 ...

  9. 前端框架 json 返回值

    layui: string strJson = "{\"code\": \"0\",\"msg\": \"\" ...

  10. super() 的入门使用

    在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如:     1 2 3 4 5 ...