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

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. oracle获取表和列的备注

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Runti ...

  2. JavaScript——Dom编程(2)

    ①.创建一个元素节点: var reference = document.createElement(element) createElement(): 按照给定的标签名创建一个新的元素节点. 方法只 ...

  3. python23种设计模式

      第一篇 Python与设计模式:前言 第二篇(23种设计模式) 创建类设计模式(5种) 单例模式.工厂模式.简单工厂模式.抽象工厂模式.建造者模式.原型模式 结构类设计模式(7种) 代理模式.装饰 ...

  4. java IO 对象流 反序列化和序列化

    例: 重点:需要序列化的对象必须实现Serializable接口 //需要序列化的对象 public class User implements Serializable { private Stri ...

  5. Alpha冲刺(三)

    Information: 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Details: 组员1(组长)柯奇豪 过去两天完成了哪些任务 ssm框架的使用并实现简单的数据处理 ...

  6. HTML、CSS、JavaScript网页制作从入门到精通 (刘西杰) pdf扫描版彩色版​

    html.css.JavaScript网页制作从入门到精通中从基础知识开始讲起,如html的基本标记.文字与段落标记.表格标记.超链接标记……同时介绍了目前流行的web标准与css网页布局实例,以及基 ...

  7. [python]模块及包

    一 .module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc&quo ...

  8. layui之弹出层--从父窗口传递数据到子窗口

    原文链接:https://blog.csdn.net/Code_shadow/article/details/80524633 var Index = layer.open({ title: &quo ...

  9. 简单使用postman

    一.get请求 获取学生信息接口文档内容: 简要描述: 获取学生信息接口 请求URL: http://ip/api/user/stu_info 请求方式: get 参数: 参数名 必选 类型 说明 s ...

  10. Mysql导入数据时-data truncated for column..

    在导入Mysql数据库时,发现怎么也导入不进去数据,报错: 查看表定义结构:可以看到comm 定义类型为double类型 原来是因为数据库文件中: 7369    smith    clerk     ...