CDQ分治

· · 算法·理论

CDQ 分治

能解决什么问题

CDQ 分治是由陈丹琦整理并推广的一类基于分治思想的离线算法,它主要用于解决以下几类问题:

  1. 多维偏序问题:主要是三维偏序。
  2. 动态规划优化:优化满足特定条件转移的 DP。
  3. 数据结构代替:在可离线的前提下,代替一些复杂的数据结构(比如树套树)。
  4. 时间轴分治:处理带修改的查询问题。

算法思想

CDQ 分治的本质是“排序 + 分治 + 数据结构”的结合:

  1. 排序降维:先对第一关键字排序,消去第一维的限制。
  2. 分治划分:将当前区间分成左右两半,递归处理左右子区间。
  3. 跨区间贡献:处理左半区间对右半区间的贡献。
  4. 数据结构维护:在处理跨区间贡献时,用树状数组等维护信息。

算法流程

对于一个区间 [l, r],CDQ 分治做三件事:

  1. 递归求解左子区间 [l, mid] 内部的所有贡献。
  2. 递归求解左子区间 [mid + 1, r] 内部的所有贡献。
  3. 计算左半区间对右半区间的贡献。

时间复杂度:

计算贡献的时间为 \mathcal{O}(n \log n)(排序 + 树状数组),分治层数为 \mathcal{O}(\log n),总时间复杂度为:

T(n) = 2 \cdot T\left(\frac n 2\right) + \mathcal{O}(n \log n) = \mathcal{O}(n \log^2 n)

三维偏序问题

首先按第一维排序,CDQ 分治处理第二维和第三维。定义函数 \mathrm{cdq}(l, r) 处理区间 [l, r] 内的所有偏序关系,流程如下:

  1. 如果 l = r,直接返回。

  2. 计算中点 mid = \dfrac{l + r}{2},调用 \mathrm{cdq}(l, mid), \mathrm{cdq}(mid + 1, r)。

  3. 计算左半区间对右半区间的贡献:

    1. 将左半区间 [l, mid] 按 b 从小到大排序。
    2. 将右半区间 [mid + 1, r] 按 b 从小到大排序。
    3. 使用双指针 i (遍历左半区间)和 j (遍历右半区间)。
    4. 对于每个右半区间的元素 j,将所有左半区间中 b_i \le b_j 的元素的 c 值加入树状数组。
    5. 查询树状数组中 \le c_j 的元素个数,累加到答案中。

如果存在完全相同的三元组,需要特殊处理:

例题

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

:::success[代码]

#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] 动态逆序对

将时间视为一个维度,问题转化为求 i < j, a_i > a_j, t_i < t_j 的数量。初始的点可以视为 t_i = 0,加逆序对,权值为 1。修改视为减逆序对,权值为 -1。

因为左右都可以贡献逆序对,所以反着再跑一边 cdq 即可。

:::success[代码]

#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摆棋子

将平面分为四个象限,第三象限的点是很好求的,考虑将所有点转化为第三象限的。将时间视为第一维,这样求最小距离转化为求最大的 x_i + y_i,且 x_i \le x, y_i \le y, t_i \le t,用树状数组维护前缀最大值即可。

每次将数组反转一次,调用一次 cdq 分治,求出所有询问的最小值。

:::success[代码]

#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] 序列

设 f_i 表示以 i 结尾的最长子序列,mx_i 和 mn_i 表示 a_i 可以变为的最大数和最小数。每次最多一次变化,所以有:

f_i = \max_{j = 1}^{i - 1} (f_j + 1), mx_j \le a_i, a_j \le mn_i

因此转化为了一个三维偏序问题,考虑用 CDQ 分治优化 DP。因为 DP 需要按顺序转移,所以其中递归的顺序要这样:先处理左半区间,再处理跨区间的点,然后是右半区间。

:::success[代码]

#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

处理跨区间的点对 (i, j) 时,考虑将问题转化为:

a_i \times a_j \le \max\left(\max_{k = i}^{mid} a_k, \max_{k = mid + 1}^j a_k\right)

从 mid 开始求出后缀最大值 l_i 和前缀最大值 r_j,则

a_i \times a_j \le \max(l_i, r_j)

可以发现 l_i 和 r_j 天然就有单调性。一个区间的最大值要么是 l_i 要么是 r_j,有两种情况:

将数组离散化一下用树状数组维护即可。

:::success[代码]

#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

先将所有点按 x 排序,CDQ 分治时找到两个点 i, j 表示矩形的两个端点,处理 i 在左半区间,j 在右半区间的情况。要使 i, j 构成的矩形中不能有任何其他的点,有两种情况:

:::success[代码]

#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 差不多,按 l 排序后,有

f_i = \max_{j = 1}^{i - 1} (f_j + 1), a_j \le s_i, w_j \le a_i

用 CDQ 分治优化 DP 即可。

:::success[代码]

#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

要是所有 a 在 b 后面,可以将 a 和 b 交换,这样 T 也就没用了,转化为了一个三维偏序模板,\mathcal{O}(n \log^2 n) 卡一卡就过了。

:::success[代码]

#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;
}

:::

我们显然不会止步于此,考虑二项式反演。设 f(k) 表示恰好有 k 个维度满足偏序关系的数量,g(k) 表示固定 k 个维度满足,其他随意的数量。有

f(k) = \sum_{i = k}^3 (-1) ^ {i - k} \binom i k g(i)

答案就是 f(3)。当你带入原式后就会发现一个神奇的结论:f(3) = g(3)。

稍微观察一下可以发现 f(3) = f(0),所以有

f(0) &= \sum_{i = 0}^3 (-1) ^ i g(i) \\ &= g(0) - g(1) + g(2) - g(3) \\ &= n(n - 1) - \frac{3n(n - 1)}{2} + g(2) - f(3) \\ &= -\frac{n(n - 1)}{2} + g(2) - f(0) \end{align*}

解得 f(0) = \dfrac{g(2) - \frac{n(n - 1)}{2}}{2},g(2) 用三遍二维偏序求出即可。

:::success[代码]

#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;
}

:::

模拟赛总结

P3120 [USACO15FEB] Cow Hopscotch G

可以 DP,有

f_{i, j} = \sum_{x, y} f_{x, y}, x < i, y < j, a_{i, j} \ne a_{x, y}

显然是一个三维偏序,可以用 CDQ 分治优化 DP。

:::success[代码]

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 755, M = 1e9 + 7;
LL n, m, k, sum[N * N];
struct V {
  LL x, y, c, f;
} a[N * N];
void cdq(int l, int r) {
  if (l >= r) return;
  sort(a + l, a + r + 1, [](V x, V y) { return x.x < y.x; });
  if (a[l].x == a[r].x) return;
  int mid = (a[l].x + a[r].x) / 2 * m;
  cdq(l, mid);
  auto C = [](V x, V y) { return x.y < y.y; };
  sort(a + l, a + mid + 1, C);
  sort(a + mid + 1, a + r + 1, C);
  LL i = l, tot = 0;
  for (int j = mid + 1; j <= r; j++) {
    for (; i <= mid && a[i].y < a[j].y; i++) {
      sum[a[i].c] = (sum[a[i].c] + a[i].f) % M, tot = (tot + a[i].f) % M;
    }
    a[j].f = (a[j].f + tot - sum[a[j].c] + M) % M;
  }
  for (int j = l; j < i; j++) sum[a[j].c] = 0;
  cdq(mid + 1, r);
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m >> k, k = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1, x; j <= m; j++) {
      cin >> x, a[++k] = {i, j, x, i == 1 && j == 1};
    }
  }
  cdq(1, k);
  for (int i = 1; i <= k; i++) {
    a[i].x == n && a[i].y == m && (cout << a[i].f);
  }
  return 0;
}

:::

P2487 [SDOI2011] 拦截导弹

第一问的答案显然就是最长不上升子序列长度,设 f_{i, 0} 表示以 i 结尾的最长上升子序列长度,c_{i, 0} 表示其方案数;f_{i, 1} 表示以 i 开头的最长上升子序列长度,c_{i, 0} 同理。

每个点被拦截的概率为其出现次数除以总方案数,判断一个点是否在最长上升子序列中,显然有 f_{i, 0} + f_{i, 1} - 1 = ans,其出现次数由乘法原理可得 c_{i, 0} \cdot c_{i, 1}。

DP 的维护用 CDQ 分治优化一下即可。

:::success[代码]

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using Pii = pair<LL, double>;
const int N = 1e5 + 5;
LL n, m, tmp[N];
struct V {
  LL x, y, p; Pii dp;
} a[N], b[N];
Pii operator*(Pii x, Pii y) {
  return x.first != y.first ? (x.first > y.first ? x : y) : Pii{x.first, x.second + y.second};
}
struct BIT {
  Pii t[N];
  void U(int x, Pii v) {
    for (; x; x -= x & -x) t[x] = v.first ? t[x] * v : v; 
  }
  Pii Q(int x) {
    Pii s = {0, 0};
    for (; x <= m; x += x & -x) s = s * t[x];
    return s;
  }
} tr;
bool cmp(V x, V y) { return x.p < y.p; }
void cdq(int l, int r, V a[]) {
  if (l == r) return;
  int mid = l + r >> 1;
  cdq(l, mid, a);
  sort(a + l, a + mid + 1, [](V x, V y) { return x.x > y.x; });
  sort(a + mid + 1, a + r + 1, [](V x, V y) { return x.x > y.x; });
  int i = l;
  for (int j = mid + 1; j <= r; j++) {
    for (; i <= mid && a[i].x >= a[j].x; i++) tr.U(a[i].y, a[i].dp);
    Pii res = tr.Q(a[j].y);
    a[j].dp = a[j].dp * Pii{res.first + 1, res.second};
  }
  for (int j = l; j < i; j++) tr.U(a[j].y, {0, 0});
  sort(a + l, a + r + 1, cmp);
  cdq(mid + 1, r, a);
}
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, i, {1, 1}}, tmp[i] = y;
  }
  sort(tmp + 1, tmp + n + 1);
  m = unique(tmp + 1, tmp + n + 1) - tmp - 1;
  for (int i = 1; i <= n; i++) {
    a[i].y = lower_bound(tmp + 1, tmp + m + 1, a[i].y) - tmp;
  }
  cdq(1, n, a);
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    b[i] = {-a[n - i + 1].x, m - a[n - i + 1].y + 1, i, {1, 1}};
  }
  cdq(1, n, b);
  sort(b + 1, b + n + 1, cmp);
  Pii ans = {0, 0};
  for (int i = 1; i <= n; i++) ans = ans * a[i].dp; 
  cout << ans.first << "\n";
  for (int i = 1; i <= n; i++) {
    auto [l1, c1] = a[i].dp;
    auto [l2, c2] = b[n - i + 1].dp;
    cout << fixed << setprecision(5) << (l1 + l2 - 1 == ans.first ? c1 * c2 * 1.0 / ans.second : 0) << " ";
  }
  return 0;
}

:::

写的过于屎了。