-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fenwick_Tree_Range_Update_Range_Query.cpp
89 lines (79 loc) · 1.78 KB
/
Fenwick_Tree_Range_Update_Range_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
// Range Update & Range Query, O(log(n))
template <typename T>
struct FenwickTree {
int n;
vector<T> f_mul, f_add;
FenwickTree() {}
FenwickTree(int _n) : n(_n) {
f_mul.assign(n, T{});
f_add.assign(n, T{});
}
FenwickTree(const vector<T> &v) {
n = v.size();
f_mul.assign(n, T{});
f_add.assign(n, T{});
for (int i = 0; i < n; i++) {
modify(i, i, v[i]);
}
}
void add(int x, T mul, T add) {
for (int i = x; i < n; i = i | (i + 1)) {
f_mul[i] += mul;
f_add[i] += add;
}
}
void modify(int l, int r, const T &v) {
assert(l >= 0 && l <= r && r < n);
add(l, v, -v * (l - 1));
add(r, -v, v * r);
}
T get(int x) {
T mul{}, add{};
for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
mul += f_mul[i];
add += f_add[i];
}
return mul * x + add;
}
T get(int l, int r) {
assert(l >= 0 && l <= r && r < n);
return get(r) - get(l - 1);
}
};
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
FenwickTree<int> fen(a);
while (q--) {
int op;
cin >> op;
if (op == 1) {
int l, r, v;
cin >> l >> r >> v;
l--, r--;
fen.modify(l, r, v);
}
else if (op == 2) {
int l, r;
cin >> l >> r;
l--, r--;
cout << fen.get(l, r) << '\n';
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}