-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2dfenwick_single_query.cpp
68 lines (60 loc) · 1.48 KB
/
2dfenwick_single_query.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
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
#include <bits/stdc++.h>
using namespace std;
int n, m;
template <typename T>
struct fenwick {
template <typename T> using vec = vector<T>;
vec<vec<T> > d;
int n, m;
template <typename tmp>
tmp lowbit(tmp i) {return i & (-i);}
fenwick(int N, int M) {
n = N, m = M;
d = vec<vec<T> >(n + 5);
for(vec<T>& x : d) x = vec<T>(m + 5);
}
T query(int x, int y) {
T ans = 0;
for(int i = x;i;i-=lowbit(i)) {
for(int j = y;j;j-=lowbit(j)) {
ans += d[i][j];
}
}
return ans;
}
void update(int x, int y, T val) {
for(int i = x; i < d.size(); i += lowbit(i)) {
for(int j = y; j < d[i].size(); j+= lowbit(j)) {
d[i][j] += val;
}
}
}
void update(int a, int b, int c, int d, T val) {
update(a, b, val);
update(c + 1, b, -val);
update(a, d + 1, -val);
update(c + 1, d + 1, val);
}
};
int main() {
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
char opt;
cin>>opt>>n>>m;
fenwick<int> st(n, m);
while(cin>>opt) {
int a, b, c, d;
cin>>a>>b>>c>>d;
if(opt == 'L') {
int x; cin>>x;
st.update(a, b, c, d, x);
} else cout<<st.query(a, b)<<'\n';
}
for(int i = 1;i<=n;++i) {
for(int j = 1;j<=m;++j) {
cout<<st.query(i, j)<<' ';
}
cout<<'\n';
}
return 0;
}