67# ifndef TPL_FENWICK_TREE_H
68# define TPL_FENWICK_TREE_H
73# include <initializer_list>
74# include <type_traits>
91 std::is_arithmetic_v<T> &&
92 std::totally_ordered<T> &&
93 std::is_signed_v<T> &&
94 !std::same_as<T, bool>;
102 template <
typename F,
typename T>
135 template <
typename T,
153 static constexpr size_t lowbit(
size_t x)
noexcept
162 for (
size_t i = 1; i <= n; ++i)
163 if (
const size_t parent = i + lowbit(i); parent <= n)
164 tree(parent) = plus_op(tree(parent), tree(i));
171 for (
size_t i = 0; i < n; ++i)
172 tree(i + 1) = getter(i);
177 template <
class StdIt>
181 for (; first != last; ++first)
187 template <
class AlephIt>
191 for (; it.has_curr(); it.next_ne())
192 tree(i++) = it.get_curr();
209 : tree(num + 1,
T()), n(num),
210 plus_op(pop), minus_op(mop)
225 : tree(il.
size() + 1,
T()), n(il.
size()),
226 plus_op(pop), minus_op(mop)
228 fill_from_std_iter(il.begin(), il.end());
239 : tree(values.
size() + 1,
T()), n(values.
size()),
240 plus_op(pop), minus_op(mop)
242 fill_from_indexed([&values](
size_t i) {
return values(i); });
256 : tree(values.
size() + 1,
T()), n(values.
size()),
257 plus_op(pop), minus_op(mop)
259 fill_from_indexed([&values](
size_t i) {
return values[i]; });
270 : tree(values.
size() + 1,
T()), n(values.
size()),
271 plus_op(pop), minus_op(mop)
273 fill_from_aleph_it(values.
get_it());
291 noexcept(std::is_nothrow_move_constructible_v<Plus>
292 and std::is_nothrow_move_constructible_v<Minus>)
293 : tree(
std::move(other.tree)), n(
std::exchange(other.n, 0)),
294 plus_op(
std::move(other.plus_op)), minus_op(
std::move(other.minus_op))
324 <<
"Gen_Fenwick_Tree::update: index " << i <<
" >= size " << n;
326 for (++i; i <= n; i += i & (-i))
327 tree(i) = plus_op(tree(i), delta);
339 <<
"Gen_Fenwick_Tree::prefix: index " << i <<
" >= size " << n;
342 for (++i; i > 0; i -= i & (-i))
343 s = plus_op(s, tree(i));
357 <<
"Gen_Fenwick_Tree::query: r=" <<
r <<
" >= n=" << n;
359 <<
"Gen_Fenwick_Tree::query: l=" <<
l <<
" > r=" <<
r;
386 update(i, minus_op(
value, get(i)));
390 [[nodiscard]]
constexpr size_t size() const noexcept {
return n; }
393 [[nodiscard]]
constexpr bool is_empty() const noexcept {
return n == 0; }
402 for (
size_t i = 0; i < n; ++i)
417 noexcept(std::is_nothrow_swappable_v<Plus>
and std::is_nothrow_swappable_v<Minus>)
421 swap_operations(other);
422 tree.
swap(other.tree);
423 std::swap(n, other.n);
467 template <FenwickArithmetic T>
491 const size_t nn = this->
n;
497 size_t bit_mask = std::bit_floor(nn);
509 return pos < nn ? pos : nn;
553 template <FenwickArithmetic T>
563 return static_cast<T>(i + 1) *
b1.prefix(i) -
b2.prefix(i);
571 for (
size_t i = 1; i <
n; ++i)
572 d2(i) = d(i) *
static_cast<T>(i);
585 :
b1(num),
b2(num),
n(num)
605 auto it = il.begin();
608 for (
size_t i = 1; i <
n; ++i, ++it)
628 for (
size_t i = 1; i <
n; ++i)
646 :
b1(std::move(other.b1)),
b2(std::move(other.b2)),
n(std::exchange(other.n, 0))
678 <<
"Range_Fenwick_Tree::update: l=" <<
l <<
" > r=" <<
r;
680 <<
"Range_Fenwick_Tree::update: r=" <<
r <<
" >= size " <<
n;
683 b2.update(
l, delta *
static_cast<T>(
l));
688 b1.update(
r + 1, neg);
689 b2.update(
r + 1, neg *
static_cast<T>(
r + 1));
711 <<
"Range_Fenwick_Tree::prefix: index " << i <<
" >= size " <<
n;
726 <<
"Range_Fenwick_Tree::query: r=" <<
r <<
" >= n=" <<
n;
728 <<
"Range_Fenwick_Tree::query: l=" <<
l <<
" > r=" <<
r;
752 [[nodiscard]]
constexpr size_t size() const noexcept {
return n; }
755 [[nodiscard]]
constexpr bool is_empty() const noexcept {
return n == 0; }
764 for (
size_t i = 0; i <
n; ++i)
774 std::swap(
n, other.n);
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Standard functor implementations and comparison objects.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
void swap(Array &s) noexcept
Swap this with s
static Array create(const size_t n)
Create an array with n logical elements.
Doubly-linked list (defined in tpl_dynList.H).
Fenwick tree over an arbitrary abelian group.
void fill_from_std_iter(StdIt first, StdIt last)
Fill the internal storage from a standard iterator range and build.
Array< T > values() const
Reconstruct all original values into an Array.
T query(const size_t l, const size_t r) const
Range query: Plus(a[l], a[l+1], ..., a[r]).
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
Gen_Fenwick_Tree(const DynList< T > &values, Plus pop=Plus(), Minus mop=Minus())
Construct from a DynList<T> in O(n) time.
void set(const size_t i, const T &value)
Set a[i] = value.
void fill_from_aleph_it(AlephIt it)
Fill the internal storage from an Aleph-style iterator (get_it()) and build.
void swap_operations(Gen_Fenwick_Tree &other)
T prefix(size_t i) const
Prefix query: Plus(a[0], a[1], ..., a[i]).
void update(size_t i, const T &delta)
Point update: a[i] = Plus(a[i], delta).
T get(const size_t i) const
Retrieve the logical value a[i].
void build()
O(n) bottom-up construction of the tree array from raw values already placed in tree[1....
Gen_Fenwick_Tree(const size_t num, Plus pop=Plus(), Minus mop=Minus())
Construct a tree with num elements, all equal to the identity T().
Gen_Fenwick_Tree(Gen_Fenwick_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Plus > and std::is_nothrow_move_constructible_v< Minus >)
Take the elements of another tree, which is left empty.
static constexpr size_t lowbit(size_t x) noexcept
Return the least significant set bit of x (as a mask).
void fill_from_indexed(F getter)
Fill the internal storage from a 0-based indexed getter and build.
T Item_Type
The type of element stored in the tree.
Gen_Fenwick_Tree(const Array< T > &values, Plus pop=Plus(), Minus mop=Minus())
Construct from an Array<T> in O(n) time.
Gen_Fenwick_Tree(std::initializer_list< T > il, Plus pop=Plus(), Minus mop=Minus())
Construct from an initializer list in O(n) time.
Gen_Fenwick_Tree & operator=(Gen_Fenwick_Tree &&other) noexcept(noexcept(swap(other)))
Exchange this tree with another, which is left with the previous elements of this one.
Gen_Fenwick_Tree(const std::vector< T > &values, Plus pop=Plus(), Minus mop=Minus())
Construct from a std::vector<T> in O(n) time.
void swap(Gen_Fenwick_Tree &other) noexcept(std::is_nothrow_swappable_v< Plus > and std::is_nothrow_swappable_v< Minus >)
Swap this tree with other in O(1).
Gen_Fenwick_Tree(const Gen_Fenwick_Tree &other)=default
Copy another tree: its array, its size and its operations.
Gen_Fenwick_Tree & operator=(const Gen_Fenwick_Tree &other)=default
Copy another tree into this one: its array, its size and its operations.
constexpr size_t size() const noexcept
Number of logical elements.
Fenwick tree supporting range updates and range queries.
void build_from_diffs(const Array< T > &d)
Rebuild b1 and b2 from a difference array stored in d.
void point_update(const size_t i, const T &delta)
Point update: add delta to a[i].
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(const size_t num)
Construct a tree with num elements, all zero.
void update(size_t l, const size_t r, const T &delta)
Range update: add delta to every a[i] with l <= i <= r.
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
void swap(Range_Fenwick_Tree &other) noexcept
Swap this tree with other in O(1).
T get(const size_t i) const
Retrieve the logical value a[i].
Range_Fenwick_Tree(Range_Fenwick_Tree &&other) noexcept
Take the elements of another tree, which is left empty.
void set(const size_t i, const T &value)
Set a[i] = value.
Range_Fenwick_Tree(const Array< T > &values)
Construct from an Array<T> in O(n) time.
Range_Fenwick_Tree(std::initializer_list< T > il)
Construct from an initializer list in O(n) time.
Array< T > values() const
Reconstruct all original values into an Array.
T prefix(const size_t i) const
Prefix query: sum of a[0..i].
constexpr size_t size() const noexcept
Number of logical elements.
Range_Fenwick_Tree & operator=(Range_Fenwick_Tree &&other) noexcept
Exchange this tree with another, which is left with the previous elements of this one.
Range_Fenwick_Tree(const Range_Fenwick_Tree &other)=default
Copy another tree: its two inner trees and its size.
T query(const size_t l, const size_t r) const
Range query: sum of a[l..r].
T prefix_sum(size_t i) const
Internal: prefix sum P(i) = (i+1)*B1.prefix(i) - B2.prefix(i)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
A binary functor closed over T.
Arithmetic domain accepted by Fenwick specializations.
Binary operation compatible with Fenwick tree group functors.
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::decay_t< typename HeadC::Item_Type > T
static void prefix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
Fenwick tree for arithmetic types with find_kth support.
size_t find_kth(T k) const
Find the smallest 0-based index i such that prefix(i) >= k.
Dynamic array container with automatic resizing.
Alias for htlist.H (DynList implementation).