-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferenceMatrix.cpp
More file actions
54 lines (44 loc) · 1.13 KB
/
Copy pathDifferenceMatrix.cpp
File metadata and controls
54 lines (44 loc) · 1.13 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @Author: Cyan
* @Date: 2021-07-06 20:41:55
* @LastEditors: Cyan
* @LastEditTime: 2021-07-06 20:41:55
* @Description: file content
*/
#include<iostream>
using namespace std;
const int N = 1010;
int a[N][N],b[N][N];
int m,n,q;
void insert(int x1, int y1, int x2, int y2,int c){
b[x1][y1] += c;
b[x2 + 1][y1] -= c;
b[x1][y2+1] -= c;
b[x2+1][y2+1] += c;
}
int main(){
scanf("%d%d%d",&m,&n,&q);
for(int i = 1; i<= m; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
// 初始化b矩阵 为a矩阵的差分矩阵
for(int i = 1; i<=m; i++)
for(int j = 1; j<=n; j++)
insert(i,j,i,j,a[i][j]);
// 输入
while(q--){
int x1,y1,x2,y2,c;
cin >> x1 >> y1 >> x2 >> y2 >> c;
insert(x1,y1,x2,y2,c);
}
// 反过来求前缀和,得到最后的结果
for(int i = 1; i<=m; i++)
for(int j = 1; j<=n ; j++)
b[i][j] += b[i-1][j] + b[i][j-1] - b[i-1][j-1];
// 输出
for(int i = 1; i <= m; i++){
for(int j =1 ;j<=n; j++)
printf("%d ",b[i][j]);
cout<<endl;
}
}