#53. [R9E]炸弹2
对于原矩阵,我们硬做是困难。
考虑有没有什么转化方法可以让我们轻易的达成题目炸到的条件。
此时我们有一个套路,就是可以把原图中的曼哈顿距离,转化为切比雪夫距离就变成一个矩形只需要二位前缀和即可满足条件。
对于所有点 \((x,y)\) 使用曼哈顿距离,变成切比雪夫距离之后 \((x+y,x-y)\)。
#include <bits/stdc++.h>
#define int long long
#define upp(a, x, y) for (int a = x; a <= y; a++)
#define dww(a, x, y) for (int a = x; a >= y; a--)
#define pb(x) push_back(x)
#define endl '\n'
#define x first
#define y second
#define PII pair<int, int>
using namespace std;
const int N = 5010, X = 1001;
int a[N][N], sum[N][N], n, m, q;
int solve(int x1, int y1, int x2, int y2) {
return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] +
sum[x1 - 1][y1 - 1];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> q;
upp(i, X, X + n - 1) upp(j, X, X + m - 1) {
int x;
cin >> x;
a[i + j][i - j + 2 * X] = x;
}
upp(i, 0, N - 1) upp(j, 0, N - 1) {
int val = 0;
if (i > 0) val += sum[i - 1][j];
if (j > 0) val += sum[i][j - 1];
if (i > 0 && j > 0) val -= sum[i - 1][j - 1];
sum[i][j] = val + a[i][j];
}
while (q--) {
int x, y, w;
cin >> x >> y >> w;
x += X - 1;
y += X - 1;
x = x + y;
y = x - y - y + 2 * X;
cout << solve(x - w, y - w, x + w, y + w) << endl;
}
return 0;
}

浙公网安备 33010602011771号