RMQ:

一、一维RMQ:
前两天学lca的时候学了这个东西,今天再来系统的学一下。
P3865 【模板】ST表:
题目背景
这是一道ST表经典题——静态区间最大值

请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每次查询复杂度为 O(1)

题目描述
给定一个长度为 N 的数列,和 M 次询问,求出每一次询问的区间内数字的最大值。

输入格式
第一行包含两个整数 N, M 分别表示数列的长度和询问的个数。

第二行包含 N 个整数依次表示数列的第 i 项。

接下来 M行,每行包含两个整数 l, r,表示查询的区间为 [ l, r]
输出格式
输出包含 M行,每行一个整数,依次表示每一次询问的结果。

输入输出样例
输入 #1 复制
8 8
9 3 1 7 5 6 0 8
1 6
1 5
2 7
2 6
1 8
4 8
3 7
1 8
输出 #1 复制
9
9
7
7
9
8
7
9
n<=1e5,m<=1e6

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
#define ll long long
#define llu unsigned ll
using namespace std;
const int maxn=101000;
int f[maxn][20],a[maxn], _log[maxn];
int n,m;
void ST(void)
{
   
    for(int i=1;i<=n;i++)
        f[i][0]=a[i],_log[i]=log(i)/log(2);


    //还有另一种求log的方法;
        _log[0]=-1;
        for(int i=1;i<=n;i++)
        {
   
            f[i][0]=a[i];
            _log[i]=(i&(i-1))==0?_log[i-1]+1:_log[i-1];
        }


    int t=_log[n]+1;
    //int t=_log[n];
    //上述两种t的取值范围均可
    for(int j=1;j<=t;j++)
    {
   
        for(int i=1;i<=n-(1<<j)+1;i++)
            f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
    }
}

int get_max(int l,int r)
{
   
    int k=_log[r-l+1];
    return max(f[l][k],f[r-(1<<k)+1][k]);
}

int main(void)
{
   
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    ST();
    int x,y;
    for(int i=1;i<=m;i++)
    {
   
        scanf("%d%d",&x,&y);
        printf("%d\n",get_max(x,y));
    }
    return 0;
}


二、二维RMQ:

二维RMQ有两种写法,一种是单独处理每一行,另一种是直接处理整个矩阵。
前者预处理时间复杂度为O(n * m * logn),查询时间复杂度为O(n)。
后者预处理时间复杂度为O(n * m * logn * logm),查询时间复杂度为O(1)。

①Cornfields POJ - 2019 :
FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he’s looking to build the cornfield on the flattest piece of land he can find.

FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it.

FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form “in this B x B submatrix, what is the maximum and minimum elevation?”. The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.
Input

  • Line 1: Three space-separated integers: N, B, and K.

  • Lines 2…N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.

  • Lines N+2…N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1…N-B+1.
    Output

  • Lines 1…K: A single integer per line representing the difference between the max and the min in each query.
    Sample Input
    5 3 1
    5 1 2 6 3
    1 3 5 2 7
    7 2 4 6 1
    9 9 8 6 5
    0 6 9 3 9
    1 2
    Sample Output
    5
    求某个小举行内最大值与最小值的差值。这个题的矩形是正方形,只给左上角坐标和边长。

//每一行分别RMQ
//313ms
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=260;
int a[maxn][maxn];
int f[2][maxn][maxn][8];
int _log[maxn];
int n,m,len;

void st(void)
{
   
    _log[0]=-1;
    for(int i=1;i<=n;i++)
    {
   
        _log[i]=(i&(i-1))==0?_log[i-1]+1:_log[i-1];
        for(int j=1;j<=n;j++)
            f[0][i][j][0]=f[1][i][j][0]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值