|
Aleph-w 3.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 &&) 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 >)=default | |
| Gen_Lazy_Segment_Tree & | operator= (const Gen_Lazy_Segment_Tree &)=default |
| Gen_Lazy_Segment_Tree & | operator= (Gen_Lazy_Segment_Tree &&) noexcept(std::is_nothrow_move_assignable_v< Array< value_type > > and std::is_nothrow_move_assignable_v< Array< lazy_type > > and std::is_nothrow_move_assignable_v< Policy >)=default |
| 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(noexcept(std::swap(std::declval< Policy & >(), std::declval< 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 922 of file tpl_segment_tree.H.
Definition at line 927 of file tpl_segment_tree.H.
| using Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy_type = typename Policy::lazy_type |
Definition at line 926 of file tpl_segment_tree.H.
| using Aleph::Gen_Lazy_Segment_Tree< Policy >::value_type = typename Policy::value_type |
Definition at line 925 of file tpl_segment_tree.H.
|
inline |
Construct with num elements, all equal to init_val.
Definition at line 1063 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1075 of file tpl_segment_tree.H.
References StlAlephIterator< SetName >::begin(), Aleph::divide_and_conquer_partition_dp(), 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 1089 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 1100 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 1111 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 |
|
defaultnoexcept |
|
inlineprivate |
Definition at line 1056 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
|
inlineprivate |
Definition at line 953 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::build(), Aleph::divide_and_conquer_partition_dp(), 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 1023 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::build(), Aleph::divide_and_conquer_partition_dp(), 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 1045 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), 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 1030 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1203 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 1212 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
Referenced by TEST().
|
default |
|
defaultnoexcept |
|
inline |
Point update: apply delta to a[i].
Convenience shorthand for update(i, i, delta).
Definition at line 1157 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::update().
Referenced by TEST().
|
inlineprivate |
Definition at line 935 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1189 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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 985 of file tpl_segment_tree.H.
References Aleph::and, Aleph::divide_and_conquer_partition_dp(), 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 1170 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 1002 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1209 of file tpl_segment_tree.H.
References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.
Referenced by TEST().
|
inlinenoexcept |
Swap this tree with other in O(1).
Definition at line 1227 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.
Referenced by 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 1143 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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 964 of file tpl_segment_tree.H.
References Aleph::and, Aleph::divide_and_conquer_partition_dp(), 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 1218 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), 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 932 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 933 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 930 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().