|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Fenwick tree supporting range updates and range queries. More...
#include <tpl_fenwick_tree.H>
Public Types | |
| using | Item_Type = T |
Public Member Functions | |
| Range_Fenwick_Tree (const size_t num) | |
Construct a tree with num elements, all zero. | |
| Range_Fenwick_Tree (std::initializer_list< T > il) | |
| Construct from an initializer list in O(n) time. | |
| Range_Fenwick_Tree (const Array< T > &values) | |
| Construct from an Array<T> in O(n) time. | |
| Range_Fenwick_Tree (const Range_Fenwick_Tree &other)=default | |
| Copy another tree: its two inner trees and its size. | |
| Range_Fenwick_Tree (Range_Fenwick_Tree &&other) noexcept | |
| Take the elements of another tree, which is left empty. | |
| Range_Fenwick_Tree & | operator= (const Range_Fenwick_Tree &other)=default |
| Copy another tree into this one: its two inner trees and its size. | |
| Range_Fenwick_Tree & | operator= (Range_Fenwick_Tree &&other) noexcept |
| Exchange this tree with another, which is left with the previous elements of this one. | |
| void | update (size_t l, const size_t r, const T &delta) |
Range update: add delta to every a[i] with l <= i <= r. | |
| void | point_update (const size_t i, const T &delta) |
Point update: add delta to a[i]. | |
| T | prefix (const size_t i) const |
Prefix query: sum of a[0..i]. | |
| T | query (const size_t l, const size_t r) const |
Range query: sum of a[l..r]. | |
| T | get (const size_t i) const |
Retrieve the logical value a[i]. | |
| void | set (const size_t i, const T &value) |
Set a[i] = value. | |
| constexpr size_t | size () const noexcept |
| Number of logical elements. | |
| constexpr bool | is_empty () const noexcept |
| True if the tree contains no elements. | |
| Array< T > | values () const |
| Reconstruct all original values into an Array. | |
| void | swap (Range_Fenwick_Tree &other) noexcept |
Swap this tree with other in O(1). | |
Private Member Functions | |
| T | prefix_sum (size_t i) const |
| Internal: prefix sum P(i) = (i+1)*B1.prefix(i) - B2.prefix(i) | |
| void | build_from_diffs (const Array< T > &d) |
Rebuild b1 and b2 from a difference array stored in d. | |
Private Attributes | |
| Gen_Fenwick_Tree< T > | b1 |
| Gen_Fenwick_Tree< T > | b2 |
| size_t | n = 0 |
Fenwick tree supporting range updates and range queries.
Maintains an implicit array a[0..n-1] and supports adding a constant to every element in a contiguous range, as well as querying the sum over any contiguous range, both in O(log n).
Internally, two classic Fenwick trees (B1, B2) are kept so that the prefix sum can be recovered as
\[ P(i) = (i+1)\,\text{B1.prefix}(i) - \text{B2.prefix}(i) \]
This class is restricted to signed arithmetic types (it uses multiplication, subtraction, and negative deltas directly).
| T | signed arithmetic element type. |
| Operation | Time |
|---|---|
| Construction | O(n) |
| update (range) | O(log n) |
| prefix | O(log n) |
| query | O(log n) |
| get / set | O(log n) |
| Space | O(n) |
Definition at line 554 of file tpl_fenwick_tree.H.
| using Aleph::Range_Fenwick_Tree< T >::Item_Type = T |
Definition at line 581 of file tpl_fenwick_tree.H.
|
inline |
Construct a tree with num elements, all zero.
Definition at line 584 of file tpl_fenwick_tree.H.
|
inline |
Construct from an initializer list in O(n) time.
Definition at line 598 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::build_from_diffs(), Aleph::Array< T >::create(), and Aleph::Range_Fenwick_Tree< T >::n.
|
inline |
Construct from an Array<T> in O(n) time.
| values | source array with the initial element values. |
Definition at line 620 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::build_from_diffs(), Aleph::Array< T >::create(), Aleph::Range_Fenwick_Tree< T >::n, and Aleph::Range_Fenwick_Tree< T >::values().
|
default |
Copy another tree: its two inner trees and its size.
| [in] | other | tree to copy. |
|
inlinenoexcept |
Take the elements of another tree, which is left empty.
The implicit move took the inner trees but copied the size: the moved-from tree then indexed trees that it no longer had.
| [in,out] | other | tree to move from. |
Definition at line 645 of file tpl_fenwick_tree.H.
|
inlineprivate |
Rebuild b1 and b2 from a difference array stored in d.
Definition at line 567 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::b1, Aleph::Range_Fenwick_Tree< T >::b2, Aleph::Array< T >::create(), and Aleph::Range_Fenwick_Tree< T >::n.
Referenced by Aleph::Range_Fenwick_Tree< T >::Range_Fenwick_Tree(), and Aleph::Range_Fenwick_Tree< T >::Range_Fenwick_Tree().
|
inline |
Retrieve the logical value a[i].
Equivalent to query(i, i). O(log n).
Definition at line 737 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::query().
Referenced by scenario_betting_promos(), Aleph::Range_Fenwick_Tree< T >::set(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Aleph::Range_Fenwick_Tree< T >::values().
|
inlineconstexprnoexcept |
True if the tree contains no elements.
Definition at line 755 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::n.
Referenced by Moved_From_Case< Range_Fenwick_Tree< int > >::is_empty(), TEST(), and Moved_From_Case< Range_Fenwick_Tree< int > >::use().
|
default |
Copy another tree into this one: its two inner trees and its size.
| [in] | other | tree to copy. |
|
inlinenoexcept |
Exchange this tree with another, which is left with the previous elements of this one.
| [in,out] | other | tree to move from. |
Definition at line 661 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::swap().
|
inline |
Point update: add delta to a[i].
Convenience shorthand for update(i, i, delta).
Definition at line 697 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::update().
Referenced by Aleph::Range_Fenwick_Tree< T >::set(), and TEST().
|
inline |
Prefix query: sum of a[0..i].
| i | 0-based index. |
| std::out_of_range | if i >= size(). |
Definition at line 708 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if, Aleph::Range_Fenwick_Tree< T >::n, and Aleph::Range_Fenwick_Tree< T >::prefix_sum().
Referenced by scenario_betting_promos(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Internal: prefix sum P(i) = (i+1)*B1.prefix(i) - B2.prefix(i)
Definition at line 561 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::b1, and Aleph::Range_Fenwick_Tree< T >::b2.
Referenced by Aleph::Range_Fenwick_Tree< T >::prefix(), and Aleph::Range_Fenwick_Tree< T >::query().
|
inline |
Range query: sum of a[l..r].
| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
[l, r]. | std::out_of_range | if l > r or r >= size(). |
Definition at line 723 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if, l, Aleph::Range_Fenwick_Tree< T >::n, Aleph::Range_Fenwick_Tree< T >::prefix_sum(), and r.
Referenced by Aleph::Range_Fenwick_Tree< T >::get(), scenario_betting_promos(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Range_Fenwick_Tree< int > >::use().
|
inline |
Set a[i] = value.
Computes the required delta and calls point_update(). O(log n).
Definition at line 746 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::get(), Aleph::Range_Fenwick_Tree< T >::point_update(), and value.
Referenced by TEST().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 752 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::n.
Referenced by TEST(), TEST(), TEST(), and Moved_From_Case< Range_Fenwick_Tree< int > >::use().
|
inlinenoexcept |
Swap this tree with other in O(1).
Definition at line 770 of file tpl_fenwick_tree.H.
References Aleph::Range_Fenwick_Tree< T >::b1, Aleph::Range_Fenwick_Tree< T >::b2, and Aleph::Range_Fenwick_Tree< T >::n.
Referenced by Aleph::Range_Fenwick_Tree< T >::operator=(), and TEST().
|
inline |
Range update: add delta to every a[i] with l <= i <= r.
| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
| delta | value to add to each element in [l, r]. |
| std::out_of_range | if l > r or r >= size(). |
Definition at line 675 of file tpl_fenwick_tree.H.
References ah_out_of_range_error_if, Aleph::Range_Fenwick_Tree< T >::b1, Aleph::Range_Fenwick_Tree< T >::b2, l, Aleph::Range_Fenwick_Tree< T >::n, and r.
Referenced by Moved_From_Case< Range_Fenwick_Tree< int > >::make(), Aleph::Range_Fenwick_Tree< T >::point_update(), scenario_betting_promos(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Range_Fenwick_Tree< int > >::use().
|
inline |
Reconstruct all original values into an Array.
O(n log n).
Definition at line 761 of file tpl_fenwick_tree.H.
References Aleph::Array< T >::create(), Aleph::Range_Fenwick_Tree< T >::get(), and Aleph::Range_Fenwick_Tree< T >::n.
Referenced by Aleph::Range_Fenwick_Tree< T >::Range_Fenwick_Tree(), and TEST().
|
private |
Definition at line 556 of file tpl_fenwick_tree.H.
Referenced by Aleph::Range_Fenwick_Tree< T >::build_from_diffs(), Aleph::Range_Fenwick_Tree< T >::prefix_sum(), Aleph::Range_Fenwick_Tree< T >::swap(), and Aleph::Range_Fenwick_Tree< T >::update().
|
private |
Definition at line 557 of file tpl_fenwick_tree.H.
Referenced by Aleph::Range_Fenwick_Tree< T >::build_from_diffs(), Aleph::Range_Fenwick_Tree< T >::prefix_sum(), Aleph::Range_Fenwick_Tree< T >::swap(), and Aleph::Range_Fenwick_Tree< T >::update().
|
private |
Definition at line 558 of file tpl_fenwick_tree.H.
Referenced by Aleph::Range_Fenwick_Tree< T >::Range_Fenwick_Tree(), Aleph::Range_Fenwick_Tree< T >::Range_Fenwick_Tree(), Aleph::Range_Fenwick_Tree< T >::build_from_diffs(), Aleph::Range_Fenwick_Tree< T >::is_empty(), Aleph::Range_Fenwick_Tree< T >::prefix(), Aleph::Range_Fenwick_Tree< T >::query(), Aleph::Range_Fenwick_Tree< T >::size(), Aleph::Range_Fenwick_Tree< T >::swap(), Aleph::Range_Fenwick_Tree< T >::update(), and Aleph::Range_Fenwick_Tree< T >::values().