P2280 [HNOI2003]激光炸弹(二维前缀和)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 2000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)

int n, m;
int s[5005][5005];
//int sum[5005][5005];
int main() {
	scanf("%d %d", &n, &m);
	m=min(m,5000);
	int x, y, t;
	int maxx = m;
	int maxy = m;
	for (int i = 1; i <= n; i++) {
		scanf("%d %d %d", &x, &y, &t);
		s[x + 1][y + 1] = t;
		maxx = max(maxx, x + 1);
		maxy = max(maxy, y + 1);
	}
	for (int i = 1; i <= maxx; i++) {
		for (int j = 1; j <= maxy; j++) {
			s[i][j] = s[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
		}
	}
	int ans = 0;
	for (int i = m; i <= maxx; i++) {
		for (int j = m; j <= maxy; j++) {
			ans = max(ans, s[i][j] - s[i - m][j] - s[i][j - m] + s[i - m][j - m]);
		}
	}
	printf("%d", ans);
	return 0;
}

你可能感兴趣的:(算法竞赛进阶指南,算法)