1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 5010; 4 int g[N][N]; 5 int main() { 6 int n, r; 7 cin >> n >> r; 8 int nn = r, mm = r; //判断边界 9 for (int i = 0; i < n; i++) { 10 int x, y, w; 11 cin >> x >> y >> w; 12 x++; 13 y++; //前缀和下标从1开始 14 nn = max(nn, x); 15 mm = max(mm, y); 16 g[x][y] += w; 17 } 18 for (int i = 1; i <= nn; i++) { 19 for (int j = 1; j <= mm; j++) { 20 g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1]; 21 } 22 } 23 int res = 0; 24 //枚举所有边长是r的矩形,枚举(i, j)为其右下角 25 for (int i = r; i <= nn; i++) { 26 for (int j = r; j <= mm; j++) { 27 res = max(res, g[i][j] - g[i - r][j] - g[i][j - r] + g[i - r][j - r]); 28 } 29 } 30 cout << res << endl; 31 return 0; 32 }