题目链接:
https://nanti.jisuanke.com/t/41298
题解:
这里要解决几个问题:
① ( x , y ) (x,y) (x,y)在螺旋方阵的值
②用某种数据结构查询矩形区间内元素值之和
由于这里 n n n很大( n ≤ 1 0 6 n\leq10^6 n≤106),二维数组开不下
所以问题①不能直接暴力求解,问题②不能用前缀和直接求出
对于问题①,我们可以先求出点 ( x , y ) (x,y) (x,y)在第几层(一圈一圈),然后分四种情况快速计算
对于问题②,先来分析一下:
若这里是1维的区间查找,直接线段树即可
如今多了1维,变成二维的区间查找,实际上就是不同时刻的线段树区间求和做差,也就是主席树
这里我们一列一列的插入形成不同时刻的线段树,然后在y轴上查询区间
这里还是有点不一样的就是平时主席树都是一个点一个点地插入,现在我们是一列一列地插入,但是实际上处理起来还是一样的,只是需要记录一下当前列第一个点和最后一个点,具体可以看代码
由于这里 n ≤ 1 0 6 n\leq10^6 n≤106,而插入点个数 m ≤ 1 0 5 m\leq10^5 m≤105,故我们需要离散化处理下防止MLE
这题思路比较简单,但是代码一点都不简单,看了别人的好久才写出来
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef vector<int> vi;
typedef pair<int, int> pii;
#ifndef ACM_LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 10;
const int M = N * 40;
struct SegTree {
#define m (l + r)/2
int last;
ll s[M];
int tot, lc[M], rc[M];
inline int build(int l, int r) {
int u = ++tot;
s[u] = 0;
if (l < r) {
lc[u] = build(l, m);
rc[u] = build(m + 1, r);
}
return u;
}
inline int init(int x) {
tot = 0;
last = x;
return build(1, last);
}
inline int insert(int pre, int l, int r, int k, int v) {//正常插入操作即可
int now = ++tot;
lc[now] = lc[pre], rc[now] = rc[pre], s[now] = s[pre] + v;
if (l < r) {
if (k <= m)lc[now] = insert(lc[pre], l, m, k, v);
else rc[now] = insert(rc[pre], m + 1, r, k, v);
}
return now;
}
ll ans;
inline void query(int u, int v, int l, int r, int k) {
if (l == r) {//到达叶节点
ans += s[u] - s[v];
return;
}
if (k <= m)query(lc[u], lc[v], l, m, k);//当前点在左子树,查询左子树
else {
ans += s[lc[u]] - s[lc[v]];//加上左子树的值
query(rc[u], rc[v], m + 1, r, k);//查询右子树
}
}
inline ll num(int u, int v, int k) {
ans = 0;
query(u, v, 1, last, k);
return ans;
}
inline ll sum(int u, int v, int l, int r) {//不同时刻的线段树查询后做差即为答案
ll sum_r = num(u, v, r), sum_l = 0;
if (l > 1)sum_l = num(u, v, l - 1);
return sum_r - sum_l;
}
#undef m
}tree;
struct Hash {//离散化处理
int a[N], tot;
void init() { tot = 0; }
void insert(int x) { a[++tot] = x; }
void build() {
sort(a + 1, a + 1 + tot);
tot = unique(a + 1, a + 1 + tot) - (a + 1);
}
inline int operator[] (int x) { return lower_bound(a + 1, a + 1 + tot, x) - a; }
inline int query(int x) {
return upper_bound(a + 1, a + 1 + tot, x) - a - 1;
}
}hx, hy;
int n, m, q;
int rt[N];
ll pre[N], val[N];
pii a[N], range[N];
void init() {//螺旋矩阵前缀和
pre[0] = 0;
for (int i = 1; i <= n / 2; i++)
pre[i] = pre[i - 1] + 4 * (n - 2 * i + 1);
}
int calc(int x, int y) {//计算(x,y)对应螺旋矩阵中的数
int k = min(min(x, n - x + 1), min(y, n - y + 1));
ll ans = pre[k - 1] + 1;
int l = k, r = n - k + 1;
if (y == r)ans += r - x;
else if (x == l)ans += r - l + r - y;
else if (y == l)ans += 2 * (r - l) + x - l;
else ans += 3 * (r - l) + y - l;
int res = 0;
while (ans)res += ans % 10, ans /= 10;
return res;
}
void solve() {
cin >> n >> m >> q;
init();
hx.init();
hy.init();
hx.insert(0);
hy.insert(0);
for (int i = 0; i < m; i++) {
cin >> a[i].first >> a[i].second;
hx.insert(a[i].first);//x和y单独离散化
hy.insert(a[i].second);
}
hx.build();
hy.build();
sort(a, a + m);
for (int i = 0; i < m; i++) {
val[i] = calc(a[i].second, a[i].first);
a[i].first = hx[a[i].first];
a[i].second = hy[a[i].second];
}
int mx = hx.tot;
int my = hy.tot;
rt[0] = tree.init(my);
for (int i = 1, top = 0; i <= mx; i++) {
range[i].first = rt[top];//当前列的第一个点的根节点
while (top < m && a[top].first <= i) {
rt[top + 1] = tree.insert(rt[top], 1, my, a[top].second, val[top]);
top++;
}
range[i].second = rt[top];//当前列的最后一个点的根节点
}
while (q--) {
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1 = hx[x1];
y1 = hy[y1];
x2 = hx.query(x2);
y2 = hy.query(y2);
cout << tree.sum(range[x2].second, range[x1].first, y1, y2) << endl;
}
}
int main() {
#ifdef ACM_LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}