Chtholly Tree
But now I call it Chtholly Tree~
珂朵莉树是一种优雅的暴力优化策略。
其主要思想是通过将一段值相同的区间统一存储到一个结构体中,如:
struct Node{
int l,r;
mutable ll v;//让 v 在任何时候都可变,即使在 set 中
Node (int l,int r,ll v):l(l),r(r),v(v){}
bool operator < (const Node x) const{return l<x.l;}
};我们通过一些数据结构来维护这些结构体(如 std::set,std::map,链表等),这里只介绍 std::set 写法,其余可以前去 oi-wiki 上学习。
主要函数
split
auto split(int pos) {
auto it=pq.lower_bound(Node(pos,0,0));//找到第一个 l>=pos 的区间(lower_bound!!!)
if(it!=pq.end()&&it->l==pos) return it;//如果已经在 pos 分开了,就直接返回
--it;
int l=it->l,r=it->r; ll v=it->v;
pq.erase(it);pq.insert(Node(l,pos-1,v));//在 set 中删去 [l,r],加入 [l,pos)
return pq.insert(Node(pos,r,v)).first;//加入 [pos,r] 并返回迭代器
}将区间 ,分裂成区间 和 ,并返回 所在区间的迭代器。详情看代码细节。
assign
assign 函数是 Chtholl Tree 保证复杂度的重要函数,具体的时间复杂度分析可以见后文。
auto assign(int l,int r,ll v) {
auto itr=split(r+1),itl=split(l);//一定先 split(r+1),再 split(l),否则可能会使迭代器失效
//因为 erase 会使指向被删去节点的迭代器失效,而若先 split(l),则split(r+1) 时可能访问到 itl 指向的节点
//会把itl指向的节点删去,那么itl就失效了
pq.erase(itl,itr);//删去 [itl,itr) 中所有的区间
pq.insert(Node(l,r,v));//加入一个新区间
}assign 实现了将 区间全部赋值为 ,细节见代码。
perform
void perform(int l,int r,ll v) {
auto itr=split(r+1),itl=split(l);
for (auto it=itl;it!=itr; ++it) {
;//想要执行的操作
}
}perform 函数实现遍历 所有区间,并可以在其中执行一些操作。
该函数在复杂度分析中具有诡异的重要作用。
复杂度分析
推式子ing…
首先我们可以非常容易地构造一组没有区间覆盖的数据(也就是只查询)然后卡掉 Chtholly Tree。所以,我们先分析 assign 函数的复杂度。
考虑一共会加入多少个基本段,由于每次 assign 操作只会加入一个基本段,最多分割出新的两个基本段,所以每次操作基本段增加数量是 的。所以一共就是 个基本段。又因为每个基本段只会被删除一次,所以总的时间复杂度就是 (实际上这是最坏时间复杂度)。
这就是只考虑 assign 操作的 Chtholly Tree 时间复杂度,类似如果每次是先对一段区间 perform 后立刻 assign,时间复杂度也是如此。
接下来考虑在纯随机数据下,Chtholly Tree 的时间复杂度。
先给结论:用 set 实现的 Chtholly Tree 时间复杂度是 。
证明
以下内容来自 这篇文章,并经过编者修改。
Lemma1:
记录每次操作的基本段数为 ,每次删除的基本段数为 。那么我们有
又因为总基本段数为 ,而每段最多删除一次,所以有
我们对一式左右两边求和,并结合二式,则有
所以 。
Lemma2:
记 为对于端点 ,在 次 assign 操作中一直为端点,即不被覆盖。
我们考虑在一次 assign 操作中端点 不被覆盖的概率 ,那么在 次 assign 操作后端点 仍然存在的概率 。
接着我们考虑放缩 。
积分(不想看的可以跳过或借助工具)
实际上,,但是我不会证。
实际上,原文里还有一段说明当 时,这个上界是不紧的,但是我不想证了。
实际上,上面两句话,大家可以自己用一些数学工具感性理解一下。
Lemma3:
记录总基本段数为 ,考虑在经过 次操作后 的期望。
直观上,我们知道在 次操作中会有 次 assign 操作;严格地说,令 为 操作中 assign 操作的数量,则 。
根据 Chernoff bound,我们有
然后,我们若想求出 ,只需要考虑每一个端点在 次操作后存在的概率即可。
对于一个端点,其在 次操作后仍然存在的概率 。
一开始有 个端点,每次操作会创建 个端点,而倒数第 次创建的端点最后仍然存在的概率 。求和后可得 (根据 Lemma2 中的补充境况,在 时,更紧的上界是 ,详情见原文,我不会)。特别地,令 为第 的操作后的总基本段数,那么 。
而实际上 Chtholly Tree 的时间复杂度只跟每次操作找到需要操作的区间的时间复杂度有关。对于用 set 实现的 Chtholly Tree,根据 Lemma3,则查找时间复杂度为 。根据 的上凸性和琴生不等式,我们有 。
为了达到理论复杂度,要注意在构造 set 时直接构造,而非一个一个元素 insert(即使用范围构造函数,详见文档)。然后插入元素时,由于我们已经知道了插入元素的位置,也可以实现 的时间复杂度(即使用文档中 (3,4) 插入方式)。删除时,则通过迭代器删除,也是 的(文档中 (1,2) 删除方式)。
当然,要是懒的话,基本上也没人会卡 的时间复杂度(链表实现的也是 )。
后话:实际上,用 Chtholly Tree 的地方大多数只用第一种复杂度分析,而大多数题因为不是随机数据,也就用不到第二种复杂度分析。所以更多题只是用到在执行 assign 操作 次后只会增加 个基本段数这个结论和其推论,然后与其他数据结构相结合。
例题
CF896C
洛谷 Codeforces
这是 Chtholly Tree 的起源题,约等于模板题了。
阅读题面,我们发现操作 2 会对一段区间重新赋值,又因为生成的数据是随机的,所以想到 Chtholly Tree(本来就是板子)。
对于操作 2,我们执行 assign 操作。对于操作 1,3,4 我们通过不同 perform 函数来完成,时间复杂度均摊为 (好吧,因为我十分懒,所以下面这份代码的时间复杂度为 ,嘿嘿)。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
#define _ =read()
ll fast_pow(ll x,ll y,ll p) {
if(y==0) return 1;if(y==1) return x%p;
ll tmp=fast_pow(x,y>>1,p);
if(y&1) return tmp*tmp%p*x%p;
else return tmp*tmp%p;
}
//Chtholly_Tree origin
struct Chtholly_Tree{
struct Node{
int l,r;
mutable ll v;
Node (int l,int r,ll v):l(l),r(r),v(v){}
bool operator < (const Node x) const{return l<x.l;}
};
set<Node> pq;
auto split(int pos) {
auto it=pq.lower_bound(Node(pos,0,0));
if(it!=pq.end()&&it->l==pos) return it;
--it;
int l=it->l,r=it->r; ll v=it->v;
pq.erase(it);pq.insert(Node(l,pos-1,v));
return pq.insert(Node(pos,r,v)).first;
}
auto assign(int l,int r,ll v) {
auto itr=split(r+1),itl=split(l);
pq.erase(itl,itr);
pq.insert(Node(l,r,v));
}
void perform1(int l,int r,ll v) {
auto itr=split(r+1),itl=split(l);
ll ans=0;
for (auto it=itl;it!=itr; ++it) it->v+=v;
}
ll perform3(int l,int r,ll x) {
auto itr=split(r+1),itl=split(l);
ll ans=0;
vector<Node> cnt;
for (auto it=itl;it !=itr; ++it) cnt.push_back(*it);
sort(cnt.begin(),cnt.end(),[](const Node x,const Node y){
return x.v<y.v;
});
for (auto it:cnt) {
x-=it.r-it.l+1;
if(x<=0) return it.v;
}
return -1;
}
ll perform4(int l,int r,ll x,ll y) {
auto itr=split(r+1),itl=split(l);
ll ans=0;
for (auto it=itl;it !=itr; ++it) ans=(ans+fast_pow(it->v%y,x,y)*(0ll+it->r-it->l+1)%y)%y;
return ans;
}
}tree;
ll n,m,seed,vmax;
int rnd() {
ll ret=seed;seed=(seed*7+13)%1000000007;
return ret;
}
int op,l,r,x,y;
signed main(){
n _,m _,seed _,vmax _;
tree.pq.insert(Chtholly_Tree::Node(1,n,0));
for (int i = 1; i <= n; i++) tree.assign(i,i,(rnd()%vmax)+1);
for (int i = 1; i <= m; i++) {
op=(rnd()%4)+1,l=(rnd()%n)+1,r=(rnd()%n)+1;
if (l > r) swap(l, r);
if (op == 3) x=(rnd()%(r-l+1))+1;
else x=(rnd()% vmax)+1;
if (op == 4) y = (rnd()%vmax)+1;
switch(op) {
case 1:
tree.perform1(l,r,x);
break;
case 2:
tree.assign(l,r,x);
break;
case 3:
printf("%lld\n",tree.perform3(l,r,x));
break;
case 4:
printf("%lld\n",tree.perform4(l,r,x,y));
break;
}
}
return 0;
}P1940
洛谷
发现有区间赋值,考虑 Chtholly Tree,然后就是板子了。查询直接暴力扫一遍就行了。
注意到这里实际上可以通过各种构造数据的方式卡掉 Chtholly Tree。(也就是复杂度没有保证,只是数据太水了)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
#define _ =read()
struct Chtholly_Tree{
struct Node{
int l,r;
mutable ll v;
Node (int l,int r,ll v):l(l),r(r),v(v){}
bool operator < (const Node x) const{return l<x.l;}
};
set<Node> pq;
auto split(int pos) {
auto it=pq.lower_bound(Node(pos,0,0));
if(it!=pq.end()&&it->l==pos) return it;
--it;
int l=it->l,r=it->r; ll v=it->v;
pq.erase(it);pq.insert(Node(l,pos-1,v));
return pq.insert(Node(pos,r,v)).first;
}
auto assign(int l,int r,ll v) {
auto itr=split(r+1),itl=split(l);
pq.erase(itl,itr);
pq.insert(Node(l,r,v));
}
int perform(int l,int r) {
auto itr=split(r+1),itl=split(l);
ll ans=0;
for (auto it=itl;it !=itr; ++it) if(it->v==1) ans+=it->r-it->l+1;
return ans;
}
}tree;
int n,m,l,r;
signed main(){
n _,m _;
tree.pq.insert(Chtholly_Tree::Node(1,n,1));
while(m--) {
l _,r _;
tree.assign(l,r,0);
printf("%d\n",tree.perform(1,n));
}
return 0;
}CF915E
洛谷 Codeforces
经过读题,发现就是 P1840 的变体,我们把 perform 修改一下就能过了~~吗?
这道题略微卡 Chtholly Tree,考虑每次 perform 只遍历有修改的区间,并更新总和,这样快到飞起~
此时时间复杂度就是前面复杂度分析说的第一种情况的执行 perform 后立刻对同一段区间 assign 一遍,所以复杂度有保证。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
#define _ =read()
struct Chtholly_Tree{
struct Node{
int l,r;
mutable ll v;
Node (int l,int r,ll v):l(l),r(r),v(v){}
bool operator < (const Node x) const{return l<x.l;}
};
set<Node> pq;
auto split(int pos) {
auto it=pq.lower_bound(Node(pos,0,0));
if(it!=pq.end()&&it->l==pos) return it;
--it;
int l=it->l,r=it->r; ll v=it->v;
pq.erase(it);pq.insert(Node(l,pos-1,v));
return pq.insert(Node(pos,r,v)).first;
}
auto assign(int l,int r,ll v) {
auto itr=split(r+1),itl=split(l);
pq.erase(itl,itr);
pq.insert(Node(l,r,v));
}
int perform(int l,int r,ll v) {
auto itr=split(r+1),itl=split(l);
ll ans=0;
for (auto it=itl;it !=itr; ++it) if(it->v==v) ans+=it->r-it->l+1;
return ans;
}
}tree;
int n,m,l,r,k;
ll sum;
signed main(){
n _,m _;sum=n;
tree.pq.insert(Chtholly_Tree::Node(1,n,2));
while(m--) {
l _,r _,k _;
k==1?sum-=tree.perform(l,r,2):sum+=tree.perform(l,r,1);//优化
printf("%d\n",sum);
tree.assign(l,r,k);
}
return 0;
}注意到,上述第一题为复杂度分析中的第二种情况,第三题为第一种情况。而第二道题复杂度是错的(嘿嘿嘿嘿)。
习题
[Ynoi Easy Round 2016] 镜中的昆虫
洛谷
lxl 发明的算法怎么能少了 Ynoi 的习题呢。
首先一看到题面,发现有区间赋值操作,立马想到直接拍一个 Chtholly Tree 的板子上去,然后就结束了。
提交记录
这能过就见鬼了。
思路借鉴了这篇题解。
非常显然,只要询问次数过多,那时间复杂度分分钟卡成 。所以还是老老实实做题吧。
首先,我们如何快速计算一段区间内元素个数。
常见的计数方法是只统计一段区间内每个第一出现的每个元素。刻画一下,就是定义 为上一次该元素的出现位置,然后统计区间 内满足 的 的个数。如果把 看作平面上的一个点,那么询问就是计算一个矩形内部点的个数。即二维矩形数点。如果没有 1 操作,那就是离线二维数点的板子,直接扫描线就行了。
然后考虑单点修改,显然,单点修改时, 的变化是 的。具体的,令 为下一次该元素出现位置,只有 和更新前后的 共 个点改变。实现方法上,先离散化,对每个颜色开一个 set 存储哪些点是该颜色,这样就可以知道 。每次修改时就分别更新即可。
接着就变成了动态二维数点,可以用树套树(但因为推荐使用 cdq 分治,且上次模拟赛写树套树直接挂成暴力,所以能写 cdq 分治就写)(并且我也没写过多少次 cdq 分治,这就当练习了),但是我们用 cdq 分治。十分板子,直接略过。
然后接着考虑区间赋值。思路十分简单,把之前对每个点维护一个 改成对基本段维护(具体上是把基本段的 值存到该区间右端点上)(其实存在哪里都可以,但最好统一,我存在右端点上,左端点可能更好)。根据 Chtholly Tree 的 assign 操作性质,累积总基础段为 的,每次加入删除时 变化 次,所以只有 次变化。然后剩下的就一样跑一个 cdq 分治就行了。
总时间复杂度大概是 ,空间 。
有一个技巧是把修改和查询分成两个数组分别存储,这样就极大优化了码风。
细节很多,以下是我踩过的所有坑:
- 确认你的 cdq 分治板子没有写错
- 确认你的 Chtholly Tree 板子没有写错
- 离散化时要把原数组和询问的颜色都离散化,不要只离散化原数组
- 离散化数组的大小要开到 的大小
- 注意时间维最好把查询和修改放到两个数组,这样避免在 cdq 分治处写挂
- 如果空间炸了,可以考虑在相同时间维内部合并相同点
inplace_merge合并是将 和 合并,不是 和- 注意排序
- 建议封装函数
- 回滚树状数组时只用回滚遍历过的修改,不要把没有加到树状数组上的修改也回滚了
- 查询时,因为端点不一定是基本段的端点,所以要
split一下,否则答案会错
(为了调这份代码花了一个下午加上半个上午,中间还写了篇作文,快崩溃了啊啊啊啊)
Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
inline ll operator ^ (const pii &x, const pii &y) {return y.s*x.f-x.s*y.f;}
inline pii operator + (const pii &x, const pii &y) {return pii(x.f+y.f,x.s+y.s);}
inline pii operator - (const pii &x, const pii &y) {return pii(x.f-y.f,x.s-y.s);}
#define cplx pair<double,double>
inline cplx operator + (const cplx &x,const cplx &y) {return cplx(x.f+y.f,x.s+y.s);}
inline cplx operator - (const cplx &x,const cplx &y) {return cplx(x.f-y.f,x.s-y.s);}
inline cplx operator * (const cplx &x,const cplx &y) {return cplx(x.f*y.f-x.s*y.s,x.f*y.s+x.s*y.f);}
bool __flag=false;
const int maxn=100010,maxm=100010;
struct Node{
int l,r;
mutable int v;
bool operator < (const Node &x) const{return l<x.l;}
};
set<Node> pq,num[maxn<<1];
int n,m;
int pre[maxn];
int a[maxn];
pii s[maxn<<1];
int tmp=0;
struct modi{
int t,x,y,k;//(x,y):k
bool operator <(const modi&y) {return x<y.x;}
}p[maxn<<4];
int pid;
struct query{
int t,x,y,id;//(0,0)~(x,y) +-id
bool operator <(const query&y) {return x<y.x;}
}q[maxn<<1];
int qid;
int opt,l,r,x;
void del(Node val) {
assert(__flag);
auto op=num[val.v].upper_bound(val),ed=op--;
assert(op->l==val.l&&op->r==val.r);
if(ed!=num[val.v].end()) p[++pid]={tmp,ed->r,pre[ed->r],-1},pre[ed->r]=pre[op->r],p[++pid]={tmp,ed->r,pre[ed->r],1};
p[++pid]={tmp,op->r,pre[op->r],-1};
pre[op->r]=0;
num[val.v].erase(op);pq.erase(val);
}
auto ins(Node val) {
assert(__flag);
auto ed=num[val.v].upper_bound(val);
int pr=0;
if(!num[val.v].empty()&&ed!=num[val.v].begin()) {pr=(--ed)->r;++ed;}
auto op=num[val.v].insert(ed,val);auto cnt=pq.insert(val).f;
if(ed!=num[val.v].end()) p[++pid]={tmp,ed->r,pre[ed->r],-1},pre[ed->r]=op->r,p[++pid]={tmp,ed->r,pre[ed->r],1};
pre[op->r]=pr;
p[++pid]={tmp,op->r,pre[op->r],1};
return cnt;
}
void check(int pl,int pr) {
if(pr<pl) return;
sort(p+pl+1,p+pr+1,[](const modi&x,const modi&y){return x.x==y.x?x.y<y.y:x.x<y.x;});
int i,j;
for (i = pl,j = pl-1; i <= pr; i++) {
if(j>=pl&&p[i].x==p[j].x&&p[i].y==p[j].y) {p[j].k+=p[i].k;}
else p[++j]=p[i];
if(j>=pl&&p[j].k==0) --j;
}
pid=j;
}
auto split(int op) {//[,op-1][op,]
if(op>n) return pq.end();
auto ed=pq.lower_bound({op,op,0});
if(ed!=pq.end()&&ed->l==op) return ed;
--ed;
Node val=*ed;
del(val);ins({val.l,op-1,val.v});ed=ins({op,val.r,val.v});
return ed;
}
void assign(int l,int r,int col) {
auto ed=split(r+1),op=split(l);
for (auto i=op; i!=ed;) {auto j=*i;++i,del(j);}
ins({l,r,col});
}
int id=0;
int ans[maxn];
const int drt=1;
#define lowbit(op) (op&-op)
struct BIT{
int tr[maxn];
inline void add(int op,int k) {op+=drt;while(op<=n+drt) tr[op]+=k,op+=lowbit(op);}
inline int ask(int op) {op+=drt;int cnt=0;while(op) cnt+=tr[op],op-=lowbit(op);return cnt;}
}tree;
void cdq(int l=0,int r=tmp,int pl=1,int pr=pid,int ql=1,int qr=qid) {
int i,j;
if(l==r) {
sort(p+pl,p+pr+1,[](const modi&x,const modi&y){return x.x<y.x;});
sort(q+ql,q+qr+1,[](const query&x,const query&y){return x.x<y.x;});
for (i = ql,j = pl; i <= qr; ++i) {
while(j<=pr&&p[j].x<=q[i].x) tree.add(p[j].y,p[j].k),++j;
if(q[i].id>0) ans[q[i].id]+=tree.ask(q[i].y);
else ans[-q[i].id]-=tree.ask(q[i].y);
}
for (int i = pl; i < j; i++) tree.add(p[i].y,-p[i].k);
return;
}
int mid=l+r>>1,pmid,qmid;//[l,mid],[mid+1,r]
for (pmid=pl; pmid<=pr; ++pmid) {if(p[pmid].t>mid) {pmid--;break;}} if(pmid>pr) pmid--;
for (qmid=ql; qmid<=qr; ++qmid) {if(q[qmid].t>mid) {qmid--;break;}} if(qmid>qr) qmid--;
cdq(l,mid,pl,pmid,ql,qmid);cdq(mid+1,r,pmid+1,pr,qmid+1,qr);
for (i = qmid+1,j = pl; i <= qr; i++) {
while(j<=pmid&&p[j].x<=q[i].x) tree.add(p[j].y,p[j].k),++j;
if(q[i].id>0) ans[q[i].id]+=tree.ask(q[i].y);
else ans[-q[i].id]-=tree.ask(q[i].y);
}
for (int i = pl; i < j; i++) tree.add(p[i].y,-p[i].k);
inplace_merge(p+pl,p+pmid+1,p+pr+1,[](const modi&x,const modi&y) {return x.x<y.x;});
inplace_merge(q+ql,q+qmid+1,q+qr+1,[](const query&x,const query&y){return x.x<y.x;});
}
struct qwq{
int opt,l,r,x;
}QwQ[maxm];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i],s[i]={a[i],i};
for (int i = 1; i <= m; i++) {
cin >> opt;
if(opt==1) {
cin >> l >> r >> x;QwQ[i]={1,l,r,x};
} else {
cin >> l >> r;
QwQ[i]={2,l,r,0};
}
}
for (int i = 1; i <= m; i++) s[n+i]={QwQ[i].x,n+i};
sort(s+1,s+n+m+1);
for (int i = 1,j = 0; i <= n+m; i++) {if(s[i].f>s[i-1].f) ++j;if(s[i].s<=n) a[s[i].s]=j;else QwQ[s[i].s-n].x=j;}
__flag=true;
for (int i = 1; i <= n; i++) ins({i,i,a[i]});
int _l=0;
for (int i = 1; i <= m; i++) {
opt=QwQ[i].opt;l=QwQ[i].l;r=QwQ[i].r;x=QwQ[i].x;
if(QwQ[i].opt==1) {
assign(l,r,x);
} else {
split(l);split(r+1);
++tmp;
check(_l+1,pid);
++id;q[++qid]={tmp,r,l-1,id};q[++qid]={tmp,l-1,l-1,-id};
++tmp;
_l=pid;
}
}
pid=_l;
;
cdq();
for (int i = 1; i <= id; i++) cout << ans[i] << '\n';
return 0;
}
总结:这道题充分展示了在非随机数据下 Chtholly Tree 最大的用处,即总基本段数的个数是线性的。大多数题也大概如此。
后话:有一说一,这道题在 Ynoi 中算比较良心了(比 P5073 好多了)。