版本比较
组合计数
正文
增加 81 行删除 0 行
10 行未改动111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910029 行未改动10 行未改动然后考虑都相交。容斥一下,我们求与 $i$ 相交,但是自身不交的,这个可以用线段树维护,考虑存有多少个左端点和右端点,合并时,把左侧右端点乘以右侧左端点的贡献加上即可。
然后就做完了,没看懂题解。
写完了。
```cpp#include <bits/stdc++.h>using namespace std;#define int long longconstexpr int N = 2e5 + 9;int n, b[2 * N], c[2 * N], vec[2 * N], d[N];struct fenwick { int c[2 * N]; void update(int x, int y) { for (; x <= 2 * n; x += x & -x) c[x] += y; } int query(int x, int r = 0) { for (; x > 0; x -= x & -x) r += c[x]; return r; }} adl, adr, dll, dlr, all, alr;struct pt { int x1, y1, x2, y2;} a[N];struct info { int ls, rs, s; info operator+(const info &x) const { return {ls + x.ls, rs + x.rs, s + x.s + rs * x.ls}; }} sgt[N << 3];void update(int u, int l, int r, int x, const info &y) { if (l == r) return sgt[u] = y, void(); int m = (l + r) >> 1; if (m >= x) update(u << 1, l, m, x, y); else update(u << 1 | 1, m + 1, r, x, y); sgt[u] = sgt[u << 1] + sgt[u << 1 | 1];}info query(int u, int l, int r, int x, int y) { if (x <= l && r <= y) return sgt[u]; int m = (l + r) >> 1; if (m >= x && m < y) return query(u << 1, l, m, x, y) + query(u << 1 | 1, m + 1, r, x, y); else if (m >= x) return query(u << 1, l, m, x, y); else return query(u << 1 | 1, m + 1, r, x, y);}int C(int x) { return x * (x - 1) / 2; }signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i].x1 >> a[i].x2 >> a[i].y1 >> a[i].y2; b[2 * i] = a[i].x1, b[2 * i - 1] = a[i].x2, c[2 * i] = a[i].y1, c[2 * i - 1] = a[i].y2; } sort(b + 1, b + 2 * n + 1); sort(c + 1, c + 2 * n + 1); for (int i = 1; i <= n; ++i) { a[i].x1 = (int) (lower_bound(b + 1, b + 2 * n + 1, a[i].x1) - b); a[i].x2 = (int) (lower_bound(b + 1, b + 2 * n + 1, a[i].x2) - b); a[i].y1 = (int) (lower_bound(c + 1, c + 2 * n + 1, a[i].y1) - c); a[i].y2 = (int) (lower_bound(c + 1, c + 2 * n + 1, a[i].y2) - c); // cerr << a[i].x1 << ' ' << a[i].y1 << ' ' << a[i].x2 << ' ' << a[i].y2 << '\n'; vec[a[i].x1] = i, vec[a[i].x2] = -i, d[i] = -1; } int g0 = n * (n - 1) * (n - 2) / 6, g1 = 0, g2 = 0, g3 = 0; for (int i = 1; i <= 2 * n; ++i) { int x = vec[i]; if (x > 0) { d[x] -= dll.query(a[x].y2) - dlr.query(a[x].y1 - 1); g3 += C(all.query(a[x].y2) - alr.query(a[x].y1 - 1)) - query(1, 1, 2 * n, a[x].y1, a[x].y2).s; all.update(a[x].y1, 1), alr.update(a[x].y2, 1); adl.update(a[x].y1, 1), adr.update(a[x].y2, 1); update(1, 1, 2 * n, a[x].y1, {1, 0, 0}); update(1, 1, 2 * n, a[x].y2, {0, 1, 0}); } else { x = -x, d[x] += adl.query(a[x].y2) - adr.query(a[x].y1 - 1); all.update(a[x].y1, -1), alr.update(a[x].y2, -1); dll.update(a[x].y1, 1), dlr.update(a[x].y2, 1); update(1, 1, 2 * n, a[x].y1, {0, 0, 0}); update(1, 1, 2 * n, a[x].y2, {0, 0, 0}); } } for (int i = 1; i <= n; ++i) { g1 += d[i], g2 += C(d[i]); } // cerr << g3 << '\n'; cout << g0 - (n - 2) * g1 / 2 + g2 - g3;}```
[P7247](https://www.luogu.com.cn/problem/P7247)
垃圾的 min-max 容斥做法。
29 行未改动