AcWing 796. 子矩阵的和
原题链接
简单
作者:
ls131
,
2020-05-15 10:10:11
,
所有人可见
,
阅读 506
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1010;
int s[N][N];
int main(){
int n,m,q;
cin>>n>>m>>q;
/*for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ )
scanf("%d", &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];*/
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&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;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%d\n",s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1]); //取区间前缀和值
}
return 0;
}