思路:二维前缀和, 枚举矩形左上端点。
1 #include<bits/stdc++.h> 2 #define LL long long 3 #define fi first 4 #define se second 5 #define mk make_pair 6 #define pii pair<int,int> 7 #define piii pair<int, pair<int,int>> 8 9 using namespace std; 10 11 const int N = 5000 + 7; 12 const int M = 1e4 + 7; 13 const int inf = 0x3f3f3f3f; 14 const LL INF = 0x3f3f3f3f3f3f3f3f; 15 const int mod = 1e9 + 7; 16 17 int n, r, a[N][N]; 18 19 int cal(int x, int y) { 20 return a[x][y]; 21 } 22 int main() { 23 scanf("%d%d", &n, &r); 24 for(int i = 1; i <= n; i++) { 25 int x, y, v; scanf("%d%d%d", &x, &y, &v); 26 x++; y++; 27 a[x][y] += v; 28 } 29 30 for(int i = 1; i <= 5001; i++) { 31 for(int j = 1; j <= 5001; j++) { 32 a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; 33 } 34 } 35 36 int ans = 0; 37 for(int i = 1; i <= 5001; i++) { 38 for(int j = 1; j <= 5001; j++) { 39 int x = i + r - 1; 40 int y = j + r - 1; 41 x = min(x, 5001); 42 y = min(y, 5001); 43 int ret = cal(x, y) - cal(i - 1, y) - cal(x, j - 1) + cal(i - 1, j - 1); 44 ans = max(ans, ret); 45 } 46 } 47 printf("%d ", ans); 48 return 0; 49 } 50 51 /* 52 */