当前位置:网站首页>Doubled and sparse tables
Doubled and sparse tables
2022-08-02 15:32:00 【Old stubborn and cute】
倍增、ST、RMQ
1、倍增
Any integer can be represented as several2的次幂项之和.例如:
整数5,其二进制表示为101,该二进制数从右向左第0、2位均为1,则5=2+20
整数26,其二进制表示为11010,该二进制数从右向左第1、3、4位均为1,则 26 = 2 4 + 2 3 + 2 1 26=2^4+2^3+2^1 26=24+23+21 .也就是说,2The power terms of can be spelled to any desired value.
倍增,顾名思义就是成倍增加.若问题的状态空间特别大,则一步步递推的算法复杂度太高,可以通过倍增思想,只考察2integer power position of ,快速缩小求解范围,直到找到解.
2、ST
ST(Sparse Table,稀疏表)The algorithm adopts the idea of multiplication,在 O ( n l o g n ) O(nlogn) O(nlogn) 时间构造一个二维表之后,可以在 O ( 1 ) O(1) O(1) 时间在线查询 [ l , r ] [l, r] [l,r] 区间的最值,有效解决 在线RMQ(Range Minimum/Maximum Query,区间最值查询)问题.如何实现呢?
设 F [ i , j ] F[i,j] F[i,j] 表示 [ i , i + 2 j − 1 ] [i, i+2^j-1] [i,i+2j−1] 区间的最值,区间长度为 2 j 2^j 2j.

根据倍增思想,长度为 2 j 2^j 2j The interval can be divided into two lengths 2 j − 1 2^{j-1} 2j−1 的子区间,Then find the maximum value of the two subintervals.
递推公式: F [ i , j ] = m a x ( F [ i , j − 1 ] , F [ i + 2 j − 1 , j − 1 ] ) F[i,j]=max(F[i,j-1], F[i+2^{j-1},j-1]) F[i,j]=max(F[i,j−1],F[i+2j−1,j−1])

2.1 ST表的创建
若 F [ i , j ] F[i, j] F[i,j] 表示 [ i , i + 2 j − 1 ] [i, i+2^{j}-1] [i,i+2j−1] 区间的最值,区间长度为 2 j 2^j 2j,则和 j j j What is the value range of ?
若数组的长度为 n n n ,Maximum interval length 2 k ≤ n < 2 k + 1 2^k≤n<2^{k+1} 2k≤n<2k+1,则 k = ⌊ l o g 2 n ⌋ k=\lfloor log_2n\rfloor k=⌊log2n⌋, 比如:
- n=8时k=3,
- n=10时k=3.
在程序中,k=log2(n), Common expressions are also available k=log(n)/log(2),log()表 show e为底的自然对数.
void ST_create() {
for(int i=1; i<=n; i++) {
F[i][0]=a[i];// 区间[i,i]的最值,interval length bits2^0
}
int k=log(n)/log(2.0);
for(int j=1; j<=k; j++) {
for(int i=1; i<=n-(1<<y)+1; i++) {
F[i][j]=max(F[i][j-1],F[i+(1<<j-1)][j-1])
}
}
}
例如,有10个元素 a [ 1..10 ] = 5 , 3 , 7 , 2 , 12 , 1 , 6 , 4 , 8 , 15 a[1..10]={5,3,7,2,12,1,6,4, 8,15} a[1..10]=5,3,7,2,12,1,6,4,8,15,Create a query for the maximum valueST表. F [ i , j ] F[i,j] F[i,j] 表示 [ i , i + 2 j − 1 ] [i, i+2^j-1] [i,i+2j−1] 区间的最值,区间长度为2.

2.2 ST表查询
若查询 [ l , r ] [l,r] [l,r] 区间的最值,则首先计算 k k k 值,和前面的计算方法相同,区间长度为 r − l + 1 r-l+1 r−l+1, 2 k ≤ r − l + 1 < 2 k + 1 2^k\leq r-l+1<2^{k+1} 2k≤r−l+1<2k+1, 因此 $k=log2(r-l+1) $.
若查询区间的长度大于或等于 2 2 2 且小于 2 k + 1 2^{k+1} 2k+1 ,则根据倍增思想,可以将查询区间分为两个查询区间,取两个区间的最值即可.The two intervals are from backward 2 k 2^k 2k 个数及从 r r r 向前的 2 k 2^k 2k 个数,这两个区间可能有重叠,但对求最值没有影响.

int ST_query(int l, int r){
int k=log2(r-l+1);
return max(F[l][k], F[r-(1<<k)+1][k])
}
3、RMQ
RMQ(区间最值查询)There are multiple solutions to the problem,Use a segment tree and ST解决RMQThe comparison of the problems is as follows:
- The time for segment tree preprocessing is O ( n l o g n ) O(nlogn) O(nlogn),查询的时间为 O ( l o g n ) O(logn) O(logn) ,支持在线修改
- STThe preprocessing time is O ( n l o g n ) O(nlogn) O(nlogn),查询的时间为 0 ( 1 ) 0(1) 0(1) ,不支持在线修改.
边栏推荐
- How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
- pygame图像连续旋转
- Cmd Markdown 公式指导手册
- MATLAB制作简易小动画入门详解
- Win11 computer off for a period of time without operating network how to solve
- 二叉树遍历之后序遍历(非递归、递归)入门详解
- 质数相关问题-小记
- 发布模块到npm应该怎么操作?及错误问题解决方案
- Introduction to MATLAB drawing functions ezplot explanation
- 基于矩阵计算的线性回归分析方程中系数的估计
猜你喜欢

Lightweight AlphaPose

2.登录退出,登录状态检查,验证码

What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer

推开机电的大门《电路》(一):电压,电流,参考方向

网络安全抓包

MATLAB绘制平面填充图入门详解

cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so

使用 腾讯云搭建一个个人博客

Yolov5 official code reading - prior to transmission

Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
随机推荐
质数相关问题-小记
MATLAB绘图函数plot详解
单端K总线收发器DP9637兼容L9637
pytorch模型转libtorch和onnx格式的通用代码
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
Yolov5 official code reading - prior to transmission
In-depth understanding of Golang's Map
推开机电的大门《电路》(三):说说不一样的电阻与电导
golang之GMP调度模型
Win10系统设置application identity自动提示拒绝访问怎么办
jest test, component test
将SSE指令转换为ARM NEON指令
How to update Win11 sound card driver?Win11 sound card driver update method
3.用户上传头像
General code for pytorch model to libtorch and onnx format
SQL的通用语法和使用说明(图文)
LORA芯片ASR6601支持M4内核的远距离传输芯片
pygame拖动条的实现方法
Win11 keeps popping up User Account Control how to fix it
Do Windows 10 computers need antivirus software installed?