forked from CodeChefLDCE/CodeChef_LDCE_CP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwick_tree.cpp
More file actions
43 lines (38 loc) · 764 Bytes
/
fenwick_tree.cpp
File metadata and controls
43 lines (38 loc) · 764 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
41
42
43
#include<bits/stdc++.h>
using namespace std;
struct FenwickTree {
// This tree works for function which are inversible like (xor, sum, sub, mult) etc.. not on (AND OR)
ll n;
// N is the size of FenwickTree
// Take note that N is not included in FenwicTree
vll bst;
void init(ll x) {
// X is the required size
n = x;
// Initialized with 0 change if you want...
bst.assign(n, 0);
}
ll func(ll k) {
// O(logN)
// Find the prefix value of function till k
ll s = 0;
while (k >= 1 && k < n) {
s += bst[k];
k -= (k & (-k));
}
return s;
}
void add(ll k, ll x) {
// O(logN)
// Add value x to index k
while (k < n) {
bst[k] += x;
k += (k & (-k));
}
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
return 0;
}