AcWing 796.子矩阵的和

AcWing 796.子矩阵的和

第二层矩阵存当前(0,0)-(x,y)和,求x1yx1-x2y2类似于求面积

可理解为坡度(大概)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<cmath>
#include<string>
#include<iomanip>
#include<algorithm>
#include<map>
#include<vector>
#include<set>

using namespace std;

typedef long long ll;
const int N=100010;
ll v[N][1000][2]={0};
int main(){
int n,m,q;
cin>>n>>m>>q;
for(int j=1;j<=n;j++){
for(int i=1;i<=m;i++){
cin>>v[j][i][0];
v[j][i][1]=v[j-1][i][1]+v[j][i-1][1]-v[j-1][i-1][1]+v[j][i][0];
}

}


int x1,y1,x2,y2,count;
for(int i=0;i<q;i++){
count=0;
cin>>x1>>y1>>x2>>y2;
ll count=v[x2][y2][1]-v[x2][y1-1][1]-v[x1-1][y2][1]+v[x1-1][y1-1][1];
cout<<count<<endl;
}
}
Author

Daniel Niu

Posted on

2024-03-18

Updated on

2024-06-26

Licensed under

Comments