当前位置:网站首页>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解决RMQ
The 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) ,不支持在线修改.
边栏推荐
- PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
- General syntax and usage instructions of SQL (picture and text)
- 轻量化AlphaPose
- golang之GMP调度模型
- How to simulate 1/3 probability with coins, and arbitrary probability?
- DP1101兼容CC1101是SUB1GHz无线收发芯片应用于智能家居
- [System Design and Implementation] Flink-based distracted driving prediction and data analysis system
- pygame拖动条的实现方法
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
- 使用 腾讯云搭建一个个人博客
猜你喜欢
Codeforces Round #605 (Div. 3)
Win10系统设置application identity自动提示拒绝访问怎么办
How to simulate 1/3 probability with coins, and arbitrary probability?
Configure clangd for vscode
关于c语言的调试技巧
Open the door of electricity "Circuit" (1): voltage, current, reference direction
总结计算机网络超全面试题
Mysql连接错误解决
TCP三次握手、四次挥手
MATLAB绘制平面填充图入门详解
随机推荐
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
使用 腾讯云搭建一个个人博客
Mysql的锁
Spark及相关生态组件安装配置——快速回忆
DP4344兼容CS4344-DA转换器
pygame绘制弧线
Mysql connection error solution
Win11 system cannot find dll file how to fix
Please make sure you have the correct access rights and the repository exists. Problem solved
How to update Win11 sound card driver?Win11 sound card driver update method
win10怎么设置不睡眠熄屏?win10设置永不睡眠的方法
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
golang之GMP调度模型
关于c语言的调试技巧
Introduction to MATLAB drawing functions ezplot explanation
[STM32 Learning 1] Basic knowledge and concepts are clear
网络安全抓包
Win11声卡驱动如何更新?Win11声卡驱动更新方法
Detailed explanation of Golang garbage collection mechanism