二维前缀和
1002. 二哥种花生
https://acm.sjtu.edu.cn/OnlineJudge/problem/1002
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5; int l,w; int a[maxn][maxn],s[maxn][maxn]; int main(){ //freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> l >> w; for(int i = 1; i <= l; i++){ for(int j = 1; j <= w; j++){ cin >> a[i][j]; s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j]; } } int ans = 0; int a,b; cin >> a >> b; for(int x1 = 1; x1 <= l; x1++){ for(int y1 = 1; y1 <= w; y1++){ int x2 = x1 + a - 1,y2 = y1 + b - 1; if(x2 <= l && y2 <= w){ int t = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]; ans = max(ans,t); } } } cout << ans; return 0; }
二维差分