-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwick_Tree_Point_Update_Range_Query.cpp
More file actions
101 lines (91 loc) · 2.13 KB
/
Fenwick_Tree_Point_Update_Range_Query.cpp
File metadata and controls
101 lines (91 loc) · 2.13 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
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
using namespace std;
// Point Update & Range 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.assign(v.begin(), v.end());;
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) {
assert(x >= 0 && x < n);
for (int i = x; i < n; i = i | (i + 1)) {
f[i] += v;
}
}
T get(int x) const {
T res{};
for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
res += f[i];
}
return res;
}
T get(int l, int r) const {
assert(l >= 0 && l <= r && r < n);
return get(r) - get(l - 1);
}
// Lower bound on prefix sum array (every element must be non-negative)
int select(const T& k) const {
T sum{};
int x = 0, pw = __bit_floor(n);
for (int len = pw; len > 0; len >>= 1) {
if (x + len <= n && sum + f[x + len - 1] < k) {
sum += f[x + len - 1];
x += len;
}
}
assert(x >= 0 && x <= n);
return x;
}
};
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 x, v;
cin >> x >> v;
x--;
fen.add(x, v);
} else if (op == 2) {
int l, r;
cin >> l >> r;
l--, r--;
cout << fen.get(l, r) << '\n';
} else if (op == 3) {
int k;
cin >> k;
cout << fen.select(k) << '\n';
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}