HDU1559 最大子矩阵和(二维前缀和模板)

二维前缀和可以实现一个矩形区域内的求和问题,而本题恰好就是要求一个矩阵内部的和的最大值,所以直接套用二维前缀和并在结尾进行最大值更新即可。

#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <queue>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#pragma GCC optimize(2)
#pragma warning(disable:4996)
#define lldin(a) scanf("%lld", &a)
#define println(a) printf("%lld\n", a)
#define print(a) printf("%lld ", a)
#define reset(a, b) memset(a, b, sizeof(a))
#define debug cout<<"procedures above are available"<<endl;
#define BigInteger __int128
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 10000000;
const int tool_const = 19991126;
const int tool_const2 = 33;
//template<typename T>
//inline BigInteger nextLong()
//{
//	BigInteger tmp = 0, si = 1;
//	char c;
//	c = getchar();
//	while (!isdigit(c))
//	{
//		if (c == '-')
//			si = -1;
//		c = getchar();
//	}
//	while (isdigit(c))
//	{
//		tmp = tmp * 10 + c - '0';
//		c = getchar();
//	}
//	return si * tmp;
//}
//std::ostream& operator<<(std::ostream& os, __int128 T)
//{
//    if (T<0) os<<"-";if (T>=10 ) os<<T/10;if (T<=-10) os<<(-(T/10));
//    return os<<( (int) (T%10) >0 ? (int) (T%10) : -(int) (T%10) ) ;
//}
//void output(BigInteger x)
//{
//	if (x < 0)
//	{
//		x = -x;
//		putchar('-');
//	}
//	if (x > 9) output(x / 10);
//	putchar(x % 10 + '0');
//}
/**Maintain your determination.Nobody knows the magnificent landscape
at his destination before the arrival with stumble.**/
/**Last Remote**/
ll sum[1200][1200];
ll matrix[1200][1200];
int DETERMINATION()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	ll t;
	cin >> t;
	while (t--)
	{
		ll m, n, x, y;
		cin >> m >> n >> x >> y;
		for (int i = 1; i <= m; i++)
			for (int j = 1; j <= n; j++)
				sum[i][j] = 0;
		for(int i=1;i<=m;i++)
			for (int j = 1; j <= n; j++)
			{
				cin >> matrix[i][j];
				sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + matrix[i][j];
			}
		ll ans = 0;
		for(int i=x;i<=m;i++)
			for (int j = y; j <= n; j++)
			{
				ans = max(ans, sum[i][j] - sum[i - x][j] - sum[i][j - y] + sum[i - x][j - y]);
			}
		cout << ans << endl;
	}
	return 0;
}

 

posted @ 2019-08-26 21:42  完全墨染的樱花  阅读(157)  评论(0编辑  收藏  举报