CDQ 分治
能解决什么问题
CDQ 分治是由陈丹琦整理并推广的一类基于分治思想的离线算法,它主要用于解决以下几类问题:
- 多维偏序问题:主要是三维偏序。
- 动态规划优化:优化满足特定条件转移的 DP。
- 数据结构代替:在可离线的前提下,代替一些复杂的数据结构(比如树套树)。
- 时间轴分治:处理带修改的查询问题。
算法思想
CDQ 分治的本质是“排序 + 分治 + 数据结构”的结合:
- 排序降维:先对第一关键字排序,消去第一维的限制。
- 分治划分:将当前区间分成左右两半,递归处理左右子区间。
- 跨区间贡献:处理左半区间对右半区间的贡献。
- 数据结构维护:在处理跨区间贡献时,用树状数组等维护信息。
算法流程
对于一个区间 ,CDQ 分治做三件事:
- 递归求解左子区间 内部的所有贡献。
- 递归求解左子区间 内部的所有贡献。
- 计算左半区间对右半区间的贡献。
时间复杂度:
计算贡献的时间为 (排序 + 树状数组),分治层数为 ,总时间复杂度为:
三维偏序问题
首先按第一维排序,CDQ 分治处理第二维和第三维。定义函数 处理区间 内的所有偏序关系,流程如下:
-
如果 ,直接返回。
-
计算中点 ,调用 。
-
计算左半区间对右半区间的贡献:
- 将左半区间 按 从小到大排序。
- 将右半区间 按 从小到大排序。
- 使用双指针 (遍历左半区间)和 (遍历右半区间)。
- 对于每个右半区间的元素 ,将所有左半区间中 的元素的 值加入树状数组。
- 查询树状数组中 的元素个数,累加到答案中。
如果存在完全相同的三元组,需要特殊处理:
- 排序后将完全相同的元素合并,记录每个种类出现的次数。
- 计算贡献时,相同元素之间也要互相计入。
- 最终答案要加上自身重复的部分。
例题
P3810 【模板】三维偏序 / 陌上花开
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 2e5 + 5;
LL n, m, k, ans[N];
struct V {
LL a, b, c, w, cnt;
} a[N];
struct BIT {
LL t[N];
void U(int x, LL v) {
for (; x <= k; x += x & -x) t[x] += v;
}
LL Q(int x) {
LL s = 0;
for (; x; x -= x & -x) s += t[x];
return s;
}
} tr;
bool cmp(V x, V y) {
return x.b != y.b ? x.b < y.b : x.c < y.c;
}
void cdq(int l, int r) {
if (l == r) return;
int o = l + r >> 1;
cdq(l, o), cdq(o + 1, r);
int i = l;
for (int j = o + 1; j <= r; j++) {
for (; i <= o && a[i].b <= a[j].b; i++) tr.U(a[i].c, a[i].cnt);
a[j].w += tr.Q(a[j].c);
}
for (int j = l; j < i; j++) tr.U(a[j].c, -a[j].cnt);
sort(a + l, a + r + 1, cmp);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i].a >> a[i].b >> a[i].c;
}
sort(a + 1, a + n + 1, [&](V x, V y) {
return x.a != y.a ? x.a < y.a : cmp(x, y);
});
for (int i = 1; i <= n; i++) {
(a[i].a != a[i - 1].a || a[i].b != a[i - 1].b || a[i].c != a[i - 1].c) && (a[++m] = a[i], 1), a[m].cnt++;
}
cdq(1, m);
for (int i = 1; i <= m; i++) {
ans[a[i].w + a[i].cnt - 1] += a[i].cnt;
}
for (int i = 0; i < n; i++) cout << ans[i] << "\n";
return 0;
}P3157 [CQOI2011] 动态逆序对
将时间视为一个维度,问题转化为求 的数量。初始的点可以视为 ,加逆序对,权值为 。修改视为减逆序对,权值为 。
因为左右都可以贡献逆序对,所以反着再跑一边 cdq 即可。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 2e5 + 5;
LL n, m, b[N], p[N], ans[N];
struct V {
LL a, b, c, v;
} a[N];
struct BIT {
LL t[N];
void U(int x, LL v) {
for (; x <= n; x += x & -x) t[x] += v;
}
LL Q(int x) {
LL s = 0;
for (; x; x -= x & -x) s += t[x];
return s;
}
} tr;
bool cmp(V x, V y) {
return x.b != y.b ? x.b < y.b : x.c < y.c;
}
void cdq(int l, int r) {
if (l == r) return;
int o = l + r >> 1;
cdq(l, o), cdq(o + 1, r);
int i = l;
for (int j = o + 1; j <= r; j++) {
for (; i <= o && a[i].b < a[j].b; i++) tr.U(a[i].c, a[i].v);
ans[a[j].a] += a[j].v * (tr.Q(n) - tr.Q(a[j].c));
}
for (int j = l; j < i; j++) tr.U(a[j].c, -a[j].v);
i = o;
for (int j = r; j > o; j--) {
for (; i >= l && a[i].b > a[j].b; i--) tr.U(a[i].c, a[i].v);
ans[a[j].a] += a[j].v * tr.Q(a[j].c - 1);
}
for (int j = o; j > i; j--) tr.U(a[j].c, -a[j].v);
sort(a + l, a + r + 1, cmp);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1, x; i <= n; i++) {
cin >> x, p[x] = i, a[i] = {0, i, x, 1};
}
for (int i = 1, x; i <= m; i++) {
cin >> x, a[n + i] = {i, p[x], x, -1};
}
cdq(1, n + m);
for (int i = 0; i < m; i++) {
ans[i + 1] += ans[i], cout << ans[i] << "\n";
}
return 0;
}P4169 [Violet] 天使玩偶/SJY摆棋子
将平面分为四个象限,第三象限的点是很好求的,考虑将所有点转化为第三象限的。将时间视为第一维,这样求最小距离转化为求最大的 ,且 ,用树状数组维护前缀最大值即可。
每次将数组反转一次,调用一次 cdq 分治,求出所有询问的最小值。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e6 + 5;
int n, m, k, x[N], y[N], ans[N];
struct V {
int a, b, c, v;
} a[N], q[N];
struct BIT {
int t[N];
void U(int x, int v) {
for (x++; x < N; x += x & -x) t[x] = max(t[x], v);
}
void Cl(int x) {
for (x++; x < N; x += x & -x) t[x] = -1e9;
}
int Q(int x) {
int s = -1e9;
for (x++; x; x -= x & -x) s = max(s, t[x]);
return s;
}
} tr;
bool cmp(V x, V y) {
return x.b != y.b ? x.b < y.b : x.c < y.c;
}
void cdq(int l, int r) {
if (l == r) return;
int o = l + r >> 1;
cdq(l, o), cdq(o + 1, r);
int i = l;
for (int j = o + 1; j <= r; j++) {
for (; i <= o && a[i].b <= a[j].b; i++) a[i].v != 2 && (tr.U(a[i].c, a[i].b + a[i].c), 1);
a[j].v == 2 && (ans[a[j].a] = min(ans[a[j].a], a[j].b + a[j].c - tr.Q(a[j].c)));
}
for (int j = l; j < i; j++) a[j].v != 2 && (tr.Cl(a[j].c), 1);
sort(a + l, a + r + 1, cmp);
}
void Solve() {
sort(a + 1, a + n + 1, [](V x, V y) { return x.a < y.a; }), cdq(1, n);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i].b >> a[i].c;
}
for (int i = n + 1; i <= n + m; i++) {
cin >> a[i].v >> a[i].b >> a[i].c, a[i].a = i, ans[i] = 1e9;
}
n += m, fill(tr.t, tr.t + N, -1e9), Solve();
for (int i = 1; i <= n; i++) a[i].b = 1e6 - a[i].b; Solve();
for (int i = 1; i <= n; i++) a[i].c = 1e6 - a[i].c; Solve();
for (int i = 1; i <= n; i++) a[i].b = 1e6 - a[i].b; Solve();
for (int i = n - m + 1; i <= n; i++) {
ans[i] != 1e9 && (cout << ans[i] << "\n");
}
return 0;
}P4093 [HEOI2016/TJOI2016] 序列
设 表示以 结尾的最长子序列, 和 表示 可以变为的最大数和最小数。每次最多一次变化,所以有:
因此转化为了一个三维偏序问题,考虑用 CDQ 分治优化 DP。因为 DP 需要按顺序转移,所以其中递归的顺序要这样:先处理左半区间,再处理跨区间的点,然后是右半区间。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e5 + 5;
int n, m, ans;
struct V {
int p, a, mx, mn, v;
} a[N];
struct BIT {
int t[N];
void U(int x, int v) {
for (; x <= 1e5; x += x & -x) {
t[x] = (v ? max(t[x], v) : v);
}
}
int Q(int x) {
int s = 0;
for (; x; x -= x & -x) s = max(s, t[x]);
return s;
}
} tr;
void cdq(int l, int r) {
if (l == r) return;
int o = l + r >> 1;
cdq(l, o);
sort(a + l, a + o + 1, [](V x, V y) { return x.mx < y.mx; });
sort(a + o + 1, a + r + 1, [](V x, V y) { return x.a < y.a; });
int i = l;
for (int j = o + 1; j <= r; j++) {
for (; i <= o && a[i].mx <= a[j].a; i++) tr.U(a[i].a, a[i].v);
a[j].v = max(a[j].v, tr.Q(a[j].mn) + 1);
ans = max(ans, a[j].v);
}
for (int j = l; j < i; j++) tr.U(a[j].a, 0);
sort(a + l, a + r + 1, [](V x, V y) { return x.p < y.p; });
cdq(o + 1, r);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1, x; i <= n; i++) {
cin >> x, a[i] = {i, x, x, x, 1};
}
for (int x, y; m; m--) {
cin >> x >> y;
a[x].mx = max(a[x].mx, y), a[x].mn = min(a[x].mn, y);
}
cdq(1, n), cout << ans;
return 0;
}P4755 Beautiful Pair
处理跨区间的点对 时,考虑将问题转化为:
从 开始求出后缀最大值 和前缀最大值 ,则
可以发现 和 天然就有单调性。一个区间的最大值要么是 要么是 ,有两种情况:
-
。
-
。
将数组离散化一下用树状数组维护即可。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e5 + 5;
LL n, m, a[N], b[N], c[N], x[N], y[N], ans;
struct BIT {
LL t[N];
void U(int x, LL v) {
for (; x <= m; x += x & -x) t[x] += v;
}
LL Q(int x) {
LL s = 0;
for (; x; x -= x & -x) s += t[x];
return s;
}
} tr;
void cdq(int l, int r) {
if (l == r) {
ans += (a[l] == 1);
return;
}
int o = l + r >> 1;
cdq(l, o), cdq(o + 1, r);
x[o + 1] = y[o] = 0;
for (int i = o; i >= l; i--) x[i] = max(x[i + 1], a[i]);
for (int i = o + 1; i <= r; i++) y[i] = max(y[i - 1], a[i]);
int i = o;
for (int j = o + 1; j <= r; j++) {
for (; i >= l && x[i] < y[j]; i--) tr.U(c[i], 1);
int v = y[j] / a[j];
ans += tr.Q(upper_bound(b + 1, b + m + 1, v) - b - 1);
}
for (int j = o; j > i; j--) tr.U(c[j], -1);
i = o + 1;
for (int j = o; j >= l; j--) {
for (; i <= r && y[i] <= x[j]; i++) tr.U(c[i], 1);
int v = x[j] / a[j];
ans += tr.Q(upper_bound(b + 1, b + m + 1, v) - b - 1);
}
for (int j = o + 1; j < i; j++) tr.U(c[j], -1);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];
sort(b + 1, b + n + 1);
m = unique(b + 1, b + n + 1) - b - 1;
for (int i = 1; i <= n; i++) {
c[i] = lower_bound(b + 1, b + m + 1, a[i]) - b;
}
cdq(1, n), cout << ans;
return 0;
}P14426 [JOISC 2014] 稻草人 / Scarecrows
先将所有点按 排序,CDQ 分治时找到两个点 表示矩形的两个端点,处理 在左半区间, 在右半区间的情况。要使 构成的矩形中不能有任何其他的点,有两种情况:
-
对于左半区间的非法点 ,。不存在非法点 ,转化为将 从小到大排序后 必须是后缀最大值。
维护一个单调栈, 从小到大, 从大到小,用树状数组维护合法点的数量。 -
对于右半区间的非法点 ,。不存在非法点 ,转化为在所有 的点中找到最大的 ,左半边可行的 必须满足 。
用树状数组或其他数据结构维护一下 ,统计左半区间合法点的数量即可。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 2e5 + 5;
LL n, tx[N], ty[N], k, q[N], ans;
struct V { int x, y; } a[N];
struct T {
LL o, t[N];
void U(int x, LL v) {
for (; x <= n; x += x & -x) {
!o ? t[x] += v : t[x] = (v ? max(t[x], v) : v);
}
}
LL Q(int x) {
LL s = 0;
for(; x; x -= x & -x) {
s = (!o ? s + t[x] : max(s, t[x]));
}
return s;
}
} ts, ta;
void cdq(int l, int r) {
if (l == r) return;
int o = l + r >> 1;
cdq(l, o), cdq(o + 1, r);
auto C = [](V x, V y) { return x.y < y.y; };
sort(a + l, a + o + 1, C);
sort(a + o + 1, a + r + 1, C);
int i = l, j = o + 1;
for (k = 0; j <= r; j++) {
for (; i <= o && a[i].y <= a[j].y; i++) {
for (; k && a[q[k]].x <= a[i].x; k--) ts.U(a[q[k]].y, -1);
q[++k] = i, ts.U(a[i].y, 1);
}
ans += ts.Q(a[j].y) - ts.Q(ta.Q(a[j].x));
ta.U(a[j].x, a[j].y);
}
for (; k; k--) ts.U(a[q[k]].y, -1);
for (int k = o + 1; k <= r; k++) ta.U(a[k].x, 0);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1, x, y; i <= n; i++) {
cin >> x >> y;
a[i] = {x, y}, tx[i] = x, ty[i] = y;
}
sort(tx + 1, tx + n + 1);
sort(ty + 1, ty + n + 1);
for (int i = 1; i <= n; i++) {
auto &[x, y] = a[i];
x = lower_bound(tx + 1, tx + n + 1, x) - tx;
y = lower_bound(ty + 1, ty + n + 1, y) - ty;
}
sort(a + 1, a + n + 1, [](V x, V y) { return x.x < y.x; });
ta.o = 1, cdq(1, n), cout << ans;
return 0;
}P3364 Cool loves touli
和 P4093 差不多,按 排序后,有
用 CDQ 分治优化 DP 即可。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 2e5 + 5;
int n, m, b[N], ans;
struct V {
int l, s, w, a, x, y, v;
} a[N];
struct BIT {
int t[N];
void U(int x, int v) {
for (; x <= m; x += x & -x) {
t[x] = (v ? max(t[x], v) : 0);
}
}
int Q(int x) {
int s = 0;
for (; x; x -= x & -x) s = max(s, t[x]);
return s;
}
} tr;
// l_i <= l_j, a_i <= s_j, w_i <= a_j
void cdq(int l, int r) {
if (l == r) return;
int o = l + r >> 1;
cdq(l, o);
sort(a + l, a + o + 1, [](V x, V y) { return x.a < y.a; });
sort(a + o + 1, a + r + 1, [](V x, V y) { return x.s < y.s; });
int i = l;
for (int j = o + 1; j <= r; j++) {
for (; i <= o && a[i].a <= a[j].s; i++) tr.U(a[i].x, a[i].v);
a[j].v = max(a[j].v, tr.Q(a[j].y) + 1);
ans = max(ans, a[j].v);
}
for (int j = l; j < i; j++) tr.U(a[j].x, 0);
sort(a + l, a + r + 1, [](V x, V y) { return x.l < y.l; });
cdq(o + 1, r);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1, l, s, w, x; i <= n; i++) {
cin >> l >> s >> w >> x;
a[i] = {l, s, w, x, 0, 0, 1}, b[++m] = w, b[++m] = x;
}
sort(a + 1, a + n + 1, [](V x, V y) { return x.l < y.l; });
sort(b + 1, b + m + 1);
m = unique(b + 1, b + m + 1) - b - 1;
for (int i = 1; i <= n; i++) {
a[i].x = lower_bound(b + 1, b + m + 1, a[i].w) - b;
a[i].y = lower_bound(b + 1, b + m + 1, a[i].a) - b;
}
cdq(1, n), cout << ans;
return 0;
}P11197 [COTS 2021] 赛狗游戏 Tiket
要是所有 在 后面,可以将 和 交换,这样 也就没用了,转化为了一个三维偏序模板, 卡一卡就过了。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 5e5 + 5;
LL n, ans;
struct V {
LL a, b, c;
} a[N];
struct BIT {
LL t[N];
void U(int x, LL v) {
for (; x <= n; x += x & -x) t[x] += v;
}
LL Q(int x) {
LL s = 0;
for (; x; x -= x & -x) s += t[x];
return s;
}
} tr;
void cdq(int l, int r) {
if (l == r) return;
int o = l + r >> 1;
cdq(l, o), cdq(o + 1, r);
int i = l;
for (int j = o + 1; j <= r; j++) {
for (; i <= o && a[i].b <= a[j].b; i++) tr.U(a[i].c, 1);
ans += tr.Q(a[j].c);
}
for (int j = l; j < i; j++) tr.U(a[j].c, -1);
sort(a + l, a + r + 1, [](V x, V y) { return x.b < y.b; });
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1, x; i <= n; i++) cin >> x;
for (int i = 1, x; i <= n; i++) cin >> x, a[x].a = i;
for (int i = 1, x; i <= n; i++) cin >> x, a[x].b = i;
for (int i = 1, x; i <= n; i++) cin >> x, a[x].c = i;
sort(a + 1, a + n + 1, [](V x, V y) { return x.a < y.a; });
cdq(1, n), cout << ans;
return 0;
}我们显然不会止步于此,考虑二项式反演。设 表示恰好有 个维度满足偏序关系的数量, 表示固定 个维度满足,其他随意的数量。有
答案就是 。当你带入原式后就会发现一个神奇的结论:。
稍微观察一下可以发现 ,所以有
解得 , 用三遍二维偏序求出即可。
代码
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 5e5 + 5;
LL n, ans;
array<int, 3> a[N];
struct BIT {
LL t[N];
void U(int x, LL v) {
for (; x <= n; x += x & -x) t[x] += v;
}
LL Q(int x) {
LL s = 0;
for (; x; x -= x & -x) s += t[x];
return s;
}
} tr;
LL Solve(int p, int q) {
fill(tr.t + 1, tr.t + n + 1, 0);
LL ans = 0;
sort(a + 1, a + n + 1, [&](auto x, auto y) { return x[p] < y[p]; });
for (int i = 1; i <= n; i++) {
ans += tr.Q(a[i][q]), tr.U(a[i][q], 1);
}
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1, x; i <= n; i++) cin >> x;
for (int j = 0; j < 3; j++) {
for (int i = 1, x; i <= n; i++) cin >> x, a[x][j] = i;
}
cout << (Solve(0, 1) + Solve(0, 2) + Solve(1, 2) - n * (n - 1) / 2) / 2;
return 0;
}