版本比较
P4148 简单题 题解
正文
增加 3 行删除 5 行
1123456789101012111214131415161725 行未改动43444546484748495011 行未改动根号重构,每次重构将坐标离散化为排列使得 $x, y$ 唯一对应,然后将二维平面按照 $\sqrt n \times \sqrt n$ 的矩形分块,预处理二维前缀和,查询时整块直接做,散块由于 $x, y$ 唯一对应所以复杂度也是对的。总复杂度时间 $O(n \sqrt n)$,空间 $O(n)$。根号重构,每次重构将坐标离散化为排列使得 $x, y$ 唯一对应,然后将二维平面按照 $\sqrt n \times \sqrt n$ 的矩形分块,预处理二维前缀和,查询时整块直接做,散块由于 $x, y$ 唯一对应所以复杂度也是对的。时间复杂度 $O(n \sqrt n)$,空间复杂度 $O(n)$。
实际上这样做跑的飞快,可以拿到目前的最优解。
https://www.luogu.com.cn/record/265501832
```cpp#include <cmath>#include <iostream>namespace {using namespace std;const int N = 5e5 + 5, M = 2e5 + 5, B = 450;const int N = 5e5 + 5, M = 2e5 + 5, B = 450, S = M / B + 5;int n, m, a[M], b[M], c[M], la[N], ra[N], lb[N], rb[N], ab[M], ba[M], ac[M], bc[M], bel[M], sum[B][B], siz, cnt, lst; sum[S][S], siz, cnt, lst;void rebuild() { siz = sqrt(m = cnt); for (int i = 1; i <= n; i++) ra[i] = rb[i] = 0; for (int i = 1; i <= m; i++) ++ra[a[i]], ++rb[b[i]];25 行未改动 for (int i = m + 1; i <= cnt; i++) if (x1 <= a[i] && a[i] <= x2 && y1 <= b[i] && b[i] <= y2) ret += c[i]; return ret;}} // namespaceint main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n;11 行未改动