1.S[i,j]即为图1红框中所有数的的和为:
S[i,j]=S[i,j−1]+S[i−1,j]−S[i−1,j−1]+a[i,j]
2.(x1,y1),(x2,y2)这一子矩阵中的所有数之和为:
S[x2,y2]−S[x1−1,y2]−S[x2,y1−1]+S[x1−1,y1−1]
#include <iostream>
using namespace std;
const int N = 1e3 + 10;
int main()
{
int n, m, q;
cin >> n >> m >> q;
int s[N][N];
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= m; j ++ )
cin >> s[i][j];
for(int i = 1; i <= n; i ++ )
for(int j = 1; j <= m; j ++ )
s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
while(q -- ){
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1] << endl;
}
return 0;
}
标签:796,x1,前缀,int,矩阵,x2,y1,y2,Acwing
From: https://www.cnblogs.com/lint-ss/p/17441243.html