-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
33 lines (33 loc) · 972 Bytes
/
Solution.cpp
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
class Solution {
public:
vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {
vector<vector<int>> mat(n, vector<int>(n));
for (auto& q : queries) {
int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3];
mat[x1][y1]++;
if (x2 + 1 < n) {
mat[x2 + 1][y1]--;
}
if (y2 + 1 < n) {
mat[x1][y2 + 1]--;
}
if (x2 + 1 < n && y2 + 1 < n) {
mat[x2 + 1][y2 + 1]++;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i > 0) {
mat[i][j] += mat[i - 1][j];
}
if (j > 0) {
mat[i][j] += mat[i][j - 1];
}
if (i > 0 && j > 0) {
mat[i][j] -= mat[i - 1][j - 1];
}
}
}
return mat;
}
};