解题思路
二维前缀和。
代码
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 5005; inline int rd(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();} while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return f?x:-x; } int n,r,a[MAXN][MAXN],ans; int main(){ n=rd(),r=rd();int x,y,w; for(int i=1;i<=n;i++){ x=rd(),y=rd(),w=rd();x++;y++; a[x][y]=w; } for(int i=1;i<=5001;i++) for(register int j=1;j<=5001;j++) a[i][j]=a[i][j]+a[i-1][j]+a[i][j-1]-a[i-1][j-1]; for(register int i=r;i<=5001;i++) for(register int j=r;j<=5001;j++) ans=max(ans,a[i][j]-a[i-r][j]-a[i][j-r]+a[i-r][j-r]); cout<<ans<<endl; return 0; }