|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Lazy segment tree with range update and range query. More...
#include <tpl_segment_tree.H>
Public Types | |
| using | value_type = typename Policy::value_type |
| using | lazy_type = typename Policy::lazy_type |
| using | Item_Type = value_type |
Public Member Functions | |
| Gen_Lazy_Segment_Tree (const size_t num, const value_type &init_val=value_type(), Policy p=Policy()) | |
Construct with num elements, all equal to init_val. | |
| Gen_Lazy_Segment_Tree (std::initializer_list< value_type > il, Policy p=Policy()) | |
| Construct from an initializer list. | |
| Gen_Lazy_Segment_Tree (const Array< value_type > &values, Policy p=Policy()) | |
| Construct from an Array<value_type>. | |
| Gen_Lazy_Segment_Tree (const std::vector< value_type > &values, Policy p=Policy()) | |
| Construct from a std::vector. | |
| Gen_Lazy_Segment_Tree (const DynList< value_type > &values, Policy p=Policy()) | |
| Construct from a DynList. | |
| Gen_Lazy_Segment_Tree (const Gen_Lazy_Segment_Tree &)=default | |
| Gen_Lazy_Segment_Tree (Gen_Lazy_Segment_Tree &&other) noexcept(std::is_nothrow_move_constructible_v< Array< value_type > > and std::is_nothrow_move_constructible_v< Array< lazy_type > > and std::is_nothrow_move_constructible_v< Policy >) | |
| Take the elements of another tree, which is left empty. | |
| Gen_Lazy_Segment_Tree & | operator= (const Gen_Lazy_Segment_Tree &)=default |
| Gen_Lazy_Segment_Tree & | operator= (Gen_Lazy_Segment_Tree &&other) noexcept(noexcept(swap(other))) |
| Exchange this tree with another, which is left with the previous elements of this one. | |
| void | update (const size_t l, const size_t r, const lazy_type &val) |
Range update: apply val to every a[i] with l <= i <= r. | |
| void | point_update (const size_t i, const lazy_type &delta) |
Point update: apply delta to a[i]. | |
| void | set (const size_t i, const value_type &val) |
Set a[i] = val. | |
| value_type | query (const size_t l, const size_t r) const |
Range query over [l, r]. | |
| value_type | 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< value_type > | values () const |
| Reconstruct all original values into an Array. | |
| void | swap (Gen_Lazy_Segment_Tree &other) noexcept(std::is_nothrow_swappable_v< Policy >) |
Swap this tree with other in O(1). | |
Private Member Functions | |
| void | push_down (size_t node, const size_t start, const size_t end) const |
| 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 l, const size_t r, const lazy_type &val) |
| value_type | query_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const |
| void | set_impl (size_t node, const size_t start, const size_t end, const size_t idx, const value_type &val) |
| 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) |
| size_t | alloc_size () const |
Private Attributes | |
| Array< value_type > | tree |
| Array< lazy_type > | lazy |
| size_t | n = 0 |
| Policy | pol |
Lazy segment tree with range update and range query.
Parameterised by a Policy that bundles value type, lazy type, and the combine/apply/compose functions. Supports range updates and range queries in O(log n) with lazy propagation.
query() is declared const in the public API, but it may still trigger propagation (push_down) by mutating internal mutable storage. This preserves logical constness for callers while allowing on-demand evaluation of pending updates.| Policy | must satisfy SegmentTreePolicy. |
Definition at line 971 of file tpl_segment_tree.H.
| using Aleph::Gen_Lazy_Segment_Tree< Policy >::Item_Type = value_type |
Definition at line 976 of file tpl_segment_tree.H.
| using Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy_type = typename Policy::lazy_type |
Definition at line 975 of file tpl_segment_tree.H.
| using Aleph::Gen_Lazy_Segment_Tree< Policy >::value_type = typename Policy::value_type |
Definition at line 974 of file tpl_segment_tree.H.
|
inline |
Construct with num elements, all equal to init_val.
Definition at line 1112 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
|
inline |
Construct from an initializer list.
Definition at line 1124 of file tpl_segment_tree.H.
References StlAlephIterator< SetName >::begin(), Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
|
inline |
Construct from an Array<value_type>.
Definition at line 1138 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build(), Aleph::Gen_Lazy_Segment_Tree< Policy >::n, and Aleph::Gen_Lazy_Segment_Tree< Policy >::values().
|
inline |
Construct from a std::vector.
Definition at line 1149 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build(), Aleph::Gen_Lazy_Segment_Tree< Policy >::n, and Aleph::Gen_Lazy_Segment_Tree< Policy >::values().
|
inline |
Construct from a DynList.
Definition at line 1160 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_from_aleph_it(), LocateFunctions< Container, Type >::get_it(), Aleph::Gen_Lazy_Segment_Tree< Policy >::n, and Aleph::Gen_Lazy_Segment_Tree< Policy >::values().
|
default |
|
inlinenoexcept |
Take the elements of another tree, which is left empty.
The implicit move took the arrays but copied the size: the moved-from tree then indexed arrays that it no longer had.
| [in,out] | other | tree to move from. |
Definition at line 1179 of file tpl_segment_tree.H.
|
inlineprivate |
Definition at line 1105 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
|
inlineprivate |
Definition at line 1002 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::build(), Aleph::Gen_Lazy_Segment_Tree< Policy >::pol, and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::build(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build().
|
inlineprivate |
Definition at line 1072 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::build(), Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_leaves_recursive(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_from_aleph_it().
|
inlineprivate |
Definition at line 1094 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree().
|
inlineprivate |
Definition at line 1079 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_leaves_recursive(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_leaves_recursive().
|
inline |
Retrieve the value a[i].
Equivalent to query(i, i). O(log n).
Definition at line 1269 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::query().
Referenced by scenario_2_ajustes_salariales(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::values().
|
inlineconstexprnoexcept |
True if the tree contains no elements.
Definition at line 1278 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
Referenced by TEST().
|
default |
|
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 1194 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::swap().
|
inline |
Point update: apply delta to a[i].
Convenience shorthand for update(i, i, delta).
Definition at line 1223 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::update().
Referenced by TEST().
|
inlineprivate |
Definition at line 984 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy, Aleph::Gen_Lazy_Segment_Tree< Policy >::pol, and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl(), Aleph::Gen_Lazy_Segment_Tree< Policy >::set_impl(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::update_impl().
|
inline |
Range query over [l, r].
Lazy tags are propagated internally via mutable storage, so this method is const and safe to call through const references.
| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
| std::out_of_range | if l > r or r >= size(). |
Definition at line 1255 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, l, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl(), and r.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::get(), scenario_2_ajustes_salariales(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 1034 of file tpl_segment_tree.H.
References Aleph::and, l, Aleph::Gen_Lazy_Segment_Tree< Policy >::pol, Aleph::Gen_Lazy_Segment_Tree< Policy >::push_down(), Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl(), r, and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::query(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl().
|
inline |
Set a[i] = val.
O(log n).
| i | 0-based index. |
| val | new value for position i. |
| std::out_of_range | if i >= size(). |
Definition at line 1236 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, and Aleph::Gen_Lazy_Segment_Tree< Policy >::set_impl().
|
inlineprivate |
Definition at line 1051 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy, Aleph::Gen_Lazy_Segment_Tree< Policy >::pol, Aleph::Gen_Lazy_Segment_Tree< Policy >::push_down(), Aleph::Gen_Lazy_Segment_Tree< Policy >::set_impl(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::set(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::set_impl().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 1275 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
Referenced by scenario_2_ajustes_salariales(), TEST(), and TEST().
|
inlinenoexcept |
Swap this tree with other in O(1).
| [in,out] | other | tree to exchange with. |
| what | swapping the policies throws. They are exchanged before the arrays, so that, if that swap throws, nothing else is exchanged, and both trees are left as they were if it left its arguments as they were. |
Definition at line 1299 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, Aleph::Gen_Lazy_Segment_Tree< Policy >::pol, Aleph::Array< T >::swap(), Aleph::Gen_Lazy_Segment_Tree< Policy >::swap(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::operator=(), Aleph::Gen_Lazy_Segment_Tree< Policy >::swap(), and TEST().
|
inline |
Range update: apply val to every a[i] with l <= i <= r.
| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
| val | lazy tag to apply. |
| std::out_of_range | if l > r or r >= size(). |
Definition at line 1209 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, l, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, r, and Aleph::Gen_Lazy_Segment_Tree< Policy >::update_impl().
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::point_update(), scenario_2_ajustes_salariales(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 1013 of file tpl_segment_tree.H.
References Aleph::and, l, Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy, Aleph::Gen_Lazy_Segment_Tree< Policy >::pol, Aleph::Gen_Lazy_Segment_Tree< Policy >::push_down(), r, Aleph::Gen_Lazy_Segment_Tree< Policy >::tree, and Aleph::Gen_Lazy_Segment_Tree< Policy >::update_impl().
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::update(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::update_impl().
|
inline |
Reconstruct all original values into an Array.
O(n log n).
Definition at line 1284 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::Gen_Lazy_Segment_Tree< Policy >::get(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree().
|
mutableprivate |
|
private |
Definition at line 981 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree(), Aleph::Gen_Lazy_Segment_Tree< Policy >::alloc_size(), Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_and_build(), Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_from_aleph_it(), Aleph::Gen_Lazy_Segment_Tree< Policy >::is_empty(), Aleph::Gen_Lazy_Segment_Tree< Policy >::query(), Aleph::Gen_Lazy_Segment_Tree< Policy >::set(), Aleph::Gen_Lazy_Segment_Tree< Policy >::size(), Aleph::Gen_Lazy_Segment_Tree< Policy >::swap(), Aleph::Gen_Lazy_Segment_Tree< Policy >::update(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::values().
|
private |
Definition at line 982 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::build(), Aleph::Gen_Lazy_Segment_Tree< Policy >::push_down(), Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl(), Aleph::Gen_Lazy_Segment_Tree< Policy >::set_impl(), Aleph::Gen_Lazy_Segment_Tree< Policy >::swap(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::update_impl().
|
mutableprivate |
Definition at line 979 of file tpl_segment_tree.H.
Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::build(), Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_leaves_recursive(), Aleph::Gen_Lazy_Segment_Tree< Policy >::push_down(), Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl(), Aleph::Gen_Lazy_Segment_Tree< Policy >::set_impl(), Aleph::Gen_Lazy_Segment_Tree< Policy >::swap(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::update_impl().