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
#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 = 1005;
const int MOD = 1e9 + 7;

#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[50005][20], dp_min[50005][20];
int n, m;
void ST()
{
    int i, j;
    for(i = 1;i <= n;++i)
    {
        cin >> dp_max[i][0];
        dp_min[i][0] = dp_max[i][0];
    }
    //这里循环手动模拟就懂
    for(j = 1;(1 << j) <= n;++j)
        for(i = 1;i + (1 << j) - 1 <= n;++i)
        {
            dp_max[i][j] = max(dp_max[i][j - 1], dp_max[i + (1 << (j - 1))][j]);
            dp_min[i][j] = min(dp_min[i][j - 1], dp_min[i + (1 << (j - 1))][j]);
        }
}

int RMQ(int l, int r)
{
    int k = 0;
    while((1 << (k + 1)) <= r - l + 1)
        k++;
    int a = max(dp_max[l][k], dp_max[r - (1 << k) + 1][k]);
    int b = min(dp_min[l][k], dp_min[r - (1 << k) + 1][k]);
    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 0;
}

原文地址:https://www.cnblogs.com/shuizhidao/p/9356327.html

时间: 2024-08-01 11:03:59

RMQ、POJ3264的相关文章

RMQ问题——ST算法

什么是RMQ.ST:RMQ(Range Minimum/Maximum Query)问题,即求区间的最值.可以写一个线段树来实现,但是每次查询的时间复杂度为O(log n),若查询次数过多则可能超时.ST算法是一种离线算法,经过O(nlogn)的预处理后,可以在O(1)的时间复杂度内进行查询,缺点是无法对数据做出修改. 算法实现: 初始化:用dp实现初始化.a[]为原始数据数组f,[i][j]表示从i向后的2j个数字中的最值.显然f[i][0]=a[i]; 我们将f[i][j]分为两段,一段为a

RMQ [HDU 1806] Frequent values

Frequent values Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1146    Accepted Submission(s): 415 Problem Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasin

【ACM算法纲要】【转自ALPC】

基本 C/C++.STL(vector.set.map.queue.string.algorithm) 枚举.贪心.递归.分治.递推.模拟 构造.位运算.常数优化 数据结构 队列.堆.栈.链表 排序(插入.冒泡.快速.归并.堆.桶.基数) 二分查找.散列表.并查集.哈夫曼树 排序二叉树.左偏树.平衡树(Splay/Treap/SBT) 树状数组.线段树.归并树.划分树.主席树.树套树 树链剖分.动态树 1/2维RMQ.LCA(在线/离线).稀疏表.字典树 字符串 KMP.扩展KMP AC自动机

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

一眼看题目吓了一跳:这TM不就是单调队列吗,200000又怎样,大不了我二分嘛 系统提示:成功开启 手残模式 开始瞎写: 1 #include <cstdio> 2 long long a[200001]; 3 int b[200001]; 4 int m,mod; 5 int find(int l,int r,long long x) 6 { 7 if(l>=r-1) 8 if(a[r]>=x) 9 return r; 10 else 11 return l; 12 int mi

算法纲要

基本 枚举.贪心.递归.分治.递推.模拟 STL(pair.vector.set.map.queue.string.algorithm) 构造.位运算.常数优化 数据结构 队列.堆.栈.链表 排序(插入.冒泡.快速.归并.堆.桶.基数) 二分查找.散列表 并查集.哈夫曼树 排序二叉树.左偏树.平衡树(Splay/Treap/SBT) 树状数组.线段树.归并树.划分树.主席树.树套树 树链剖分.动态树 1/2维RMQ.LCA(在线/离线).稀疏表.字典树 字符串 KMP.扩展KMP AC自动机 后

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

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序列中所有值都能被其中一个值整除,②且子序列范围尽可能大(r-l尽可能大). 解题思路: 对于要求1,不难发现只有min(L,R)=gcd(L,R)时才行.其中gcd是L,R范围内的最大公约数,min是L,R范围内的最小值. 对于要求2,传统思路是r-l从大到小枚举,每次确定一个(L,R)范围,进行判

2016年的总结

距离大一入学已经经过了两年半的时间,离我第一次接触C语言也过去了两年半.向王瑞洲(以下简称GodWang)请教字符串的读入.01背包的情景还历历在目,弹指一挥间,如今已是大三. 在高考结束的时候,莫名其妙的只想填计算机类的专业,也许是出于对编程的好奇,又或许是觉得会编程的人特别帅.在纠结了很久是选择计算机还是软件工程之后,我决定选择软件工程,因为我对硬件方面没有太大的兴趣.就这样,我和爸爸妈妈将全国带有软件工程专业的学校都找了出来,寻找自己能去的地方.最后,我来到了这里——浙江财经大学信息学院.

HDU 2586 How far away?(LCA使用详解)

关键词:LCA.并查集.动态规划.深度优先搜索.哈希.RMQ.递归 题目: Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it ha

·算法」 纲要

基本 C/C++.STL(vector.set.map.queue.string.algorithm) 枚举.贪心.递归.分治.递推.模拟 构造.位运算.常数优化 数据结构 队列.堆.栈.链表 排序(插入.冒泡.快速.归并.堆.桶.基数) 二分查找.散列表.并查集.哈夫曼树 排序二叉树.左偏树.平衡树(Splay/Treap/SBT) 树状数组.线段树.归并树.划分树.主席树.树套树 树链剖分.动态树 1/2维RMQ.LCA(在线/离线).稀疏表.字典树 字符串 KMP.扩展KMP AC自动机