-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwick_tree_range_update_point_query_2d.cpp
More file actions
74 lines (62 loc) · 1.46 KB
/
fenwick_tree_range_update_point_query_2d.cpp
File metadata and controls
74 lines (62 loc) · 1.46 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// BOJ 15646 농부 후안은 바리스타입니다
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
struct FenwickTree2D {
int n, m;
vector<vector<ll>> tree;
FenwickTree2D(int n, int m) : n(n), m(m) { tree.resize(n + 1, vector<ll>(m + 1)); }
void range(int i, int j, ll k) {
int ii = i;
while (j > 0) {
while (i > 0) {
tree[i][j] += k;
i -= (i & -i);
}
j -= (j & -j);
i = ii;
}
}
ll point(int i, int j) {
ll ret = 0;
int ii = i;
while (j <= m) {
while (i <= n) {
ret += tree[i][j];
i += (i & -i);
}
j += (j & -j);
i = ii;
}
return ret;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
FenwickTree2D ft(n, m);
while (q--) {
int op;
cin >> op;
if (op == 1) {
int x, y, z, w, k;
cin >> x >> y >> z >> w >> k;
ft.range(z, w, k);
ft.range(x - 1, w, -k);
ft.range(z, y - 1, -k);
ft.range(x - 1, y - 1, k);
} else if (op == 2) {
int x, y;
cin >> x >> y;
cout << ft.point(x, y) << '\n';
}
}
}