当前位置:网站首页>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) ,不支持在线修改.
边栏推荐
- 模板系列-并查集
- win10 system update error code 0x80244022 how to do
- DP1332E刷卡芯片支持NFC内置mcu智能楼宇/终端poss机/智能门锁
- 7.Redis
- casbin模型
- CMAKE
- Win11系统找不到dll文件怎么修复
- Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
- Introduction to MATLAB drawing functions ezplot explanation
- 质数相关问题-小记
猜你喜欢
将SSE指令转换为ARM NEON指令
Redis的线程模型
Mysql lock
In-depth understanding of Golang's Map
How to add a one-key shutdown option to the right-click menu in Windows 11
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
Summarize computer network super comprehensive test questions
7.Redis
Win10 computer can't read U disk?Don't recognize U disk how to solve?
cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
随机推荐
【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
MATLAB绘图命令fimplicit绘制隐函数图形入门详解
Win11没有本地用户和组怎么解决
MATLAB绘图函数fplot详解
Redis的线程模型
质数相关问题-小记
Mysql connection error solution
LORA芯片ASR6601支持M4内核的远距离传输芯片
C语言函数参数传递模式入门详解
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
MATLAB绘图函数plot详解
KiCad常用快捷键
pytorch模型转libtorch和onnx格式的通用代码
日常-笔记
vscode镜像
word方框怎么打勾?
CS4398音频解码替代芯片DP4398完全兼容DAC解码
Win11 keeps popping up User Account Control how to fix it
开心一下,9/28名场面合集
13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议