-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwick_Tree_Range_Update_Point_Query.cpp
More file actions
89 lines (79 loc) · 1.7 KB
/
Fenwick_Tree_Range_Update_Point_Query.cpp
File metadata and controls
89 lines (79 loc) · 1.7 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
// Range Update & Point Query, O(log(n))
template <typename T>
struct FenwickTree {
int n;
vector<T> f;
FenwickTree() : n(0) {}
FenwickTree(int _n) : n(_n) {
f.assign(n, T{});
}
// O(n) Construction
FenwickTree(const vector<T>& v) {
n = v.size();
f.resize(n);
f[0] = v[0];
for (int i = 1; i < n; i++) {
f[i] = v[i] - v[i - 1];
}
for (int i = 0; i < n; i++) {
int r = i | (i + 1);
if (r < n) {
f[r] += f[i];
}
}
}
void add(int x, const T& v) {
for (int i = x; i < n; i = i | (i + 1)) {
f[i] += v;
}
}
void add(int l, int r, const T& v) {
assert(l >= 0 && l <= r && r < n);
add(l, v);
add(r + 1, -v);
}
T get(int x) const {
assert(x >= 0 && x < n);
T res{};
for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
res += f[i];
}
return res;
}
};
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.add(l, r, v);
} else if (op == 2) {
int x;
cin >> x;
x--;
cout << fen.get(x) << '\n';
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}