版本比较
CDQ分治
正文
增加 250 行删除 0 行
381 行未改动全部展开382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635381 行未改动全部展开 return 0;}```:::
### P14426 [JOISC 2014] 稻草人 / Scarecrows
先将所有点按 $x$ 排序,CDQ 分治时找到两个点 $i, j$ 表示矩形的两个端点,处理 $i$ 在左半区间,$j$ 在右半区间的情况。要使 $i, j$ 构成的矩形中不能有任何其他的点,有两种情况:
+ 对于左半区间的非法点 $k$,$x_i \le x_k, y_i \le x_k \le y_j$。不存在非法点 $k$,转化为将 $y$ 从小到大排序后 $x$ 必须是后缀最大值。\ 维护一个单调栈,$y$ 从小到大,$x$ 从大到小,用树状数组维护合法点的数量。
+ 对于右半区间的非法点 $k$,$x_k \le x_j, y_i \le x_k \le y_j$。不存在非法点 $k$,转化为在所有 $x \le x_j, y \le y_j$ 的点中找到最大的 $y_{low}$,左半边可行的 $i$ 必须满足 $y_i > y_{low}$。\ 用树状数组或其他数据结构维护一下 $y_{low}$,统计左半区间合法点的数量即可。
:::success[代码]```cpp#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[代码]```cpp#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_jvoid 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[代码]```cpp#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)$,所以有
$$\begin{align*}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[代码]```cpp#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;}```:::