-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwick.cpp
More file actions
40 lines (35 loc) · 724 Bytes
/
Copy pathFenwick.cpp
File metadata and controls
40 lines (35 loc) · 724 Bytes
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
template <typename T>
struct Fenwick {
int n;
vector<T> a;
Fenwick(int n = 0) {
init(n);
}
void init(int n) {
this->n = n;
a.assign(n + 1, T());
}
void add(int x, T v) {
for (int i = x; i <= n; i += i & -i) {
a[i] += v;
}
}
T query(int x) {
auto ans = T();
for (int i = x; i > 0; i -= i & -i) {
ans += a[i];
}
return ans;
}
//树状数组倍增
int kth(T k) {
int x = 0;
for (int i = 1 << __lg(n); i; i /= 2) {
if (x + i <= n && k >= a[x + i]) {
x += i;
k -= a[x];
}
}
return x;
}
};