版本比较
STL:rope
标题
增加 1 行删除 1 行
1简单的 cxx::rope1STL:rope
正文
增加 82 行删除 19 行
12345678910111213141516171819203 行未改动2425262728293031323334353637384 行未改动43444546474849505152535455565758596061626386 行未改动150151152153154155156157102 行未改动全部展开26026126226326426526626776 行未改动344345346347348349350351352### 声明本文认为 rope 是 $O(\sqrt n)$ 的。测试:使用学校机房,$\text{10th Gen Intel(R) Core(TM)i5 }$,内存 $16GB$。#### 结果|代码|$n=10^5$|$n=5\times 10^5$|$n=10^6$|$n=5\times 10^6$|-|:-:|:-:|:-:|:-:||Treap|$62.5ms$|$109.4ms$|$187.5ms$|$843ms$|rope|$453.1ms$|$\red{2547ms}$|$\red{5109ms}$|不想跑了|写假了的 AVL|-|-|-|$3047ms$||AVL|-|-|-|$1391ms$||BIT|-|-|-|$\green{140ms}$### 基础用法#### 引用```cpp#include<ext/rope>using namespace __gnu_cxx;```3 行未改动```cpp#include<bits/extc++.h>using namespace __gnu_cxx;```#### 定义与 vector 类似,`rope<type>` 即可定义。`rope<type> rp(100000)` 可以定义一个大小为 $100000$ 的 rope另外,`rope<char>` 可以写为 `crope`。#### 操作**这里只讲数组 rope 的使用方法。**在接下来的说明中,使用 $a$ 作为一个 rope。4 行未改动- `a.append(x)`:同 `a.push_back(x)`- `a.substr(p,x)`:从 $pos$ 位置向后截取 $x$ 个数,并返回一个 rope。- rope 支持 `clear()`,`empty()`,`begin()`,`end()`,`size()`。- rope 支持 `for(auto i:a)` 格式的访问。### rope 强在哪里?rope 是用来做可持久化的。如果开 $n$ 个 rope,每两个 rope 之间只有单点修改的差异,那么它的空间复杂度是 $O(n\sqrt n)$,并非 $O(n^2)$。例题:[P1383 高级打字机](https://www.luogu.com.cn/problem/P1383)***以上还是简单的应用,接下来的可能有些**玄学**。### rope 的玄学运用例题:[P3402 【模板】可持久化并查集](https://www.luogu.com.cn/problem/P3402)笔者翻阅了网上的[资料](https://zhuanlan.zhihu.com/p/675904773),发现这篇文章的代码并不是在线的,并且会被卡掉。:::info[资料代码]```cpp#include<bits/stdc++.h>86 行未改动```:::同样,还有一篇题解的::::info[题解代码]```cpp#include <bits/stdc++.h>#include <ext/rope>102 行未改动全部展开而笔者的呢?PS:[这个帖子](https://www.luogu.com.cn/discuss/1215399)其实是我发的,我被【】了。:::info[代码]```cpp lines=25-25#include<bits/extc++.h>using namespace std;76 行未改动}```:::$T=\sqrt n\log n$ 时非常快。此时相当于 $\dfrac{\sqrt n}{n\log n}$ 的概率进行路径压缩。这正与我发的帖子中的 $917$ 接近。我当时只尝试了 $T=1,91,917$ 这三个数。~~细节~~。这样使得由原来的 $O(m\sqrt n\log n)$ 变为【神秘】。~~我也不会证啊。~~**不加任何快读,最慢点的是 $\bm{300ms}$。**123456789101112131415161718192021222324252627282930313233343536373 行未改动414243444546474849505152535455565758594 行未改动64656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611786 行未改动204205206207208209210211212102 行未改动全部展开31531631731831932032132232376 行未改动400401402403404405406407408409410411412413414415upd:rope 确实是 $O(\log n)$。不过是巨大常数的。据说这个玩意其实是一开始只有 `crope` 的。因此其实他不是用来搞 OI 的。***关于 rope 的时间复杂度一直有争议,官方文档是描述为 $O(\log n)$,可是实际表现并非如此。### 声明:::warning[测试]使用学校机房,$\text{10th Gen Intel(R) Core(TM)i5 }$,内存 $16\ GB$。#### 结果| 代码 | $n=10^5$ | $n=5\times 10^5$ | $n=10^6$ | $n=5\times 10^6$ || -------- | :-------: | :--------------: | :------------: | :--------------: || Treap | $62.5ms$ | $109.4ms$ | $187.5ms$ | $843ms$ || rope | $453.1ms$ | $\red{2547ms}$ | $\red{5109ms}$ | 不想跑了 || 写假了的 AVL | - | - | - | $3047ms$ || AVL | - | - | - | $1391ms$ || BIT | - | - | - | $\green{140ms}$ |:::注明:rope 在 CCF 的大多数赛事中可以使用,~~笔者已经亲身试毒。~~### 基础用法#### 引用```cpp#include<ext/rope>using namespace __gnu_cxx;```3 行未改动```cpp#include<bits/extc++.h>using namespace __gnu_cxx;```#### 定义与 vector 类似,`rope<type>` 即可定义。`rope<type> rp(100000)` 可以定义一个大小为 $100000$ 的 rope。另外,`rope<char>` 可以写为 `crope`。#### 操作**这里只讲数组 rope 的使用方法。**在接下来的说明中,使用 $a$ 作为一个 rope。4 行未改动- `a.append(x)`:同 `a.push_back(x)`- `a.substr(p,x)`:从 $pos$ 位置向后截取 $x$ 个数,并返回一个 rope。- rope 支持 `clear()`,`empty()`,`begin()`,`end()`,`size()`。- rope 支持 `for(auto i:a)` 格式的访问。### rope 强在哪里?首先,rope 调用方法简单,比某 pbds 简单多了。```cpp__gnu_pbds::tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>t;```rope 是用来做可持久化的。**因为 rope 的复制(即为 `A=B`,$\bm{A,B}$ 都是 rope 类型)是 $\bm{O(1)}$ 的。它只有在接下来有修改的时候复制。**开 $n$ 个满的 rope 并不会 MLE。你可以把它理解成一个可持久化的数组。同样,把线段树的数组换为 rope 即可做可持久化线段树,但是不建议。谁愿意使用一个每次平衡都要开节点而且没有预分配空间的可持久化平衡树作为数组呢。---例题:[P3919 【模板】可持久化线段树 1(可持久化数组)](https://www.luogu.com.cn/problem/P3919)例题:[P1383 高级打字机](https://www.luogu.com.cn/problem/P1383)按照题意模拟即可。| $opt$ | 对应的 rope 操作 || ----- | ------------ || Type | `push_back` || Undo | 回滚到 `rp[x]` || Query | `rp[ver][x]` |---例题:[P3835 【模板】可持久化平衡树](https://www.luogu.com.cn/problem/P3835),你可以尝试拿到 $\bm{\green{92pts\sim96pts}}$。由于 hack 数据的存在,rope 并不能通过这道题。题单:[pbds 与 rope 的应用](https://www.luogu.com.cn/training/460959#information)---以上还是简单的应用,接下来的可能有些**玄学**。### rope 的玄学运用(或许也是并查集的优化?)例题:[P3402 【模板】可持久化并查集](https://www.luogu.com.cn/problem/P3402)**笔者翻阅了网上的[资料](https://zhuanlan.zhihu.com/p/675904773),发现这篇文章的代码并不是在线的,并且会被卡掉。**:::info[资料代码]```cpp#include<bits/stdc++.h>86 行未改动```:::同样,还有一篇题解的::::info[题解代码]```cpp#include <bits/stdc++.h>#include <ext/rope>102 行未改动全部展开而笔者的呢?PS:[这个帖子](https://www.luogu.com.cn/discuss/1215399)其实是我发的,我被【】了。:::info[代码]```cpp lines=25-25#include<bits/extc++.h>using namespace std;76 行未改动}```:::$T=\sqrt n\log n$ 时非常快。此时相当于 $\dfrac{\sqrt n}{n\log n}$ 的概率进行路径压缩。这正与我发的帖子中的 $917$ 接近。**不加任何快读,最慢点的是 $\bm{300ms}$。这已经是~~吊打~~优于同学的主席树的解法了。**虽然我也不知道什么原理。可能是恰好让 rope 少进行 `_S_balance ` 吧。### 总结不知道有啥用。可能时间紧写不完或者赛时调不出来(可持久化)平衡树啥的就有点用吧。