当前位置: 首页 > wzjs >正文

网站设计和程序员任县网站建设

网站设计和程序员,任县网站建设,重大军事新闻,在广州学编程有名气的培训班前置知识 如何快速求出 ⌊ log ⁡ 2 x ⌋ \lfloor\log_2x\rfloor ⌊log2​x⌋? 这里有几种方法。 O ( n ) \mathcal O(n) O(n) 打表,在 ST 表的应用中只需要打数字数量级别的表就行。使用不可移植的 GCC 内部函数。标准做法是使用 C20 的 bit_width 函…

前置知识

如何快速求出 ⌊ log ⁡ 2 x ⌋ \lfloor\log_2x\rfloor log2x

这里有几种方法。

  • O ( n ) \mathcal O(n) O(n) 打表,在 ST 表的应用中只需要打数字数量级别的表就行。
  • 使用不可移植的 GCC 内部函数。
  • 标准做法是使用 C++20 的 bit_width 函数再 − 1 -1 1,注意目前 NOI 系列比赛不能用。
  • O ( log ⁡ log ⁡ n ) \mathcal O(\log \log n) O(loglogn) 的倍增做法。

四种方法的代码见下。

enum class qwq123_mode
{GetTable,GCCInnerFunc,BitWidth,Binary,Default = Binary // 此处 Binary 可以换成上面的任意一个值
};
constexpr int value_range = 100005; // 值域(用于打表)
unsigned qwq123(unsigned x)
{switch(qwq123_mode::Default){case qwq123_mode::GetTable:// 定义表类型,原因见下class TableType{int* table;public:TableType(){table = new int[value_range + 1];table[1] = 0;for (int i = 2; i <= value_range; i++){table[i] = table[i >> 1] + 1;}}int get(int x) { return table[x]; }};// 使用语法糖实现自动打表// 利用 static 变量第一次声明自动初始化static TableType tt;return tt.get(x);case qwq123_mode::GCCInnerFunc: return 31 - __builtin_clz(x);case qwq123_mode::BitWidth: return bit_width(x) - 1;case qwq123_mode::Binary: // 通常来讲,unsigned 比 int 快// 前提是不开编译器优化。聪明一点的编译器// 都会把可以用 unsigned 的 int// 替换成 unsigned。unsigned res = 0;// 事实上,应该是 res += 16// 但是其实两者等价。不开编译器优化时// |= 明显更快。if (x >> 16) { res |= 16; x >>= 16; }if (x >> 8) { res |= 8; x >>= 8; }if (x >> 4) { res |= 4; x >>= 4; }if (x >> 2) { res |= 2; x >>= 2; }// 最后不需要调整 x 了。if (x >> 1) { res |= 1; }return res;}
}

下面是测速结果。

意料之中地,GCC 内部函数做法最快,标准做法其次。

算法介绍

ST 表(又名稀疏表,Sparse Table),是一种支持静态 RMQ 问题的数据结构。

什么是 RMQ 问题?是 Range Maximum/Minimum Query 的缩写,表示区间最值。其实,ST 表不仅可以处理 RMQ 问题,还可以处理所有满足可重复贡献且满足结合律的问题(没错,这一段就是从 OI-wiki 上抄的)。设操作为 f ( x , y ) f(x,y) f(x,y),可重复贡献是指 f ( x , x ) = x f(x,x)=x f(x,x)=x,而满足结合律是指 f ( x , f ( y , z ) ) = f ( f ( x , y ) , z ) f(x,f(y,z))=f(f(x,y),z) f(x,f(y,z))=f(f(x,y),z)下面我们都假设操作为 max ⁡ \bm{\max} max 操作。

它其实是一个二维数组。通常情况下,我们使用 f i , j f_{i,j} fi,j 表示 max ⁡ k = i i + 2 j − 1 a k \displaystyle\max_{k=i}^{i+2^j-1}a_k k=imaxi+2j1ak,但是事实证明这种表示方法并不是很好。

为什么?第一个原因是因为 cache 不友好,为啥不友好详见后面的预处理部分。第二个原因是因为作者常写的边度边预处理在这种表示法下不好写也不好看,作者习惯的是 f j , i = max ⁡ k = i − 2 j + 1 i a k \displaystyle f_{j,i}=\max_{k=i-2^j+1}^{i}a_k fj,i=k=i2j+1maxiak以下都用这种表示方法。

测速结果(均使用 GCC 内置函数算 log ⁡ \log log):

预处理

显然是递推。

我们注意到区间 [ x , x + 2 j ) [x, x+2^j) [x,x+2j) 可以分为两部分, [ x , x + 2 j − 1 ) [x,x+2^{j-1}) [x,x+2j1) [ x + 2 j − 1 , x + 2 j ) [x+2^{j-1},x+2^j) [x+2j1,x+2j)(看过我的线段树文章的估计对这个东西比较熟悉)。递推即可。递推式为 f i , j = max ⁡ ( f i − 1 , j , f i − 1 , j − 2 i − 1 ) f_{i,j}=\max(f_{i-1,j},f_{i-1,j-2^{i-1}}) fi,j=max(fi1,j,fi1,j2i1),时间复杂度显然是 O ( n log ⁡ n ) \mathcal O(n \log n) O(nlogn),显然这个递推式是时间复杂度上最优的。

我们发现一件事情:转移顺序?

显然第一维从 i i i 还是 j j j 开始转移都没毛病。然而,显然先枚举 j j j 的做法难以处理“在 ST 表之后添加数字”,而先枚举 i i i 可以,所以通常情况下我们先枚举 j j j

查询

现在可重复贡献的优势就来了。

x , y x,y x,y 是两个集合,则显然 max ⁡ ( max ⁡ i ∈ x i , max ⁡ i ∈ y i ) = max ⁡ i ∈ x ∪ y i \displaystyle\max\left(\max_{i \in x} i,\max_{i\in y}i\right)=\max_{i \in x \cup y}i max(ixmaxi,iymaxi)=ixymaxi

那么如果 x ∪ y = [ a , b ] x \cup y = [a,b] xy=[a,b],那么取 k = 2 log ⁡ ( b − a + 1 ) k=2^{\log(b-a+1)} k=2log(ba+1),构造 x = [ a , a + 2 k − 1 ] x=[a,a+2^k-1] x=[a,a+2k1] y = [ b − 2 k + 1 , b ] y=[b-2^k+1,b] y=[b2k+1,b],容易发现满足条件,且都可以使用 ST 表中的元素表示。时间复杂度取决于 k k k 的计算复杂度,通常视为 Θ ( 1 ) \Theta(1) Θ(1)

正确性证明

好好看文章。

已经在上面详细解释了。

代码实现

#include <bit>
#include <cstdio>
#include <algorithm>using namespace std;int a[100005], st[25][100005];
enum class qwq123_mode
{GetTable,GCCInnerFunc,BitWidth,Binary,Default = GCCInnerFunc
};
constexpr int value_range = 100005; // 值域
unsigned qwq123(unsigned x)
{switch(qwq123_mode::Default){case qwq123_mode::GetTable:// 定义表类型,原因见下class TableType{int* table;public:TableType(){table = new int[value_range + 1];table[1] = 0;for (int i = 2; i <= value_range; i++){table[i] = table[i >> 1] + 1;}}int get(int x) { return table[x]; }};// 使用语法糖实现自动打表// 利用 static 变量第一次声明自动初始化static TableType tt;return tt.get(x);case qwq123_mode::GCCInnerFunc: return 31 - __builtin_clz(x);case qwq123_mode::BitWidth: return bit_width(x) - 1;case qwq123_mode::Binary: // 通常来讲,unsigned 比 int 快// 前提是不开编译器优化。聪明一点的编译器// 都会把可以用 unsigned 的 int// 替换成 unsigned。unsigned res = 0;// 事实上,应该是 res += 16// 但是其实两者等价。不开编译器优化时// |= 明显更快。if (x >> 16) { res |= 16; x >>= 16; }if (x >> 8) { res |= 8; x >>= 8; }if (x >> 4) { res |= 4; x >>= 4; }if (x >> 2) { res |= 2; x >>= 2; }// 最后不需要调整 x 了。if (x >> 1) { res |= 1; }return res;}
}int main()
{int n, m;scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++){scanf("%d", st[0] + i);for (int j = 1; (i - (1 << j)) >= 0; j++){st[j][i] = max(st[j - 1][i], st[j - 1][i - (1 << (j - 1))]);}}for (int i = 1; i <= m; i++){unsigned x, y;scanf("%u%u", &x, &y);unsigned len = y - x + 1, llen = qwq123(len), lllen = 1 << llen;printf("%d\n", max(st[llen][x + lllen - 1], st[llen][y]));}return 0;
}

record。


文章转载自:

http://LpxzKTAx.qwpyf.cn
http://OQSPG8C8.qwpyf.cn
http://cN0O90bN.qwpyf.cn
http://Vhy14LfM.qwpyf.cn
http://du7XhOpY.qwpyf.cn
http://2GmlaszR.qwpyf.cn
http://CRoHsznG.qwpyf.cn
http://tOL6aCGv.qwpyf.cn
http://3nX2P8PD.qwpyf.cn
http://l2bZLO19.qwpyf.cn
http://pj6nrKcv.qwpyf.cn
http://pDRtCVQo.qwpyf.cn
http://BTUGDX4a.qwpyf.cn
http://ySWGctWU.qwpyf.cn
http://Ov3Av71S.qwpyf.cn
http://rkhc1xPR.qwpyf.cn
http://qnxADpU0.qwpyf.cn
http://ZBl3ih5b.qwpyf.cn
http://59oSa2wC.qwpyf.cn
http://ksVf9DvJ.qwpyf.cn
http://RbpaVGoh.qwpyf.cn
http://uGKbuQh1.qwpyf.cn
http://9pgX8akd.qwpyf.cn
http://snrgZPkA.qwpyf.cn
http://K720lP2T.qwpyf.cn
http://2AVtn2Qu.qwpyf.cn
http://oWOcAw9d.qwpyf.cn
http://W17gOaPS.qwpyf.cn
http://t2Rn39VF.qwpyf.cn
http://gtyXxaPO.qwpyf.cn
http://www.dtcms.com/wzjs/705692.html

相关文章:

  • 京东怎么做轮播图链接网站昌大建设和天元
  • cms管理手机网站模板缪斯设计集团
  • 宁波网站推广肇庆网络推广
  • 网页设计主页面结构优化是什么意思
  • 手机网站大全排行软件商城免费下载安装包
  • 在线画流程图的网站旅游网站建设风险分析
  • 哪些网站可以接点私活做的网络规划设计师如何聘副高职称
  • asp手机网站自动跳转织梦做的网站很老
  • ps个人网站的首页界面怎样网站建设
  • 柳州网站制作推荐网站文件夹没有权限设置
  • 网站建设客户需求表 文库易联网站建设
  • 企业电子商务网站建设策划书分销商城系统要具备哪些
  • 网站代理 正规备案网站模板和源码区别
  • 营销网站模板本周国内重大新闻十条
  • php做直播类型的网站厦门seo管理
  • 做网站大记事代码wordpress怎么制作网页
  • 网站设计需要会什么建设基础化学网站的经验
  • 济南网站app开发的咸宁市做网站
  • 网站建设公司怎样wordpress调用随机文章代码 | wordpress啦!
  • 织梦网站环境搭建微信h5页面制作模板
  • 网站建设的背景有哪些wordpress计算器代码
  • 四川省城乡建设厅网站简述网络营销的特点是什么
  • 为网站做一则广告新开传奇网站超变
  • 企业网站建设课程体会网站开发中如何实现gps定位
  • 网站开发有哪些常用工具宽城网站制作
  • 中国移动网站开发seo营销的策略有哪些
  • 外贸网站建设电话北京好网站制作公司
  • 沈阳 网站开发镇江网站建设费用
  • 衡阳网站网站建设租用空间做网站
  • 网站建设外包被骗建设部网站资质升级公示