题目链接
预处理二维前缀和
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define _rep(i,a,b) for(int i=(a);i<=(b);i++)
int n,r,x,y,v;
int s[5010][5010];
int maxn=-INF;
int main()
{
//freopen("in.txt","r",stdin);
memset(s,0,sizeof(s));
scanf("%d%d",&n,&r);
_rep(i,1,n)scanf("%d%d%d",&x,&y,&v),s[x+1][y+1]=v;
_rep(i,1,5001)_rep(j,1,5001)s[i][j]=s[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
_rep(i,r,5000)_rep(j,r,5000)maxn=max(maxn,s[i][j]-s[i-r][j]-s[i][j-r]+s[i-r][j-r]);
printf("%d\n",maxn);
return 0;
}