A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d6/d2e/fenwick__tree_8cpp_source.html below:

TheAlgorithms/C++: range_queries/fenwick_tree.cpp Source File

Go to the documentation of this file. 34

std::vector<int>

bit

{};

41 inline int offset

(

int

x) {

return

(x & (-x)); }

49 template

<

typename

T>

51 bit

.assign(

n

+ 1, 0);

52 for

(

int

i = 0; i <

n

; ++i) {

63 template

<

typename

T>

74 template

<

typename

T>

89 template

<

typename

T>

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.

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