#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 10; int n, R; int a[N][N]; int b[N][N]; int main() { cin >> n >> R; for (int i = 0; i < n; i++) { int x, y, v; cin >> x >> y >> v; x++, y++; a[x][y] = a[x][y] + v; } for (int i = 1; i <= 1005; i++) { for (int j = 1; j <= 1005; j++) { b[i][j] = a[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1]; } } int ans = 0; for (int i = R; i <= 1005; i++) { for (int j = R; j <= 1005; j++) { ans = max(ans, b[i][j] - b[i - R][j] - b[i][j - R] + b[i - R][j - R]); } } cout << ans; return 0; }
(ps:最好不要求xy最大值,直接用1001或者更大的值作为边界即可)