-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_2d_prefix_sum.cpp
More file actions
58 lines (40 loc) · 999 Bytes
/
3_2d_prefix_sum.cpp
File metadata and controls
58 lines (40 loc) · 999 Bytes
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
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
vector<vector<int>> pre(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
pre[i][j] = a[i][j];
if (i > 0) {
pre[i][j] += pre[i - 1][j];
}
if (j > 0) {
pre[i][j] += pre[i][j - 1];
}
if (i > 0 && j > 0) {
pre[i][j] -= pre[i - 1][j - 1];
}
}
}
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int ans = pre[x2][y2];
if (x1 > 0) {
ans -= pre[x1 - 1][y2];
}
if (y1 > 0) {
ans -= pre[x2][y1 - 1];
}
if (x1 > 0 && y1 > 0) {
ans += pre[x1 - 1][y1 - 1];
}
cout << ans << "\n";
}