正文
增加 389 行删除 0 行
242 行未改动全部展开243244245246242 行未改动全部展开return 0;}```:::242 行未改动全部展开243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635242 行未改动全部展开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[代码]```cpp#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$,有两种情况:+ $l_i \le r_j, a_i \times a_j \le l_i, a_j \le \dfrac{l_i}{a_i}$。+ $l_i > r_j, a_i \times a_j \le r_j, a_i \le \dfrac{r_j}{a_j}$。将数组离散化一下用树状数组维护即可。:::success[代码]```cpp#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$ 构成的矩形中不能有任何其他的点,有两种情况:+ 对于左半区间的非法点 $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;}```:::