题目链接:点击这里
本题目标是“点”,二维前缀和是“格子”,如上图所示,实际上是求边长为 的矩形区域。
即套用二维前缀和时,以 为右下角,以 为左上角。
另外,本题注意内存限制较为严格,少开一个二维数组。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 5010;
int a[maxn][maxn];
int main()
{
int N, R;
scanf("%d%d", &N, &R);
int row = R, col = R;
for(int i = 0; i < N; ++i)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
x++; y++; //下标从1开始
row = max(x,row);
col = max(y,col);
a[x][y] += w;
}
for(int i = 1; i <= row; ++i)
for(int j = 1; j <= col; ++j)
a[i][j] = a[i-1][j] + a[i][j-1] - a[i-1][j-1] + a[i][j];
int ans = -1;
for(int i = R; i <= row; ++i)
for(int j = R; j <= col; ++j)
ans = max(ans, a[i][j] - a[i-R][j] - a[i][j-R] + a[i-R][j-R]);
printf("%d\n", ans);
return 0;
}