标题
增加 1 行删除 1 行
1珂朵莉树与其部分应用和扩展1珂朵莉树
正文
增加 322 行删除 11 行
92 行未改动9394959697989910010110210310410510610710810911011111211311411511611711811912012167 行未改动18919019119219319419519619719819920020120220320420520620720892 行未改动return mp.insert(it,{x,it->second});}```虽然 `split` 操作比 `set` 的实现简单了,但其他操作明显会比 `set` 的实现难写,所以需要按情况选择实现方式。## 例题### CF896C,*2600这似乎是第一道 std 是 ODT 的题目,和那个指令集暴打标算的第二分块是同一场比赛的。题目简述:写一个数据结构维护四种操作:- 区间加- 区间推平- 区间 kth- $\sum^r_{i=l}a_i^x\%y$$n,m\le10^5$,操作类型和参数随机生成因为数据随机生成,所以有 $O(\frac{n}{4})$ 的操作是 $2$,所以总颜色段数是 $O(\log n)$ 的。::::info[证明]我不会,但是找到了[这篇文章](https://www.luogu.com.cn/article/hu3a0img)证得很好。::::对于区间加,暴力扫描区间的颜色段并加 $v$,区间推平如上面的 `assign`。因为段数是 $O(\log n)$ 的,所以上述两个操作时间复杂度是 $O(\log n)$。对于区间 kth,取出区间中的每个颜色段(而不是每个数)排序后查找,时间复杂度 $O(\log n\log\log n)$,如果使用 P1923 的类似快排的分治,时间复杂度是最差 $O(\log^2n)$,平均 $O(\log n)$ 的。对于最恶心的操作 $4$,暴力扫描区间中的每个颜色段,用快速幂求 $\text{len}v^x\%y$,时间复杂度 $O(\log n\log V)$总时间复杂度 $O(n\log n\log V)$。### 贴海报::::info[题面(由 GPT-5 提取)]67 行未改动}```爆锤线段树,成功拿下农夫乐事最优解。::::至此,你可以解决的题目有:- [P4344 [SHOI2015] 脑洞治疗仪](https://www.luogu.com.cn/problem/P4344)- [P2824 [HEOI2016/TJOI2016] 排序](https://www.luogu.com.cn/problem/P2824) (你需要学会整体二分)- [P5350 序列](https://www.luogu.com.cn/problem/P5350) ODT中的大模拟,来一起赤石- [P8512 [Ynoi Easy Round 2021] TEST_152](https://www.luogu.com.cn/problem/P8512) ,[我的题解](https://www.luogu.com.cn/article/yvdnxrye)- [P13983 数列分块入门 8](https://www.luogu.com.cn/problem/P13983)- [P14228 [ICPC 2024 Kunming I] 漫步野径](https://www.luogu.com.cn/problem/P14228) ,[我的题解](https://www.luogu.com.cn/article/7xys7m8y)- [P3071 [USACO13JAN] Seating G](https://www.luogu.com.cn/problem/P3071)- [P2146 [NOI2015] 软件包管理器](https://www.luogu.com.cn/problem/P2146)- [P2486 [SDOI2011] 染色](https://www.luogu.com.cn/problem/P2486)- [CF915E Physical Education Lessons](https://codeforces.com/problemset/problem/915/E)# 珂朵莉树与定期重构以下内容参考了[这篇文章](https://www.luogu.com.cn/article/0mys9qkh)先咕咕咕,期中考完再更。92 行未改动9394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120267 行未改动27027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851992 行未改动return mp.insert(it,{x,it->second});}```虽然 `split` 操作比 `set` 的实现简单了,但其他操作明显会比 `set` 的实现难写,所以需要按情况选择实现方式。## ODT 的 vector 实现下面讲一个 [OI-wiki](https://oi-wiki.org/misc/odt/) 上没有的维护方式:### 为什么要用 vector 维护 ODT阅读[这篇文章](https://www.luogu.com.cn/article/hu3a0img)的证明可知,ODT 在随机操作下段数为 $O(\log n)$ 级别,在大部分的题目中,ODT 的正确性也是基于段数少。而在数据这么小的情况下,`set/map` 大常数的 $O(\log)$ 时间复杂度并没有优势。现在写 ODT 的 OIer 大约有 $70\%$ 只会 `set` 的实现,$20\%$ 会 `set` 和 `map`,剩下的会神奇的数据结构,现在流传的比较广的只有红黑树和链表维护。`vector` 维护 ODT 的大体思路就是让 `vector` 模拟 `set`。::::info[为什么不用普通数组]两者虽然功能类似,但 `vector` 有着更优秀的插入常数(约 $\frac{1}{4}$),而且 `vector` 比数组更好写。::::::::info[vector 速成]- `vector<node>::iterator`:迭代器,支持迭代器相减,迭代器加减整数- `insert(it,x)` 在 `it` 前插入 `x`- `erase(itl,itr)` 删除 $[itl,itr)$ 间的所有内容。::::### split由于 `vector` 支持随机访问,所以自然有迭代器版和下标版。先是下标版实现:```cppstruct node{int l,r,v;inline bool operator<(int v)const{return l<v;}};vector<node>s;inline int split(int x) {int i=lower_bound(s.begin(),s.end(),x)-s.begin();if(i<s.size()&&s[i].l==x)return i;//刚刚匹配到node t=s[--i];if(t.r<x)return s.size();//边界s[i].r=x-1;//由于是 vector,无需 mutable 关键字即可修改return s.insert(s.begin()+i+1,{x,t.r,t.v}),i+1;}```敲重点:如果你是这么写的,**需要先** $\textbf{split(l)}$ **再** $\textbf{split(r+1)}$。如果反过来,`split(r+1)` 得到的下标 $i$ 对应的数会被 `split(l)` 分裂出来的新区间往后顶一格。Q:既然这样,我可以用 `i=split(r+1)+1` 规避这个问题吗A:不建议,因为前面可能没有分裂出新区间,需要特判。下面是迭代器实现:::::info[code]```cppinline vector<node>::iterator split(int x) {auto i=lower_bound(s.begin(),s.end(),x);if(i!=s.end()&&(*i).l==x)return i;node t=*--i;if(t.r<x)return s.end();(*i).r=x-1;return s.insert(i+1,{x,t.r,t.v}),i+1;}```::::并没有什么重要的地方。### assign 与 perform这里只管下标实现。```cppinline void assign(int l,int r,int v){int L=split(l),R=split(r+1);s.erase(s.begin()+L,s.begin()+R);s.insert(lower_bound(s.begin(),s.end(),l),{l,r,v});}```与其他维护方式差异不大,最重要的是先 `split(l)` 再 `split(r)`。### 时间复杂度珂朵莉树依赖数据随机生成,需要期望 $O(n)$ 的操作是 `assign`,所以总颜色段数是 $O(\log n)$ 的。::::info[证明]我不会,但是找到了[这篇文章](https://www.luogu.com.cn/article/hu3a0img)证得很好。::::vector 实现:`split` 复杂度是 $O(\log\log n)$ 不变,这是显然的。`assign` 的两个 `split` 贡献了 $O(\log \log n)$ 的时间复杂度,因为总段数 $O(\log n)$,所以下面两个 `vector` 的操作时间复杂度 $O(\log n)$,总时间复杂度 $O(\log n)$。`perform` 与 `assign` 情况相同。`set` 和 `map` 实现具体可以看 CF896C 部分。## 例题### CF896C,*2600这似乎是第一道 std 是 ODT 的题目,和那个指令集暴打标算的第二分块是同一场比赛的。题目简述:写一个数据结构维护四种操作:- 区间加- 区间推平- 区间 kth- $\sum^r_{i=l}a_i^x\bmod y$$n,m\le10^5$,操作类型和参数随机生成因为数据随机生成,所以有期望 $\frac{n}{4}$ 的操作是 $2$,所以总颜色段数是 $O(\log n)$ 的。对于区间加,暴力扫描区间的颜色段并加 $v$,区间推平如上面的 `assign`。因为段数是 $O(\log n)$ 的,所以上述两个操作时间复杂度是 $O(\log n)$。对于区间 kth,取出区间中的每个颜色段(而不是每个数)排序后查找,时间复杂度 $O(\log n\log\log n)$,如果使用 P1923 的类似快排的分治,时间复杂度是最差 $O(\log^2n)$,平均 $O(\log n)$ 的。对于最恶心的操作 $4$,暴力扫描区间中的每个颜色段,用快速幂求 $\text{len}v^x\bmod y$,时间复杂度 $O(\log n\log V)$总时间复杂度 $O(n\log n\log V)$。### 贴海报::::info[题面(由 GPT-5 提取)]67 行未改动}```爆锤线段树,成功拿下农夫乐事最优解。::::### [P8512 [Ynoi Easy Round 2021] TEST_152](https://www.luogu.com.cn/problem/P8512)::::info[题意]有长度为 $n$ 的操作序列,每个操作是区间推平,$l,r\le m$。$q$ 次询问,每次对全零序列依次执行操作数列的 $l\sim r$ 项,求最后数组和,可以离线。::::[我古早的题解](https://www.luogu.com.cn/article/yvdnxrye)考虑计算每个操作的贡献,因为每个操作的加入只会影响在她之前的操作的贡献,所以如果 $r$ 相等,那么对于任意的 $l$,这段区间的答案都是 $l$ 到 $r$ 的贡献和。所以考虑在 $r$ 上挂扫描线回答。下面只用解决维护贡献了,区间覆盖是珂朵莉树可以维护的,每个颜色段的信息有左右端点、数和操作时间,时间复杂度证明可以见上文贴海报。至此,你可以解决的题目有:- [P4344 [SHOI2015] 脑洞治疗仪](https://www.luogu.com.cn/problem/P4344)- [P2824 [HEOI2016/TJOI2016] 排序](https://www.luogu.com.cn/problem/P2824) (你需要学会整体二分)- [P5350 序列](https://www.luogu.com.cn/problem/P5350) ODT中的大模拟- [P13983 数列分块入门 8](https://www.luogu.com.cn/problem/P13983)- [P3071 [USACO13JAN] Seating G](https://www.luogu.com.cn/problem/P3071)- [P2146 [NOI2015] 软件包管理器](https://www.luogu.com.cn/problem/P2146)- [P2486 [SDOI2011] 染色](https://www.luogu.com.cn/problem/P2486)- [CF915E Physical Education Lessons](https://codeforces.com/problemset/problem/915/E)# 珂朵莉树与定期重构## [P3391 【模板】文艺平衡树](https://www.luogu.com.cn/problem/P3391)以下内容和[这篇文章](https://www.luogu.com.cn/article/0mys9qkh)有交集,您可以参考阅读。::::info[问题 1]{open}给定一个长度为 $n$ 数列和 $m$ 个操作,每个操作给出 $l,r,s,d$,表示求 $[l,r]$ 的和,并把 $[l,r]$ 替换为首项为 $s$,公差为 $d$ 的等差数列。$n,m\le10^6$,强制使用珂朵莉树(:::success[解法]因为每个操作都还是区间覆盖,所以理论上可以 ODT。在之前的文章中,ODT 每个节点的信息都是简单的,现在一个节点需要表示一段等差数列,需要维护 ODT 必要的 $l,r$,还有等差数列要的首项、公差。其他操作和普通珂树大差不差,比较难写的是 `split`,稍微推下公式也好做。::::::::::::info[问题 2]{open}已知一个长度为 $n$ 的数列 $\{a_i\}$,你需要进行下面两种操作一共 $m$ 次:1. 将某区间每一个数加上 $k$。2. 求出某区间每一个数的和。$n,m\le10^5$,强制使用珂朵莉树(::::info[Hint 1]如果原数组为全零的,使用珂朵莉树暴力修改并查询 $x$ 次的时间复杂度是多少?:::success[解答]修改+查询 $x$ 次后会有 $O(x)$ 段,时间复杂度是 $O(x^2+x\log x)$ 的。:::::::::::info[Hint 2]如何把这题的数组转为全零的(换句话说,是否可以 $O(n)$ 预处理,然后 $O(1)$ 查找区间和?):::success[解答]前缀和。:::::::::::info[Hint 3]你学过的什么技巧可以平衡时间复杂度?:::success[解答]定期重构。:::::::::::success[解法]首先建议你先看一遍所有 Hint。回顾:根据 Hint2 的解法,可以在 $O(n)$ 的时间内重构数组。又根据 Hint1,可以 $O(x^2+x\log x)$ 的解决连续 $x$ 个操作。考虑定期重构,每 $B$ 个操作重构一次,则时间复杂度是:$$O(\frac{n}{B}(B^2+B\log B)+\frac{n^2}{B})$$当 $B=\sqrt{n}$ 时有最优时间复杂度 $O((n+\log n)\sqrt{n})$。当然,都询问分块了不用珂朵莉树维护也行。:::info[code]```cppnamespace Chtholly{struct node{int l,r;mutable int v;friend bool operator<(node a,node b){return a.l<b.l;}node(int L,int R=0,int V=0):l(L),r(R),v(V){}};set<node>s;int sum[100005];int a[100005];inline auto split(int x){auto it=s.lower_bound(node(x));if(it->l==x)return it;auto tmp=*(--it);s.erase(it);s.insert(node(tmp.l,x-1,tmp.v));return s.insert(node(x,tmp.r,tmp.v)).first;}inline void add(int l,int r,int v){auto itr=split(r+1);for(auto it=split(l);it!=itr;++it)it->v+=v;}inline void query(int l,int r){int ans=sum[r]-sum[l-1];auto itr=split(r+1);for(auto it=split(l);it!=itr;++it)ans+=it->v*(it->r-it->l+1);out<<ans<<"\n";}int n,m;void reset(){for(auto i:s)for(int j=i.l;j<=i.r;j++)a[j]+=i.v;for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];s.clear();s.insert(node(0,n+1));}const int len=300;void main(){in>>n>>m;for(int i=1;i<=n;i++)in>>a[i];reset();for(;m--;){int opt,l,r;in>>opt>>l>>r;if(opt-1)query(l,r);else{int v;in>>v;add(l,r,v);}if(m%len==0)reset();}}}```[提交记录](https://www.luogu.com.cn/record/246932851)::::::::::::珂朵莉树维护维护区间每个数下标的**映射**(注意不是对映的值)::::info[细说映射]维护长度为 $n$ 映射数组 $\text{p}$,是一个 $1\sim n$ 的排列,表示 $a_i=\text{tmp}_{\text{p}_{i}}$,$\text{tmp}$ 表示原来的数组,$\text{a}$ 表示映射后的结果。::::下面思考如何表示一个映射:未进行操作世的映射数组是 $1,2,3,\dots,n$。每次操作会翻转一段区间,就是变成类似 $1,2,3,6,5,4,7,8,9$ 的东西,观察到最开始的映射数组是 $1\sim n$,公差为 $1$ 的等差数列,翻转后也只是将一段取出来,依次翻转(公差 $d\rightarrow-d$),最后操作的结果也只是段数增加了 $O(1)$(最多为 $3$)段的等差数列拼在一起的数列。所以可以考虑用多段公差为 $-1/1$ 的等差数列表示映射数组,那么要记录的信息有:区间左端点 `l`,区间右端点 `r`,等差数列首项 `f`,公差 `d`,~~项数~~(可以使用 `r-l+1` 算出来)。设从初始状态进行了 $x$ 次操作,则这 $x$ 次操作的总时间复杂度是 $O(x^2+x\log x)$。维护映射的好处是可以重构数组,每次重构的时间复杂度是 $O(n)$,再设 $B$ 为重构间隔,则总时间复杂度为:$$O(\frac{n^2}{B}+(\frac{n}{B})^3+(\frac{n}{B})^2\log\frac{n}{B})$$化简得:$$O(\frac{n^2}{B}+(\frac{n}{B})^3)$$当$$\frac{n^2}{B}=(\frac{n}{B})^3,B=n^{0.5}$$时有最优时间复杂度$$O(n^{1.5})$$::::info[code]```cpp#include <bits/stdc++.h>using namespace std;#ifdef __linux__#define gc getchar_unlocked#define pc putchar_unlocked#else#define gc getchar#define pc putchar#define __int128 __int128_t#endiftemplate<typename T> inline void read(T&a){a=0;char ch=gc();bool ok=0;for(;ch<'0'||ch>'9';)ok^=(ch=='-'),ch=gc();for(;ch>='0'&&ch<='9';)a=(a<<1)+(a<<3)+(ch^48),ch=gc();if(ok)a=-a;}template<typename T> inline void write(T a){if(a==0){pc('0');return;}static char ch[10];int till=0;if(a<0){pc('-');for(;a;)ch[till++]=-(a%10),a/=10;}else for(;a;)ch[till++]=a%10,a/=10;for(;till;)pc(ch[--till]^48);}namespace MYcode{struct node{int l,r,f,d;node(int L,int R=0,int F=0,int D=0):l(L),r(R),f(F),d(D){};inline friend bool operator<(node a,node b){return a.l<b.l;}};set<node>s;int a[100005],tmp[100005];inline set<node>::iterator split(int x){set<node>::iterator it=s.lower_bound(node(x));if(it!=s.end()&&it->l==x)return it;--it;if(it->r<x)return s.end();node tmp=*it;s.erase(it);s.insert(node(tmp.l,x-1,tmp.f,tmp.d));return s.insert(node(x,tmp.r,tmp.f+(x-tmp.l)*tmp.d,tmp.d)).first;}void main(){int n,m;read(n);read(m);s.insert(node(1,n,1,1));for(int i=1;i<=n;i++)a[i]=i;int B=sqrt(n)*0.12;//微调块长int cnt=0;for(;m--;){++cnt;if(cnt>=B){cnt-=B;for(int i=1;i<=n;i++)tmp[i]=a[i];for(set<node>::iterator it=s.begin();it!=s.end();++it){node i=*it;for(;i.l<=i.r;++i.l,i.f+=i.d)a[i.l]=tmp[i.f];}s.clear();s.insert(node(1,n,1,1));}int l,r;read(l);read(r);set<node>::iterator itr=split(r+1),itl=split(l);vector<node>e;for(set<node>::iterator it=itl;it!=itr;++it){e.push_back(node(r-it->r+l,r-it->l+l,it->f+(it->r-it->l)*it->d,-it->d));}s.erase(itl,itr);for(int i=0;i<e.size();i++)s.insert(e[i]);}for(set<node>::iterator it=s.begin();it!=s.end();++it){node i=*it;for(;i.l<=i.r;++i.l,i.f+=i.d)tmp[i.l]=a[i.f];}for(int i=1;i<=n;i++){write(tmp[i]);pc(' ');}}}signed main(){MYcode::main();return 0;}```::::目前最优解也是珂朵莉树,但是我人傻常数大,只拿下 rk3(尽力了。卡常可以看[这篇](https://www.luogu.com.cn/article/r0fg6kqg),因为在教程里写没啥意义就不放了。