简单题解

· · 题解

这道题,怎么,没有 polylog 的题解。。。
追求一下渐进复杂度。
这里给出一种较为简单的 polylog 算法,仅供参考。

首先,我们发现这是一个三维偏序,强制在线,卡空间。
三维问题并不好处理,考虑先降掉一维。
直接对时间进行 CDQ 分治即可。

然而强制在线。
不过考虑一下 CDQ 的过程,我们把递归树建出来。
容易发现,每次可以先递归左边,然后递归右边,最后计算左右贡献。
而我们的修改/查询是从左到右读入的。
所以动态维护递归的过程即可。

然而这样只能合并信息。
查询的话,考虑任意时刻最多只有 O(\log n) 个节点。遍历每个节点求解即可。

上面的思路可能比较抽象。等效的理解方式是二进制分组。
新加入一个点暴力进位即可。
复杂度分析同 CDQ 分治。只考虑合并的话可以做到 O(n \log n) 或 O(n \log ^ 2 n)。

现在考虑,对于每个节点,我们有静态的若干个二维点,需要支持矩形求和。

考虑常规 O(n \sqrt n) 算法,对所有点按 x 坐标排序,然后分块,用分散层叠优化二分。
单次查询复杂度 O(\sqrt n + \log n)。可以说很差。

如果两边差距很大,那就平衡复杂度吧。
直接把块长开到 O(\frac{n}{\log n}),这样整块查询就变成了 O(\log n)。

考虑散块。块长过大,显然不能再暴力扫了。
所以使用上述结构递归处理即可。

分析一下复杂度。
为了方便考虑,我们假设查询都是前缀查询。

则单次查询复杂度 T(n) = T(\frac{n}{\log n}) + O(\log n),解得 T(n) = O(\log n)。
由于递归树上面有 O(\log n) 个节点所以总的复杂度为 O(\log ^ 2 n)。

更重要的是空间复杂度。

本人过菜不会解。 下附神仙 [UnyieldingTrilobite](https://www.luogu.com.cn/user/250637) 给出的证明。 > 由于每一层都是线性所以本质上只需要分析把 $n$ 每次除以 $\log n$ 几次能变成 1。很显然这个东西的级别不会超过 $\log_{\log n}n = \frac{\log n}{\log \log n}$,然后我们不难发现在 $n$ 变到根号规模之前每次除的东西的规模是不会变的,所以这个东西的级别不会低于 $\log_{\log n} \sqrt n$,然后就是一样的。 事实上,在这道题的数据范围下 $\frac{\log n}{\log \log n}$ 是不大于 4 的。可以认为是个大常数吧。。。 整体复杂度优于树套树。 可能还能通过一些其他技巧做到更加优秀的复杂度。 能力有限仅分析至此。 --- 上面的做法复杂度确实优秀,然而。。。。 一方面,分散层叠的空间常数本身就不小,这道题开的 20M 属实不够用。 另一方面,由于内存访问的原因,实际测得上述算法的效率**极低**,一点也不像是 $O(n \log ^ 2 n)$ 该有的样子。 对于静态区间 $< k$ 的数的个数,用上述算法(去掉 CDQ)时间复杂度为 $O(n \log n)$,然而在 $10^6$ 的数据范围下需要 8s 的时间,空间约为 100M。 所以上述算法分析一下复杂度,理论很优也就行了吧。 我们考虑如何通过这道题。 既然是分散层叠常数大,那就把分散层叠扔了即可。 直接每个块暴力二分,时间复杂度 $O(n \log ^ 3 n)$,空间复杂度 $O(\frac{n \log n}{\log \log n})$。 实际上由于常数极小跑的飞快。 朴素实现最大的测试点也没有超过 650ms。 同时使用的空间约为 8M。 下附代码。有点冗长。 ```cpp #include <stdio.h> #include <algorithm> using namespace std; const int BL = 64; const int B[12] = {1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4}; struct site { int x, y, val; site() = default; site(int a, int b, int c) { x = a; y = b; val = c; } }; site poi[BL]; struct asd { int y, sum; asd() = default; asd(int a) { y = a; } }; struct node { bool flag; int len; long long sum; site *num; asd *fc; void div(int, int, int); int geta(int, int, int, int); int gest(asd*, int, int, int, int, int, int); }; node work[12]; site space[277777]; asd anospace[909033]; int top, last; int read(); void load(); void adj(); int sol(int, int, int, int); int main() { int x, y, a, b, c; read(); load(); while (1) if ((c = read()) == 1) { x = read() ^ last; y = read() ^ last; poi[top++] = site(x, y, read() ^ last); if (top == BL) adj(); }else if (c == 2) { x = read() ^ last; a = read() ^ last; y = read() ^ last; b = read() ^ last; last = sol(x, y, a, b); printf ("%d\n", last); }else break; return 0; } int read() { int x = 0; char c = getchar(); while (c < '0') c = getchar(); do { x = x * 10 + (c & 15); c = getchar(); }while (c >= '0'); return x; } bool cmp0(const asd &a, const asd &b) { return a.y < b.y; } bool cmp1(const site &a, const site &b) { return a.x < b.x; } void load() { int p1 = 0, p2 = 0; for (int i = 0, j = 64; i < 12; ++i, j <<= 1) { work[i].flag = false; work[i].len = j; work[i].num = space + p1; work[i].fc = anospace + p2; p1 += j + 1; p2 += B[i] * j + 1; } } void adj() { for (int i = 0; ; ++i) if (!work[i].flag) { work[i].flag = true; work[i].sum = 0LL; int x = 0; for (int j = 0; j < i; ++j) { work[j].flag = false; for (int k = 0; k < work[j].len; ++k) work[i].num[x + k] = work[j].num[k]; x += work[j].len; } for (int j = 0; j < top; ++j) work[i].num[x + j] = poi[j]; sort(work[i].num, work[i].num + work[i].len, cmp1); for (int j = 0; j < work[i].len; ++j) work[i].sum += work[i].num[j].val; work[i].div(0, work[i].len - 1, 0); break; } top = 0; } int sol(int a, int b, int c, int d) { int ret = 0; for (int i = 0; i < top; ++i) if (poi[i].x >= a && poi[i].x <= b && poi[i].y >= c && poi[i].y <= d) ret += poi[i].val; for (int i = 0; i < 12; ++i) if (work[i].flag) ret += work[i].geta(a, b, c, d); return ret; } void node::div(int a, int b, int c) { if (b - a + 1 <= 60) return; int big = (b - a + 1) / __lg(b - a + 1); asd *f = fc + c * len; for (int i = a, j; i <= b; i = j + 1) { j = min(b, i + big - 1); for (int k = i; k <= j; ++k) { f[k].y = num[k].y; f[k].sum = num[k].val; } sort(f + i, f + j + 1, cmp0); for (int k = i + 1; k <= j; ++k) f[k].sum += f[k - 1].sum; div(i, j, c + 1); } } int node::geta(int a, int b, int c, int d) { int x, y; x = lower_bound(num, num + len, site(a, a, 0), cmp1) - num; y = upper_bound(num, num + len, site(b, b, 0), cmp1) - num - 1; if (x > y) return 0; return gest(fc, 0, len - 1, x, y, c, d); } int node::gest(asd *s, int lf, int rt, int a, int b, int c, int d) { int ret = 0; if (b - a + 1 <= 60) { for (int i = a; i <= b; ++i) if (num[i].y >= c && num[i].y <= d) ret += num[i].val; return ret; } int big = (rt - lf + 1) / __lg(rt - lf + 1); int oa = (a - lf) / big, ob = (b - lf) / big; if (oa == ob) return gest(s + len, oa * big + lf, min((oa + 1) * big + lf - 1, rt), a, b, c, d); if (a != oa * big + lf) { ret += gest(s + len, oa * big + lf, (oa + 1) * big + lf - 1, a, (oa + 1) * big + lf - 1, c, d); ++oa; } if (b != min((ob + 1) * big + lf - 1, rt)) { ret += gest(s + len, ob * big + lf, min((ob + 1) * big + lf - 1, rt), ob * big + lf, b, c, d); --ob; } for (int i = oa, j = oa * big + lf; i <= ob; ++i, j += big) { int x, y; x = lower_bound(s + j, s + min(j + big, rt + 1), asd(c), cmp0) - s - 1; y = upper_bound(s + j, s + min(j + big, rt + 1), asd(d), cmp0) - s - 1; if (y < j || x > y) continue; ret += s[y].sum; if (x >= j) ret -= s[x].sum; } return ret; } ```