前缀和水题,没什么好讲的,注意横纵坐标加1。
另外此题空间有点紧。
代码实现如下:
#includeusing namespace std;#define rep(i, a, b) for (register int i = (a); i <= (b); i++)const int maxn = 5e3 + 5;int n, m, N, R;int a[maxn][maxn];int MAX(int a, int b) { return a > b ? a : b;}int read() { int x = 0, flag = 0; char ch = ' '; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') { flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar(); } return flag ? -x : x;}void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0');}int main() { N = read(), R = read(); n = R, m = R; rep(i, 1, N) { int x, y, w; x = read(), y = read(), w = read(); x++, y++; a[x][y] = w; n = MAX(n, x); m = MAX(m, y); } rep(i, 1, n) rep(j, 1, m) a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; int ans = 0; rep(i, R, n) rep(j, R, m) ans = MAX(ans, a[i][j] - a[i - R][j] - a[i][j - R] + a[i - R][j - R]); write(ans); return 0;}