首先你要知道莫队怎么做三维偏序:https://www.luogu.com.cn/article/sh9evz6h
然后静态数点相对于偏序其实就是你把修改和询问拆成两个。
这简直就是板子,不用脑子就能写出 的三维莫队套一维分块代码。
然后你发现你寄了。
我本地随机数据测的是 10s,然后洛谷又比本地慢,于是寄了。
怎么办呢?
作为坚定的根号算法使用者,我们肯定不能向 cdq 屈服!
此时你突然看到这个题:https://www.luogu.com.cn/problem/P6778
哦原来我们可以二维分块!
你发现你可以做到什么? 单点修和 矩形和。
具体的,我们按照 、、、 这四种大小对二维平面分块,然后单点加就是直接找到对应的块加,矩形和就是拆成块求和,但是对于剩下 、 的部分怎么做?你注意到离散化为排列后每行每列只会有一个值,所以直接做复杂度还是对的。
然后你就可以给莫队减去一维,你发现这样二维莫队套二维分块就是 的!
这样就做完了。
https://www.luogu.com.cn/record/256578031
#include <algorithm>
#include <iostream>
namespace {
using namespace std;
const int N = 4e5 + 5, B0 = 25, B1 = 625, B2 = 15625, S1 = N / B1 + 5, S2 = N / B2 + 5;
int n, m, a[N], b[N], c[N], d[N], ab[N], ac[N], ad[N], ba[N], bc[N], bd[N], cd[N], dc[N], b1[N],
b2[N], v0[N], s1[S1][S1], s2[S1][S2], s3[S2][S1], s4[S2][S2], ans[N];
struct node {
int x, y;
bool operator<(const node &r) const { return x < r.x; }
} va[N], vb[N], vc[N], vd[N];
struct query {
int a, b, c, d, id;
bool operator<(const query &r) const {
if (b1[a] != b1[r.a]) return a < r.a;
return b1[a] & 1 ? b < r.b : b > r.b;
}
} q[N];
void insert(int x, int y) {
++v0[x], ++s1[b1[x]][b1[y]], ++s2[b1[x]][b2[y]], ++s3[b2[x]][b1[y]], ++s4[b2[x]][b2[y]];
}
void remove(int x, int y) {
--v0[x], --s1[b1[x]][b1[y]], --s2[b1[x]][b2[y]], --s3[b2[x]][b1[y]], --s4[b2[x]][b2[y]];
}
int query(int x, int y) {
if (!x || !y) return 0;
int ret = 0;
for (int i = 1; i < b2[x]; i++)
for (int j = 1; j < b2[y]; j++) ret += s4[i][j];
for (int i = 1; i < b2[x]; i++)
for (int j = B0 * (b2[y] - 1) + 1; j < b1[y]; j++) ret += s3[i][j];
for (int i = B0 * (b2[x] - 1) + 1; i < b1[x]; i++)
for (int j = 1; j < b2[y]; j++) ret += s2[i][j];
for (int i = B0 * (b2[x] - 1) + 1; i < b1[x]; i++)
for (int j = B0 * (b2[y] - 1) + 1; j < b1[y]; j++) ret += s1[i][j];
for (int i = B1 * (b1[x] - 1) + 1; i <= x; i++)
if (cd[i] <= B1 * (b1[y] - 1)) ret += v0[i];
for (int i = B1 * (b1[y] - 1) + 1; i <= y; i++)
if (dc[i] <= x) ret += v0[dc[i]];
return ret;
}
} // namespace
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
va[i] = {a[i], i}, vb[i] = {b[i], i}, vc[i] = {-c[i], i}, vd[i] = {-d[i], i};
}
sort(va + 1, va + n + 1), sort(vb + 1, vb + n + 1);
sort(vc + 1, vc + n + 1), sort(vd + 1, vd + n + 1);
for (int i = 1; i <= n; i++) a[va[i].y] = i, b[vb[i].y] = i, c[vc[i].y] = i, d[vd[i].y] = i;
for (int i = 1; i <= n; i++) {
ba[b[i]] = a[i], ab[a[i]] = b[i];
ac[a[i]] = bc[b[i]] = dc[d[i]] = c[i], ad[a[i]] = bd[b[i]] = cd[c[i]] = d[i];
}
for (int i = 1; i <= m; i++) {
cin >> q[i].a >> q[i].b >> q[i].c >> q[i].d, q[i].id = i;
q[i].a = lower_bound(va + 1, va + n + 1, node{q[i].a + 1, 0}) - va - 1;
q[i].b = lower_bound(vb + 1, vb + n + 1, node{q[i].b + 1, 0}) - vb - 1;
q[i].c = lower_bound(vc + 1, vc + n + 1, node{-q[i].c + 1, 0}) - vc - 1;
q[i].d = lower_bound(vd + 1, vd + n + 1, node{-q[i].d + 1, 0}) - vd - 1;
}
for (int i = 1; i <= n; i++) b1[i] = (i - 1) / B1 + 1;
for (int i = 1; i <= n; i++) b2[i] = (i - 1) / B2 + 1;
sort(q + 1, q + m + 1);
for (int i = 1, x = 0, y = 0; i <= m; i++) {
while (x < q[i].a)
if (ab[++x] <= y) insert(ac[x], ad[x]);
while (x > q[i].a)
if (ab[x--] <= y) remove(ac[x + 1], ad[x + 1]);
while (y < q[i].b)
if (ba[++y] <= x) insert(bc[y], bd[y]);
while (y > q[i].b)
if (ba[y--] <= x) remove(bc[y + 1], bd[y + 1]);
ans[q[i].id] = query(q[i].c, q[i].d);
}
for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
}