AcWing99 激光炸弹 (二维前缀和)

题目链接:https://www.acwing.com/problem/content/101/

维护二维前缀和,扫一遍边长为\(R\)的正方形的最大值即可,注意边界不要溢出

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 5010;

int n,R;
int sum[maxn][maxn];

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	n = read(), R = read();
	
	int x, y, w;
	for(int i=1;i<=n;++i){ x = read(), y = read(), w = read(); sum[x+1][y+1] += w; }
	
	for(int i=1;i<=5001;++i){
		for(int j=1;j<=5001;++j){
			sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + sum[i][j];
		}
	}
	
	int ans = 0;
	for(int i=1;i<=5001;++i){
		for(int j=1;j<=5001;++j){
			ans = max(ans, sum[i][j] - sum[max(0,i-R)][j] - sum[i][max(0,j-R)] + sum[max(0,i-R)][max(0,j-R)]);
		}
	}
	
	printf("%d\n",ans);
	
	return 0;
}
posted @ 2020-11-01 16:40  Tartarus_li  阅读(70)  评论(0编辑  收藏  举报