bzoj 1218: [HNOI2003]激光炸弹

时间:2021-08-05 07:59:33

思路:二维前缀和, 枚举矩形左上端点。

 #include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int>> using namespace std; const int N = + ;
const int M = 1e4 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, r, a[N][N]; int cal(int x, int y) {
return a[x][y];
}
int main() {
scanf("%d%d", &n, &r);
for(int i = ; i <= n; i++) {
int x, y, v; scanf("%d%d%d", &x, &y, &v);
x++; y++;
a[x][y] += v;
} for(int i = ; i <= ; i++) {
for(int j = ; j <= ; j++) {
a[i][j] += a[i - ][j] + a[i][j - ] - a[i - ][j - ];
}
} int ans = ;
for(int i = ; i <= ; i++) {
for(int j = ; j <= ; j++) {
int x = i + r - ;
int y = j + r - ;
x = min(x, );
y = min(y, );
int ret = cal(x, y) - cal(i - , y) - cal(x, j - ) + cal(i - , j - );
ans = max(ans, ret);
}
}
printf("%d\n", ans);
return ;
} /*
*/