|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Segment tree over an arbitrary associative binary operation. More...
#include <tpl_segment_tree.H>
Public Types | |
| using | Item_Type = T |
Public Member Functions | |
| Gen_Segment_Tree (const size_t num, const T &init_val, const T &id, Op oper=Op()) | |
Construct a segment tree with num elements, all equal to init_val. | |
| Gen_Segment_Tree (std::initializer_list< T > il, const T &id, Op oper=Op()) | |
| Construct from an initializer list. | |
| Gen_Segment_Tree (const Array< T > &values, const T &id, Op oper=Op()) | |
| Construct from an Array<T>. | |
| Gen_Segment_Tree (const std::vector< T > &values, const T &id, Op oper=Op()) | |
| Construct from a std::vector<T>. | |
| Gen_Segment_Tree (const DynList< T > &values, const T &id, Op oper=Op()) | |
| Construct from a DynList<T>. | |
| Gen_Segment_Tree (const Gen_Segment_Tree &)=default | |
| Gen_Segment_Tree (Gen_Segment_Tree &&) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< Op >)=default | |
| Gen_Segment_Tree & | operator= (const Gen_Segment_Tree &)=default |
| Gen_Segment_Tree & | operator= (Gen_Segment_Tree &&) noexcept(std::is_nothrow_move_assignable_v< Array< T > > and std::is_nothrow_move_assignable_v< Op >)=default |
| void | update (const size_t i, const T &delta) |
Point update: a[i] = Op(a[i], delta). | |
| void | set (const size_t i, const T &val) |
Set a[i] = val. | |
| T | query (const size_t l, const size_t r) const |
Range query over [l, r]. | |
| T | get (const size_t i) const |
Retrieve the value a[i]. | |
| 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 (Gen_Segment_Tree &other) noexcept(noexcept(std::swap(std::declval< T & >(), std::declval< T & >())) and noexcept(std::swap(std::declval< Op & >(), std::declval< Op & >()))) |
Swap this tree with other in O(1). | |
Private Member Functions | |
| void | build (size_t node, const size_t start, const size_t end) |
| void | update_impl (size_t node, const size_t start, const size_t end, const size_t idx, const T &delta) |
| void | set_impl (size_t node, const size_t start, const size_t end, const size_t idx, const T &val) |
| T | query_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const |
| template<class Getter > | |
| void | fill_and_build (Getter getter) |
| template<class Getter > | |
| void | fill_leaves_recursive (size_t node, size_t start, size_t end, Getter &getter) |
| template<class AlephIt > | |
| void | fill_from_aleph_it (AlephIt it) |
Private Attributes | |
| Array< T > | tree |
| size_t | n = 0 |
| T | identity |
| Op | op |
Segment tree over an arbitrary associative binary operation.
This is the classical segment tree supporting:
It maintains an implicit binary tree backed by Array<T>, using 1-based node indexing and allocating roughly 4*n nodes.
Unlike Sparse Table, the operation need not be idempotent. Unlike Fenwick Tree, the operation need not be invertible. Only associativity is required.
update(i, delta) applies a[i] = op(a[i], delta). Use set(i, val) if you need assignment semantics.| T | element type. |
| Op | associative binary functor. |
Definition at line 148 of file tpl_segment_tree.H.
| using Aleph::Gen_Segment_Tree< T, Op >::Item_Type = T |
Definition at line 252 of file tpl_segment_tree.H.
|
inline |
Construct a segment tree with num elements, all equal to init_val.
| num | number of elements. |
| init_val | value for every position. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 262 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.
|
inline |
Construct from an initializer list.
| il | initializer list with the element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 283 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.
|
inline |
Construct from an Array<T>.
| values | source array with the initial element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 301 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().
|
inline |
Construct from a std::vector<T>.
| values | source vector with the initial element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 316 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().
|
inline |
Construct from a DynList<T>.
| values | source list with the initial element values. |
| id | identity element of the monoid. |
| oper | associative binary functor. |
Definition at line 331 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it(), Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::values().
|
default |
|
defaultnoexcept |
|
inlineprivate |
Definition at line 155 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::build(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::op, and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::build(), and Aleph::Gen_Segment_Tree< T, Op >::fill_and_build().
|
inlineprivate |
Definition at line 215 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::build(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive(), and Aleph::Gen_Segment_Tree< T, Op >::n.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), and Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it().
|
inlineprivate |
Definition at line 240 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::n.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree().
|
inlineprivate |
Definition at line 225 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive(), and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), and Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive().
|
inline |
Retrieve the value a[i].
Equivalent to query(i, i). O(log n).
| i | 0-based index. |
| std::out_of_range | if i >= size(). |
Definition at line 406 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::query_impl().
Referenced by scenario_1_el_arbol_del_tiempo(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Aleph::Gen_Segment_Tree< T, Op >::values().
|
inlineconstexprnoexcept |
True if the tree contains no elements.
Definition at line 418 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::n.
|
default |
|
defaultnoexcept |
|
inline |
Range query over [l, r].
Returns Op(a[l], a[l+1], ..., a[r]).
| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
Op over the range. | std::out_of_range | if l > r or r >= size(). |
Definition at line 389 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, l, Aleph::Gen_Segment_Tree< T, Op >::n, Aleph::Gen_Segment_Tree< T, Op >::query_impl(), and r.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 200 of file tpl_segment_tree.H.
References Aleph::and, Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::identity, l, Aleph::Gen_Segment_Tree< T, Op >::op, Aleph::Gen_Segment_Tree< T, Op >::query_impl(), r, and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::get(), Aleph::Gen_Segment_Tree< T, Op >::query(), and Aleph::Gen_Segment_Tree< T, Op >::query_impl().
Set a[i] = val.
| i | 0-based index. |
| val | new value for position i. |
| std::out_of_range | if i >= size(). |
Definition at line 372 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::set_impl().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 183 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::op, Aleph::Gen_Segment_Tree< T, Op >::set_impl(), and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::set(), and Aleph::Gen_Segment_Tree< T, Op >::set_impl().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 415 of file tpl_segment_tree.H.
References Aleph::Gen_Segment_Tree< T, Op >::n.
|
inlinenoexcept |
Swap this tree with other in O(1).
Definition at line 433 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::identity, Aleph::Gen_Segment_Tree< T, Op >::n, Aleph::Gen_Segment_Tree< T, Op >::op, and Aleph::Gen_Segment_Tree< T, Op >::tree.
Referenced by TEST().
Point update: a[i] = Op(a[i], delta).
| i | 0-based index. |
| delta | value to combine with a[i]. |
| std::out_of_range | if i >= size(). |
Definition at line 358 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Segment_Tree< T, Op >::n, and Aleph::Gen_Segment_Tree< T, Op >::update_impl().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 166 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::op, Aleph::Gen_Segment_Tree< T, Op >::tree, and Aleph::Gen_Segment_Tree< T, Op >::update_impl().
Referenced by Aleph::Gen_Segment_Tree< T, Op >::update(), and Aleph::Gen_Segment_Tree< T, Op >::update_impl().
|
inline |
Reconstruct all original values into an Array.
O(n log n).
Definition at line 424 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Segment_Tree< T, Op >::get(), and Aleph::Gen_Segment_Tree< T, Op >::n.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), and TEST().
Definition at line 152 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::query_impl(), and Aleph::Gen_Segment_Tree< T, Op >::swap().
|
private |
Definition at line 151 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::Gen_Segment_Tree(), Aleph::Gen_Segment_Tree< T, Op >::fill_and_build(), Aleph::Gen_Segment_Tree< T, Op >::fill_from_aleph_it(), Aleph::Gen_Segment_Tree< T, Op >::get(), Aleph::Gen_Segment_Tree< T, Op >::is_empty(), Aleph::Gen_Segment_Tree< T, Op >::query(), Aleph::Gen_Segment_Tree< T, Op >::set(), Aleph::Gen_Segment_Tree< T, Op >::size(), Aleph::Gen_Segment_Tree< T, Op >::swap(), Aleph::Gen_Segment_Tree< T, Op >::update(), and Aleph::Gen_Segment_Tree< T, Op >::values().
|
private |
Definition at line 150 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Segment_Tree< T, Op >::build(), Aleph::Gen_Segment_Tree< T, Op >::fill_leaves_recursive(), Aleph::Gen_Segment_Tree< T, Op >::query_impl(), Aleph::Gen_Segment_Tree< T, Op >::set_impl(), Aleph::Gen_Segment_Tree< T, Op >::swap(), and Aleph::Gen_Segment_Tree< T, Op >::update_impl().