R M Q RMQ RMQ问题
RMQ(Range Minimum Query),范围最小值问题。具体表现为一下一类问题:
给出一个 n 个元素的数组 A 1 , A 2 , … , A n A1,A2,…,An A1,A2,…,An ,求解 m i n ( l , r ) min(l,r) min(l,r) : 计算 m i n A l , A l + 1 , … , A r min{Al,Al+1,…,Ar} minAl,Al+1,…,Ar
R M Q RMQ RMQ 问题有很多解法,其中较为快捷和简便的是 Tarjan 的 S p a r s e − T a b l e Sparse−Table Sparse−Table 算法,简称 ST 表。
S p a r s e − T a b l e Sparse−Table Sparse−Table
算法基于倍增思想,整个算法由预处理和查询两部分组成。
- 讲解传送门1
- 讲解传送门2
一维RMQ代码
#include<bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 10;
int ar[mxn];
int st[mxn][30], Log[mxn];
void ST(int n)
{
//预处理Log, 并且对Log[i] 进行了向下取整
Log[1] = 0;
for(int i = 2; i <= n; i ++) Log[i] = Log[i / 2] + 1;
//初始化st数组
for(int i = 1; i <= n; i ++) st[i][0] = ar[i];
//dp求解状态转移
for(int j = 1; (1 << j) <= n; j ++)
for(int i = 1; i + (1 << (j - 1)) <= n; i ++)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
scanf("%d", &ar[i]);
ST(n);
int q, l, r;
scanf("%d", &q);
while(q --)
{
scanf("%d %d", &l, &r);
int k = Log[r - l + 1];
int mx = max(st[l][k], st[r - (1 << k) + 1][k]);
printf("%d\n", mx);
}
return 0;
}
R M Q RMQ RMQ问题
二维RMQ:快速求矩形的子矩阵中的最大、最小值
- 讲解传送门1
- 讲解传送门2
二维RMQ代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
/* #include <unordered_map> */
#include <bitset>
#include <vector>
void fre() { system("clear"), freopen("A.txt", "r", stdin); freopen("Ans.txt","w",stdout); }
void Fre() { system("clear"), freopen("A.txt", "r", stdin);}
#define ios ios::sync_with_stdio(false)
#define Pi acos(-1)
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define db double
#define Pir pair<int, int>
#define PIR pair<Pir, Pir>
#define m_p make_pair
#define INF 0x3f3f3f3f
#define esp 1e-7
#define mod (ll)(1e9 + 7)
#define for_(i, s, e) for(int i = (ll)(s); i <= (ll)(e); i ++)
#define rep_(i, e, s) for(int i = (ll)(e); i >= (ll)(s); i --)
#define sc scanf
#define pr printf
#define sd(a) scanf("%d", &a)
#define ss(a) scanf("%s", a)
using namespace std;
#define Max(a, b, c, d) max(max(a, b), max(c, d));
#define Min(a, b, c, d) min(min(a, b), min(c, d));
const int mxn = 255;
int mz[mxn][mxn];
int Log[mxn];
int st1[mxn][mxn][8][8];
int st2[mxn][mxn][8][8];
void init(int n)
{
Log[1] = 0;
for_(i, 2, n) Log[i] = Log[i / 2] + 1;
}
void ST(int n)
{
for_(i, 1, n)
for_(j, 1, n)
st1[i][j][0][0] = st2[i][j][0][0] = mz[i][j];
for(int k = 0; (1 << k) <= n; k ++)
for(int l = 0; (1 << l) <= n; l ++)
{
if(l == 0 && k == 0) continue;
for(int i = 1; i + (1 << k) - 1 <= n; i ++)
for(int j = 1; j + (1 << l) - 1 <= n; j ++)
{
if(k) //如果k不等于0,就按行 分成大小相同的两块,从这两个小块中的极值 得到 大块的极值
{
st1[i][j][k][l]=max(st1[i][j][k-1][l],st1[i+(1<<(k-1))][j][k-1][l]);
st2[i][j][k][l]=min(st2[i][j][k-1][l],st2[i+(1<<(k-1))][j][k-1][l]);
}
else //否则的话,就按列将所求的区域分成两小块
{
st1[i][j][k][l]=max(st1[i][j][k][l-1],st1[i][j+(1<<(l-1))][k][l-1]);
st2[i][j][k][l]=min(st2[i][j][k][l-1],st2[i][j+(1<<(l-1))][k][l-1]);
}
}
}
}
int main()
{
/* fre(); */
int n, m, q;
sc("%d %d %d", &n, &m, &q);
init(n);
for_(i, 1, n)
for_(j, 1, n)
sd(mz[i][j]);
ST(n);
int l, r;
int k = Log[m];
while(q --)
{
sc("%d %d", &l, &r);
int L = l + m - 1;
int R = r + m - 1;
//最大/小值从四小块中产生
int mx = Max(st1[l][r][k][k], st1[L - (1 << k) + 1][r][k][k], st1[l][R - (1 << k) + 1][k][k], st1[L - (1 << k) + 1][R - (1 << k) + 1][k][k]);
int mn = Min(st2[l][r][k][k], st2[L - (1 << k) + 1][r][k][k], st2[l][R - (1 << k) + 1][k][k], st2[L - (1 << k) + 1][R - (1 << k) + 1][k][k]);
pr("%d\n", mx - mn);
}
return 0;
}