C - Crowd HDU - 4456
有一个\(N*N\)的网格,有\(M\)个询问/修改,\((1 ≤ N ≤10 000, 1 ≤ M ≤ 80 000)\)
初始每个格点上都是\(0\), 每次修改\(1,x,y,z\)表示将\((x,y)\)的格点上\(+z\),询问\(2,x,y,z\)表示求\((x,y)\)曼哈顿距离\(\leq z\)的格点权值和
$ 1 ≤ x ≤ N, 1 ≤ y ≤ N. $
p = 1 the value of d(x,y) is increased by z, here$ -100 ≤ z ≤ 100.$
p = 2 query the value of \(c(x,y,z)\), here \(0 ≤ z ≤ 2N-1.\)
题解
如果在坐标系求一个矩形内的信息, 是不是可以用矩形的四个点到原点的信息容斥一下?
这样就利用前缀来求出了区间信息
对于原图,将坐标\((x,y)\)转化为\((x-y,x+y)或(x+y,x-y)\)就可以翻转45度,将原图中菱形区间变成正方形(思考, \(\cos(\theta+\frac{\pi}{4})=\cos\theta-\sin\theta~\),\(~\sin(\theta+\frac{\pi}{4})=\cos\theta+\sin\theta\))
然后把询问拆成四个点的询问就好了
注意边界(暴力改大就好...)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
const int BASE = 1e5;
inline int in()
{
int x = 0, flag = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x * flag;
}
int n, m, tot;
int an[MAXN << 2];
struct Fenwick
{
int va[MAXN << 2];
void dec(int pos)
{
for (int i = pos; i <= 400000; i += (i & (-i)))
va[i] = 0;
}
void upd(int pos, int v)
{
for (int i = pos; i <= 400000; i += (i & (-i))) va[i] += v;
}
int qry(int x)
{
int ret = 0;
for (int i = x; i > 0; i &= (i - 1)) ret += va[i];
return ret;
}
} T;
struct Node
{
int opt, x, y, v, t;
bool operator < (const Node & b) const
{
return (x == b.x) ? ((y == b.y) ? t < b.t : y < b.y) : x < b.x;
}
} a[MAXN << 2], b[MAXN << 2];
void solve(int l, int r)
{
if (l >= r) return ;
int mid = (l + r) >> 1;
for (int i = l; i <= r; i ++) b[i] = a[i];
sort(b + l, b + r + 1);
for (int i = l; i <= r; i ++)
{
if (b[i].t <= mid && b[i].opt == 1) T.upd(b[i].y, b[i].v);
else if (b[i].t > mid && b[i].opt == 2) an[b[i].t] += T.qry(b[i].y);
}
for (int i = l; i <= mid; i ++)
if (a[i].opt == 1) T.upd(a[i].y, -a[i].v);
solve(l, mid); solve(mid + 1, r);
}
int main()
{
while (1)
{
n = in();
if (n == 0) break;
m = in(); tot = 0;
memset(an, 0, sizeof an);
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
for (int i = 1; i <= m; i ++)
{
int opt = in(), x = in(), y = in(), v = in();
if (opt == 1)
{
a[++tot] = (Node) { 1, x - y + 3 * n, x + y + 3 * n, v, tot };
}
else
{
int xx = x - y + 3 * n, yy = x + y + 3 * n;
a[++tot] = (Node) { 2, xx - v - 1, yy - v - 1, v, tot };
a[++tot] = (Node) { 2, xx + v, yy - v - 1, v, tot };
a[++tot] = (Node) { 2, xx - v - 1, yy + v, v, tot };
a[++tot] = (Node) { 2, xx + v, yy + v, v, tot };
for (int j = tot - 3; j <= tot; j ++)
{
a[i].x = max(a[i].x, 1), a[i].x = min(a[i].x, 400000);
a[i].y = max(a[i].y, 1), a[i].y = min(a[i].y, 400000);
}
}
}
solve(1, tot);
for (int i = 1; i <= tot; i ++)
if (a[i].opt == 2)
{
printf("%d\n", an[i + 3] - an[i + 2] - an[i + 1] + an[i]);
i += 3;
}
}
return 0;
}
/*
*/
C
如果在坐标系求一个矩形内的信息, 是不是可以用矩形的四个点到原点的信息容斥一下?
这样就利用前缀来求出了区间信息
对于原图,将坐标\((x,y)\)转化为\((x-y,x+y)或(x+y,x-y)\)就可以翻转45度,将原图中菱形区间变成正方形(思考, \(\cos(\theta+\frac{\pi}{4})=\cos\theta-\sin\theta~\),\(~\sin(\theta+\frac{\pi}{4})=\cos\theta+\sin\theta\))
然后把询问拆成四个点的询问就好了
注意边界(暴力改大就好...)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
const int BASE = 1e5;
inline int in()
{
int x = 0, flag = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x * flag;
}
int n, m, tot;
int an[MAXN << 2];
struct Fenwick
{
int va[MAXN << 2];
void dec(int pos)
{
for (int i = pos; i <= 400000; i += (i & (-i)))
va[i] = 0;
}
void upd(int pos, int v)
{
for (int i = pos; i <= 400000; i += (i & (-i))) va[i] += v;
}
int qry(int x)
{
int ret = 0;
for (int i = x; i > 0; i &= (i - 1)) ret += va[i];
return ret;
}
} T;
struct Node
{
int opt, x, y, v, t;
bool operator < (const Node & b) const
{
return (x == b.x) ? ((y == b.y) ? t < b.t : y < b.y) : x < b.x;
}
} a[MAXN << 2], b[MAXN << 2];
void solve(int l, int r)
{
if (l >= r) return ;
int mid = (l + r) >> 1;
for (int i = l; i <= r; i ++) b[i] = a[i];
sort(b + l, b + r + 1);
for (int i = l; i <= r; i ++)
{
if (b[i].t <= mid && b[i].opt == 1) T.upd(b[i].y, b[i].v);
else if (b[i].t > mid && b[i].opt == 2) an[b[i].t] += T.qry(b[i].y);
}
for (int i = l; i <= mid; i ++)
if (a[i].opt == 1) T.upd(a[i].y, -a[i].v);
solve(l, mid); solve(mid + 1, r);
}
int main()
{
while (1)
{
n = in();
if (n == 0) break;
m = in(); tot = 0;
memset(an, 0, sizeof an);
memset(a, 0, sizeof a);
memset(b, 0, sizeof b);
for (int i = 1; i <= m; i ++)
{
int opt = in(), x = in(), y = in(), v = in();
if (opt == 1)
{
a[++tot] = (Node) { 1, x - y + 3 * n, x + y + 3 * n, v, tot };
}
else
{
int xx = x - y + 3 * n, yy = x + y + 3 * n;
a[++tot] = (Node) { 2, xx - v - 1, yy - v - 1, v, tot };
a[++tot] = (Node) { 2, xx + v, yy - v - 1, v, tot };
a[++tot] = (Node) { 2, xx - v - 1, yy + v, v, tot };
a[++tot] = (Node) { 2, xx + v, yy + v, v, tot };
for (int j = tot - 3; j <= tot; j ++)
{
a[i].x = max(a[i].x, 1), a[i].x = min(a[i].x, 400000);
a[i].y = max(a[i].y, 1), a[i].y = min(a[i].y, 400000);
}
}
}
solve(1, tot);
for (int i = 1; i <= tot; i ++)
if (a[i].opt == 2)
{
printf("%d\n", an[i + 3] - an[i + 2] - an[i + 1] + an[i]);
i += 3;
}
}
return 0;
}
/*
*/