跳到正文

算法·理论

CDQ分治

LovevoL约 15 分钟
原文

这是 2026年7月7日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年6月18日
收录
2026年7月7日
更新
2026年11月4日
篇幅
约 15 分钟

版本

  1. v2当前
  2. v1

前言

其实很早就会了,现在才想起来补

讲解

P3810 【模板】三维偏序 / 陌上花开

我们联想一下二位偏序问题,对元素以 aa 为第一关键字,以 bb 为第二关键字从小到大排序,然后有两种做法,一种是用权值树状数组,一种是在归并排序的过程中统计答案,也就是熟知的逆序对问题

现在元素数量变成了 33 个,如何解决呢?

没错,归并排序套树状数组,我们称之为 CDQ 分治

直接考虑具体实现

首先模仿二维偏序,先以 aa 为第一关键字,以 bb 为第二关键字,以 cc 为第三关键字排序,这一步的目的是保证对于 ii 元素,满足 aj≤ai,bj≤bi,cj≤cia_j\le a_i,b_j\le b_i,c_j\le c_i 的 jj 一定 <i< i

然后对序列分治,考虑归并的过程,在归并 [l,mid],[mid+1,r][l,mid],[mid+1,r] 两个区间时,计算 [l,mid][l,mid] 区间的数对 [mid+1,r][mid+1,r] 的数产生的贡献,具体地,归并时按 bib_i 为关键字从小到大排序,当 [l,mid][l,mid] 的数归并后,将 cic_i 加入权值树状数组;当 [mid+1,r][mid+1,r] 的数归并后,查询树状数组中小于 cic_i 的个数

实现如下:

C++
#include<bits/stdc++.h>
#define int long long 
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);	
#define inf 1e14
using namespace std;
const int maxn=1e6+10;

int n,k,tot=0,ans[maxn],f[maxn];

struct node{int a,b,c,cnt,idx;}q[maxn],a[maxn];

bool judge(node i,node j){return (i.a==j.a)&&(i.b==j.b)&&(i.c==j.c);}

bool cmp(node i,node j){
	if(i.a!=j.a)return i.a<j.a;
	else if(i.b!=j.b)return i.b<j.b;
	else return i.c<j.c;
}

node tmp[maxn];

struct Tree{
	int tr[maxn];
	int lowbit(int x){return x&-x;}
	void add(int u,int x){for(int i=u;i<=k;i+=lowbit(i))tr[i]+=x;}
	int sum(int u){int res=0; for(;u;u-=lowbit(u))res+=tr[u]; return res;}
}T;

void CDQ(int L,int R){
	if(L==R)return;
	int mid=(L+R)>>1;
	CDQ(L,mid); CDQ(mid+1,R);
	for(int i=L;i<=R;i++)tmp[i]=q[i];
	int L1=L,L2=mid+1,pos=L;
	while(L1<=mid&&L2<=R){
		if(tmp[L1].b<=tmp[L2].b)q[pos]=tmp[L1],T.add(tmp[L1].c,tmp[L1].cnt),L1++,pos++;
		else q[pos]=tmp[L2],ans[q[pos].idx]+=T.sum(q[pos].c),L2++,pos++;
	}
	while(L1<=mid)T.add(tmp[L1].c,tmp[L1].cnt),q[pos++]=tmp[L1++];
	while(L2<=R)ans[tmp[L2].idx]+=T.sum(tmp[L2].c),q[pos++]=tmp[L2++];
	for(int i=L;i<=mid;i++)T.add(tmp[i].c,-tmp[i].cnt);
}

signed main(){
	YUANSHEN QIDONG
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>a[i].a>>a[i].b>>a[i].c;
	sort(a+1,a+1+n,cmp); 
	for(int i=1;i<=n;i++)if(judge(a[i],a[i-1]))q[tot].cnt++; else q[++tot]={a[i].a,a[i].b,a[i].c,1,i};
	CDQ(1,tot);
	for(int i=1;i<=tot;i++)f[ans[q[i].idx]+q[i].cnt-1]+=q[i].cnt;
	for(int i=0;i<n;i++)cout<<f[i]<<endl;
	return 0;
} 

运用

CDQ 分治本质上是另一种分治思想,它计算右区间到左区间的贡献,而不是想朴素分治那样计算跨中线的贡献

三维偏序

CDQ 分治最常用于解决的问题是三维偏序问题,因此很多有关 CDQ 的题目都需要转化为三维偏序问题


P3157 [CQOI2011] 动态逆序对

典中典

对于逆序对 (i,j)(i,j),它存在当且仅当当前 i,ji,j 均未被删除

我们考虑维护一维时间轴 tit_i

可以考虑每个元素被删除后,逆序对减少的个数,对于数 ii,我们求出其左侧满足 aj>aia_j>a_i 且 tj<tit_j<t_i 的 jj 的个数

但是这样只能统计到逆序对右边被先删的情况,我们把序列倒过来再做一遍 CDQ 即可

讲得有些肤浅,还请见代码理解

C++
#include<bits/stdc++.h>
#define int long long 
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);	
#define inf 1e14
#define mod 998244353
using namespace std;
const int maxn=1e6+10;

int n,m,ans[maxn],pos[maxn],tot;

struct node{int a,t;}q[maxn],tmp[maxn],qq[maxn];

struct BIT{
	int tr[maxn];
	int lowbit(int x){return x&-x;}
	void add(int u,int x){for(;u<=n;u+=lowbit(u))tr[u]+=x;}
	int sum(int u){int res=0; for(;u;u-=lowbit(u))res+=tr[u]; return res;}
}T;

void CDQ(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	CDQ(l,mid); CDQ(mid+1,r);
	for(int i=l;i<=r;i++)tmp[i]=q[i];
	int L1=l,L2=mid+1,pos=l;
	while(L1<=mid&&L2<=r){
		if(tmp[L1].a>=tmp[L2].a)T.add(tmp[L1].t,1),q[pos]=tmp[L1],pos++,L1++;
		else ans[tmp[L2].t]+=T.sum(m)-T.sum(tmp[L2].t-1),q[pos]=tmp[L2],pos++,L2++;
	}
	while(L1<=mid)T.add(tmp[L1].t,1),q[pos]=tmp[L1],pos++,L1++;
	while(L2<=r)ans[tmp[L2].t]+=T.sum(m)-T.sum(tmp[L2].t-1),q[pos]=tmp[L2],pos++,L2++;
	for(int i=l;i<=mid;i++)T.add(tmp[i].t,-1);
}

signed main(){
	YUANSHEN QIDONG
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>q[i].a,pos[q[i].a]=i,q[i].t=m;
	for(int i=1,x;i<=m;i++)cin>>x,q[pos[x]].t=i;
	for(int i=1;i<=n;i++)qq[i]=q[i];
	CDQ(1,n);
	for(int i=1;i<=n;i++)q[i]={n-qq[n-i+1].a,qq[n-i+1].t};
	CDQ(1,n);
	for(int i=1;i<=n;i++)tot+=((i-1)-T.sum(qq[i].a-1)),T.add(qq[i].a,1);
	for(int i=1;i<=m;i++)cout<<tot-ans[i-1]<<endl,ans[i]+=ans[i-1];
	return 0;
} 

P4390 [BalkanOI 2007] Mokia 摩基亚

明显的三维偏序

对于 tit_i 时刻新增的 cic_i 人 (xi,yi)(x_i,y_i) 将其拆成 xi,yi,tix_i,y_i,t_i 三维维护

询问矩形 (0,0),(xi,yi)(0,0),(x_i,y_i) 在 tit_i 时刻包含的点的数量,就是询问满足 xj≤xi,yj≤yi,tj≤tix_j\le x_i,y_j\le y_i,t_j\le t_i 的数量,于是 CDQ 即可

对于答案的查询,类似二维前缀和拆掉式子即可

C++
#include<bits/stdc++.h>
#define int long long 
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);	
#define inf 1e14
#define mod 998244353
using namespace std;
const int maxn=1e6+10;

int w,op,a,tot,tim,qtot,ans[maxn];

struct node{int x,y,c,t,idx;}q[maxn],qus[maxn],tmp[maxn];

bool cmp(node i,node j){
	if(i.x!=j.x)return i.x<j.x;
	if(i.y!=j.y)return i.y<j.y;
	if(i.t!=j.t)return i.t<j.t;
	else return (i.c==0);
}

struct BIT{
	int tr[maxn];
	int lowbit(int x){return x&-x;}
	void add(int u,int x){for(;u<=tim;u+=lowbit(u))tr[u]+=x;}
	int sum(int u){int res=0; for(;u;u-=lowbit(u))res+=tr[u]; return res;}
}T;

void CDQ(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	CDQ(l,mid); CDQ(mid+1,r);
	for(int i=l;i<=r;i++)tmp[i]=q[i];
	int L1=l,L2=mid+1,pos=l;
	while(L1<=mid&&L2<=r){
		if(tmp[L1].y<=tmp[L2].y)q[pos]=tmp[L1],T.add(tmp[L1].t,tmp[L1].c),pos++,L1++;
		else q[pos]=tmp[L2],ans[tmp[L2].idx]+=T.sum(tmp[L2].t),pos++,L2++;
	}
	while(L1<=mid)q[pos]=tmp[L1],T.add(tmp[L1].t,tmp[L1].c),pos++,L1++;
	while(L2<=r)q[pos]=tmp[L2],ans[tmp[L2].idx]+=T.sum(tmp[L2].t),pos++,L2++;
	for(int i=l;i<=mid;i++)T.add(tmp[i].t,-tmp[i].c);
}

signed main(){
	YUANSHEN QIDONG
	cin>>op;
	while(op!=3){
		tim++; int x,y,xx,yy;
		if(op==0)cin>>w;
		if(op==1)cin>>x>>y>>a,q[++tot]={x,y,a,tim,tot};
		if(op==2){
			cin>>x>>y>>xx>>yy; qtot++;
			q[++tot]={xx,yy,0,tim,tot},qus[qtot].t=tot;
			q[++tot]={x-1,y-1,0,tim,tot},qus[qtot].c=tot;
			q[++tot]={xx,y-1,0,tim,tot},qus[qtot].x=tot;
			q[++tot]={x-1,yy,0,tim,tot},qus[qtot].y=tot;
		}cin>>op;
	}
	sort(q+1,q+1+tot,cmp);
	CDQ(1,tot);
	for(int i=1;i<=qtot;i++)cout<<ans[qus[i].t]+ans[qus[i].c]-ans[qus[i].x]-ans[qus[i].y]<<endl;
	return 0;
} 

P3755 [CQOI2017] 老 C 的任务

和上一题几乎一样,唯一区别是去掉了时间轴

二维数点问题

C++
#include<bits/stdc++.h>
#define int long long 
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);	
#define inf 1e14
using namespace std;
const int maxn=1e6+10;

int n,m,tot,ans[maxn];

struct node{
	int cnt,x,y,idx;
	friend bool operator <(node i,node j){
		if(i.x!=j.x)return i.x<j.x;
		if(i.y!=j.y)return i.y<j.y;
		return i.cnt>j.cnt;
	}
}q[maxn],qus[maxn],tmp[maxn];

void CDQ(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	CDQ(l,mid); CDQ(mid+1,r);
	for(int i=l;i<=r;i++)tmp[i]=q[i];
	int L1=l,L2=mid+1,pos=l,sum=0;
	while(L1<=mid&&L2<=r){
		if(tmp[L1].y<=tmp[L2].y)q[pos]=tmp[L1],sum+=q[pos].cnt,pos++,L1++;
		else q[pos]=tmp[L2],ans[q[pos].idx]+=sum,pos++,L2++;
	}
	while(L1<=mid)q[pos]=tmp[L1],sum+=q[pos].cnt,pos++,L1++;
	while(L2<=r)q[pos]=tmp[L2],ans[q[pos].idx]+=sum,pos++,L2++;
}

signed main(){
	YUANSHEN QIDONG
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>q[i].x>>q[i].y>>q[i].cnt,q[i].idx=++tot;
	for(int i=1,x,xx,y,yy;i<=m;i++){
		cin>>x>>y>>xx>>yy;
		q[++tot]={0,xx,yy,tot},qus[i].cnt=tot;
		q[++tot]={0,x-1,yy,tot},qus[i].x=tot;
		q[++tot]={0,xx,y-1,tot},qus[i].y=tot;
		q[++tot]={0,x-1,y-1,tot},qus[i].idx=tot;
	}
	sort(q+1,q+1+tot);
	CDQ(1,tot);
	for(int i=1;i<=m;i++)cout<<ans[qus[i].cnt]+ans[qus[i].idx]-ans[qus[i].x]-ans[qus[i].y]<<endl;
	return 0;
}

CDQ 优化 DP

当 dpdp 的转移有顺序限制,也就是后面只能由前面转移过来的时候,可以用 CDQ 分治批量转移

P2487 [SDOI2011] 拦截导弹