假设边长为2,该题就是将边长为2的正方形从左到右平移,然后换下一行从左到右平移,边平移边记录该区域内的总值,最终取一个最大的
/* 前缀和: a[1], a[2], a[3], ... a[n] s[i] = a[1] + a[2] + ... + a[i] a[3] + a[4] + ... + a[15] = s[15] - s[2] */ #include <iostream> using namespace std; const int N = 5010; int g[N][N]; int main() { int N, R; cin >> N >> R; int n = R, m = R; for(int i = 0, x, y, w; i < N; ++ i) { cin >> x >> y >> w; x ++, y ++; n = max(n, x), m = max(m, y); g[x][y] += w; } // 前缀和 for(int i = 1; i <= n; ++ i) for(int j = 1; j <= m; ++ j) g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1]; int res = 0; for(int i = R; i <= n; ++ i) for(int j = R; j <= m; ++ j) res = max(res, g[i][j] - g[i - R][j] - g[i][j - R] + g[i - R][j - R]); cout << res << endl; return 0; }