思路:二維前綴和spa
#include using namespace std; const int N = 5010; int dp[N][N]; int n, r; int main(){ #ifdef ONLINE_JUDGE #else freopen("in.txt", "r", stdin); #endif //ONLINE_JUDGE int a, b, c, ans; while(~scanf("%d%d", &n, &r)){ for(int i = 1; i <= n; i++){ ans = 0; scanf("%d%d%d", &a, &b, &c); dp[a + 1][b + 1] = c; } for(int i = 1; i <= 5001; i++){ for(int j = 1; j <= 5001; j++){ dp[i][j] += dp[i - 1][j]; } } for(int i = 1; i <= 5001; i++){ for(int j = 1; j <= 5001; j++){ dp[i][j] += dp[i][j - 1]; } } for(int i = r; i <= 5001; i++){ for(int j = r; j <= 5001; j++){ ans = max(ans, dp[i][j] - dp[i - r][j] - dp[i][j - r] + dp[i - r][j - r]); } } printf("%d\n", ans); } return 0; }