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

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. Web大文件上传控件-示例更新-Xproer.HttpUploader6.2

    版权所有 2009-2016荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  2. touchmove和touchend的使用

    touchstart:当手指触摸屏幕时触发:即使已经有一个手指放在了屏幕上也会触发.touchmove:当手指在屏幕上滑动时连续的触发.在这个事件发生期间,调用preventDefault()可阻止滚 ...

  3. 系统架构一:snmp+mrtg服务器监控

    //@author:yuan<turing_zhy@163.com> 码字不易,转载请注明出处 #================================== 开始,服务器准备   ...

  4. 阿里 vs. 腾讯,谁的收购更有眼光?

    近年来我们国内企业高速发展,各大集团纷纷收购其他公司发展自己,在这么多的集团收购里面尤其以阿里巴巴和腾讯的收购引人注目.在2014年里阿里巴巴先后投资了中信,美国奢侈品电子商务lstdibs,高德,优 ...

  5. 七月小说网 Python + GraphQL (三)

    概述 后台数据库几个基本表基本搭建完毕,看了下Github Develop的V4 Api抛弃了RESTful,采用GraphQL,感觉很有意思,一看文档,竟然有Python的开源实现 Graphene ...

  6. nagios+influxdb+grafana的监控数据可视化流程

    nagios介绍 nagios是一款开源监控的应用,可用于监控本地和远程主机的日志.资源.死活等等诸多功能.通过snmp协议和nrpe协议. nagios的配置文件是由nconf上进行配置,然后点击生 ...

  7. WebStrom-JS编程小技巧

    快速打印某个名为***的对象:***.log回车效果如下:

  8. Flutter的使用教学笔记

    QQ交流群 Flutter 程序开发群:766307130 教程 官方实战 使用Flutter 构建精美的页面 云在千峰 博主一直是从事 Android 开发的,所以主要从 Android 技术角度来 ...

  9. 查看进程的pid和ppid

    用个栗子来说明吧from multiprocessing import Processimport time,os def task(): print('%s is running ,parents ...

  10. day08.3-apache网页服务

    1. 安装软件:yum   install   httpd   -y 2. 查看配置文件:vim   /etc/httpd/conf/httpd.cof,其中,"Listen   80&qu ...