跳到正文

算法·理论

CDQ divide-and-conquer

zhoujiefu约 11 分钟
原文

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

文章信息与版本

文章信息

发布
2026年5月6日
收录
2026年5月6日
更新
2026年11月11日
篇幅
约 11 分钟

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1

Previous problem Introduction

You are given a sequence (𝑎1,𝑎2,…,𝑎𝑛)(𝑎_1,𝑎_2,\dots,𝑎_𝑛). For each 1≤i≤n1\le i\le n, you have to find the number of j<ij<i that satisfied aj<aia_j<a_i. n≤105n\le 10^5.

This problem is a classic problem, and I believe Mr.Jia has told you the solution.

It’s clear that we have two different solutions:

  1. We can use the Fenwick Tree to solve it.
  2. We can use Divide and Conquer, which is the algorithm for merge sort.
The code for algorithm 1
C++
struct BIT{
    int c[N];
    inline int lb(int x){return x&-x;}
    inline void add(int x,int k){for(;x<=m;x+=lb(x))c[x]+=k;}
    inline int ask(int x){int ans=0;for(;x;x-=lb(x))ans+=c[x];return ans;}
}bit;
signed main(){
    read(n);
    rep(i,1,n) read(a[i]);
    rep(i,1,n){
        write(bit.ask(a[i])),pc(' ');
        bit.add(a[i],1);
    }
}
The code for algorithm 2
C++
inline bool cmp(int x,int y){return a[x]<a[y];}
void solve(int l,int r){
    if(l==r) return;
    int mid=(l+r)>>1;
    solve(l,mid),solve(mid+1,r);
    int j=l,cnt=0;
    rep(i,mid+1,r){
        while(j<=mid&&a[id[j]]<a[id[i]]) ++cnt,++j;
        ans[id[i]]+=cnt;
    }
    inplace_merge(id+l,id+mid+1,id+r+1,cmp);
}

Both two algorithms are important in today’s problem.

The Real Problem

Here, you are given two sequences (x1,x2,…,xn)(x_1,x_2,\dots,x_n) and (y1,y2,…,yn)(y_1,y_2,\dots,y_n). In this problem, you have to find the number of j<ij<i that satisfied xj<xix_j<x_i and yj<yiy_j<y_i. The time complexity should be O(nlog⁡2n)O(n\log^2 n).

What’s the problem? It’s obvious that the old ways fail. One solution is to use complex data structures like trees on trees, but there are many drawbacks, for instance, the high space complexity up to O(nlog⁡2n)O(n\log^2 n). Is there any way to decrease the space complexity and make the code concise?

This is a 3-d problem. As we know, Divide and Conquer can make a 3-d problem into 2-d problem. So we Just need to solve a 2-d problem in each floor of the Divide Conquer.

In detail, we divide all the contribution into three parts: left to left, right to right, and left to right. We first solve both the left and right. So we only need to resolve the contribution from the left half to the right half. So in every floor of the divide and conquer, we use the fenwick tree to maintain the information of the left half, and use these infomation of the left half to answer the right half of the sequence.

If the time complexity solving of a sequence which is of lenth nn is T(n)T(n), then T(n)=2T(n2)+O(nlog⁡n)T(n)=2T(\frac{n}{2})+O(n\log n), we can analyse it with the master theory that it’s O(nlog⁡2n)O(n\log^2 n).

The Code
C++
inline bool cmp(node a,node b){
    if(a.a!=b.a) return a.a<b.a; //j<i
    if(a.b!=b.b) return a.b<b.b; //x[j]<x[i]
    return a.c<b.c; //y[j]<y[i]
}
inline bool cmp1(node a,node b){
    return a.b<b.b;
}
void cdq(int l,int r){
    if(l==r) return;
    int mid=(l+r)>>1;
    cdq(l,mid),cdq(mid+1,r); //left and right
    int i=l,j=mid+1; //left to right
    for(;j<=r;++j){
        while(i<=mid&&a[i].b<=a[j].b) bit.add(a[i].c,a[i].cnt),++i;
        a[j].ans+=bit.ask(a[j].c);
    }
    rep(j,l,i-1) bit.add(a[j].c,-a[j].cnt); //to clear the fenwick
    inplace_merge(a+l,a+mid+1,a+r+1,cmp1);
}

Sample problem

T1

Dynamic inversions

For a sequence aa, the number of inversions is defined as the number of elements in the set

{(i,j)∣i<j∧ai>aj}.\{(i,j) \mid i < j \wedge a_i > a_j \}.

Now, given a permutation of 1∼n1 \sim n, we delete mm elements one by one in a certain order. Your task is, before each deletion, to count the number of inversions in the entire sequence.

solution

Consider what could change the answer. For a time point tt, only ti>tt_i>t could contribute to the answer. At the time point, there are two types of contribution:

  1. pi>pjp_i>p_j and vali<valjval_i<val_j
  2. pi<pjp_i<p_j and vali>valjval_i>val_j

We each calculate them, then take the prefix sum of the answer array.

AC Code
C++
struct BIT{
    int c[N];
    inline void clear(int n){rep(i,1,n)c[i]=0;}
    inline int lowbit(int x){return x&-x;}
    inline void add(int x,int k){for(;x<=n;x+=lowbit(x))c[x]+=k;}
    inline int ask(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}
    inline int query(int l,int r){return ask(r)-ask(l-1);}
}bit;
inline bool cmp(node a,node b){
    return a.p<b.p;
}
void cdq(int l,int r){
    if(l==r) return;
    int mid=(l+r)>>1;
    cdq(l,mid),cdq(mid+1,r);
    sort(a+l,a+mid+1,cmp),sort(a+mid+1,a+r+1,cmp);
    int i=l,j=mid+1;
    while(j<=r){
        while(i<=mid&&a[i].p<=a[j].p) bit.add(a[i].x,a[i].k),++i;
        ans[a[j].id]+=a[j].k*bit.query(a[j].x+1,n);
        ++j;
    }
    rep(j,l,i-1) bit.add(a[j].x,-a[j].k);
    i=mid,j=r;
    while(j>mid){
        while(i>=l&&a[i].p>=a[j].p) bit.add(a[i].x,a[i].k),--i;
        ans[a[j].id]+=a[j].k*bit.query(1,a[j].x-1);
        --j;
    }
    rep(j,i+1,mid) bit.add(a[j].x,-a[j].k);
}
signed main(){
    read(n),read(m);
    rep(i,1,n){
        int x;read(x);pos[x]=i;
        a[++tot]={x,i,1,0};
    }
    rep(i,1,m){
        int x;read(x);
        a[++tot]={x,pos[x],-1,i};
    }
    cdq(1,tot);
    rep(i,1,m) ans[i]+=ans[i-1];
    rep(i,0,m-1) write(ans[i]),pc('\n');
    return 0;
}