当前位置:网站首页>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) ,不支持在线修改.
边栏推荐
- Redis的线程模型
- Win11怎么在右键菜单添加一键关机选项
- 推开机电的大门《电路》(三):说说不一样的电阻与电导
- What should I do if I install a solid-state drive in Win10 and still have obvious lags?
- 倍增和稀疏表
- Impressions of Embrace Jetpack
- Win10无法连接打印机怎么办?不能使用打印机的解决方法
- MATLAB绘图函数plot详解
- 项目:数据库表的梳理
- Publish module to NPM should be how to operate?Solutions to problems and mistake
猜你喜欢
Codeforces Round #605 (Div. 3)
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
Mapreduce环境详细搭建和案例实现
win10怎么设置不睡眠熄屏?win10设置永不睡眠的方法
Installation and configuration of Spark and related ecological components - quick recall
网络安全抓包
Mysql lock
Win10电脑需要安装杀毒软件吗?
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
pygame draw arc
随机推荐
ASR6601牛羊定位器芯片GPS国内首颗支持LoRa的LPWAN SoC
LeetCode2 电话号码的字母组合
基于最小二乘法的线性回归分析方程中系数的估计
What is Win10 God Mode for?How to enable God Mode in Windows 10?
Do Windows 10 computers need antivirus software installed?
CS4398音频解码替代芯片DP4398完全兼容DAC解码
Impressions of Embrace Jetpack
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
Win10上帝模式干嘛的?Win10怎么开启上帝模式?
SQL的通用语法和使用说明(图文)
动态规划理论篇
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
Installation and configuration of Spark and related ecological components - quick recall
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
C语言函数参数传递模式入门详解
发布模块到npm应该怎么操作?及错误问题解决方案
What are IPV4 and IPV6?
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
win11一直弹出用户账户控制怎么解决