34 std::vector<int>
bit{};
41 inline int offset(
int x) {
return (x & (-x)); }
52 for (
int i = 0; i <
n; ++i) {
115 std::vector<int> arr = {1, 2, 3, 4, 5};
118 assert(fenwick_tree.
sum_range(0, 0) == 1);
119 assert(fenwick_tree.
sum_range(0, 1) == 3);
120 assert(fenwick_tree.
sum_range(0, 2) == 6);
121 assert(fenwick_tree.
sum_range(0, 3) == 10);
122 assert(fenwick_tree.
sum_range(0, 4) == 15);
124 fenwick_tree.
update(0, 6);
125 std::cout <<
"All tests have successfully passed!\n";
The class that initializes the Fenwick Tree.
int sum_range(int l, int r)
Returns the prefix sum in range from L to R.
void update(T id, T val)
Updates the value of an element in original array and accordingly updates the values in BIT array.
int sum(T id)
Returns the sum of elements in range from 0 to ID.
fenwick_tree(const std::vector< T > &arr)
Class Constructor.
int offset(int x)
Returns the highest power of two which is not more than x.
fenwick_tree(T x)
Class Constructor.
std::vector< int > bit
Array that represents Binary Indexed Tree.
size_t n
No. of elements present in input array.
static void tests()
Self-test implementations.