std::vector<int>
bit{};
41 inline int offset(
intx) {
return(x & (-x)); }
49 template<
typenameT>
51 bit.assign(
n+ 1, 0);
52 for(
inti = 0; i <
n; ++i) {
63 template<
typenameT>
74 template<
typenameT>
89 template<
typenameT>
115std::vector<int> arr = {1, 2, 3, 4, 5};
118assert(fenwick_tree.
sum_range(0, 0) == 1);
119assert(fenwick_tree.
sum_range(0, 1) == 3);
120assert(fenwick_tree.
sum_range(0, 2) == 6);
121assert(fenwick_tree.
sum_range(0, 3) == 10);
122assert(fenwick_tree.
sum_range(0, 4) == 15);
124fenwick_tree.
update(0, 6);
125std::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.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4