# 每日打卡day9——差分矩阵

#### 数据范围

1≤n,m≤1000
1≤q≤100000
1≤x1≤x2≤n
1≤y1≤y2≤m
−1000≤c≤1000
−1000≤矩阵内元素的值≤1000

#### 输入样例：

``````3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
1 1 2 2 1
1 3 2 3 2
3 1 3 4 1
``````

#### 输出样例：

``````2 3 4 1
4 3 4 1
2 2 2 2``````

``````#include <iostream>
#include <vector>
using namespace std;

const int N = 1010;

int n, m, q;
int l1, r1, l2, r2, v;
vector<vector<int>> nums(N, vector<int>(N, 0));

void insert(int l1, int r1, int l2, int r2, int c){
nums[l1][r1] += c;
nums[l2 + 1][r1] -= c;
nums[l1][r2 + 1] -= c;
nums[l2 + 1][r2 + 1] += c;
}

int main(){
cin >> n >> m >> q;

for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
int c = 0;
cin >> c;
insert(i, j, i, j, c);
}
}

while(q--){
cin >> l1 >> r1 >> l2 >> r2 >> v;
insert(l1, r1, l2, r2, v);
}

for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
nums[i][j] += nums[i - 1][j] + nums[i][j - 1] - nums[i - 1][j - 1];
}
}

for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cout << nums[i][j] << " ";
}
puts(" ");
}

return 0;
}``````

THE END