|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Ji Driver's Segment Tree (Segment Tree Beats). More...
#include <tpl_segment_tree.H>
Classes | |
| struct | Node |
Public Types | |
| using | Item_Type = T |
Public Member Functions | |
| Segment_Tree_Beats (const size_t num, const T &init_val=T()) | |
Construct with num elements, all equal to init_val. | |
| Segment_Tree_Beats (std::initializer_list< T > il) | |
| Construct from an initializer list. | |
| Segment_Tree_Beats (const Array< T > &values) | |
| Construct from an Array<T>. | |
| Segment_Tree_Beats (const std::vector< T > &values) | |
| Construct from a std::vector<T>. | |
| Segment_Tree_Beats (const DynList< T > &values) | |
| Construct from a DynList<T>. | |
| Segment_Tree_Beats (const Segment_Tree_Beats &)=default | |
| Segment_Tree_Beats (Segment_Tree_Beats &&other) noexcept(std::is_nothrow_move_constructible_v< Array< Node > >) | |
| Take the elements of another tree, which is left empty. | |
| Segment_Tree_Beats & | operator= (const Segment_Tree_Beats &)=default |
| Segment_Tree_Beats & | operator= (Segment_Tree_Beats &&other) noexcept(std::is_nothrow_move_assignable_v< Array< Node > >) |
| Exchange this tree with another, which is left with the previous elements of this one. | |
| void | chmin (const size_t l, const size_t r, const T &v) |
Range chmin: for each i in [l, r], set a[i] = min(a[i], v). | |
| void | chmax (const size_t l, const size_t r, const T &v) |
Range chmax: for each i in [l, r], set a[i] = max(a[i], v). | |
| T | query_sum (const size_t l, const size_t r) const |
Range sum query over [l, r]. | |
| T | query_max (const size_t l, const size_t r) const |
Range max query over [l, r]. | |
| T | query_min (const size_t l, const size_t r) const |
Range min query over [l, r]. | |
| T | get (const size_t i) const |
Retrieve the value at position 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 values into an Array. | |
| void | swap (Segment_Tree_Beats &other) noexcept |
Swap this tree with other in O(1). | |
Private Member Functions | |
| void | init_leaf (size_t node, const T &val) |
| void | pull_up (size_t node) const |
| void | push_chmin_to_child (size_t child, T v) const |
| void | push_chmax_to_child (size_t child, T v) const |
| void | push_down (size_t node) const |
| void | build (size_t node, size_t start, size_t end) |
| void | chmin_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r, T v) |
| void | chmax_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r, T v) |
| T | query_sum_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const |
| T | query_max_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const |
| T | query_min_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 (const size_t node, size_t start, size_t end, Getter &getter) |
| void | init_all_nodes () |
Private Attributes | |
| Array< Node > | tree |
| size_t | n = 0 |
Static Private Attributes | |
| static constexpr T | INF = std::numeric_limits<T>::max() |
| static constexpr T | NEG_INF = std::numeric_limits<T>::lowest() |
Ji Driver's Segment Tree (Segment Tree Beats).
Supports range chmin(l, r, v) and chmax(l, r, v) operations together with range sum, min, and max queries.
Each node maintains: max, second_max, count_max, min, second_min, count_min, sum. The push-down uses break/tag conditions for amortised O(n log^2 n) total complexity.
chmin/chmax) in addition to sum/min/max queries. For simpler update patterns, prefer Gen_Lazy_Segment_Tree.| T | a signed arithmetic type. |
Definition at line 1391 of file tpl_segment_tree.H.
| using Aleph::Segment_Tree_Beats< T >::Item_Type = T |
Definition at line 1652 of file tpl_segment_tree.H.
|
inline |
Construct with num elements, all equal to init_val.
| num | number of elements to allocate in the tree. |
| init_val | initial value assigned to every leaf. |
| std::bad_alloc | if internal storage allocation fails. |
Definition at line 1660 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::fill_and_build(), Aleph::Segment_Tree_Beats< T >::init_all_nodes(), and Aleph::Segment_Tree_Beats< T >::n.
|
inline |
Construct from an initializer list.
| il | source initializer list with the initial values. |
| std::bad_alloc | if internal storage allocation fails. |
Definition at line 1673 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::fill_and_build(), Aleph::Segment_Tree_Beats< T >::init_all_nodes(), and Aleph::Segment_Tree_Beats< T >::n.
|
inline |
Construct from an Array<T>.
| values | source array with the initial values. |
| std::bad_alloc | if internal storage allocation fails. |
Definition at line 1690 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::fill_and_build(), Aleph::Segment_Tree_Beats< T >::init_all_nodes(), Aleph::Segment_Tree_Beats< T >::n, and Aleph::Segment_Tree_Beats< T >::values().
|
inline |
Construct from a std::vector<T>.
| values | source vector with the initial values. |
| std::bad_alloc | if internal storage allocation fails. |
Definition at line 1704 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::fill_and_build(), Aleph::Segment_Tree_Beats< T >::init_all_nodes(), Aleph::Segment_Tree_Beats< T >::n, and Aleph::Segment_Tree_Beats< T >::values().
|
inline |
Construct from a DynList<T>.
| values | source list with the initial values. |
| std::bad_alloc | if internal storage allocation fails. |
Definition at line 1718 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::Segment_Tree_Beats< T >::fill_and_build(), Aleph::Segment_Tree_Beats< T >::init_all_nodes(), Aleph::Segment_Tree_Beats< T >::n, and Aleph::Segment_Tree_Beats< T >::values().
|
default |
|
inlinenoexcept |
Take the elements of another tree, which is left empty.
The implicit move took the nodes but copied the size: the moved-from tree then indexed nodes that it no longer had.
| [in,out] | other | tree to move from. |
Definition at line 1742 of file tpl_segment_tree.H.
|
inlineprivate |
Definition at line 1527 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::build(), and Aleph::Segment_Tree_Beats< T >::pull_up().
Referenced by Aleph::Segment_Tree_Beats< T >::build(), and Aleph::Segment_Tree_Beats< T >::fill_and_build().
|
inline |
Range chmax: for each i in [l, r], set a[i] = max(a[i], v).
| std::out_of_range | if l > r or r >= size(). |
Definition at line 1779 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Segment_Tree_Beats< T >::chmax_impl(), l, Aleph::Segment_Tree_Beats< T >::n, and r.
Referenced by scenario_3_balanceo_servidores(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
inlineprivate |
Definition at line 1557 of file tpl_segment_tree.H.
References Aleph::and, Aleph::Segment_Tree_Beats< T >::chmax_impl(), l, Aleph::Segment_Tree_Beats< T >::pull_up(), Aleph::Segment_Tree_Beats< T >::push_chmax_to_child(), Aleph::Segment_Tree_Beats< T >::push_down(), r, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::chmax(), and Aleph::Segment_Tree_Beats< T >::chmax_impl().
|
inline |
Range chmin: for each i in [l, r], set a[i] = min(a[i], v).
| std::out_of_range | if l > r or r >= size(). |
Definition at line 1765 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, Aleph::Segment_Tree_Beats< T >::chmin_impl(), l, Aleph::Segment_Tree_Beats< T >::n, and r.
Referenced by scenario_3_balanceo_servidores(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
inlineprivate |
Definition at line 1538 of file tpl_segment_tree.H.
References Aleph::and, Aleph::Segment_Tree_Beats< T >::chmin_impl(), l, Aleph::Segment_Tree_Beats< T >::pull_up(), Aleph::Segment_Tree_Beats< T >::push_chmin_to_child(), Aleph::Segment_Tree_Beats< T >::push_down(), r, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::chmin(), and Aleph::Segment_Tree_Beats< T >::chmin_impl().
|
inlineprivate |
Definition at line 1622 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::build(), Aleph::Segment_Tree_Beats< T >::fill_leaves_recursive(), and Aleph::Segment_Tree_Beats< T >::n.
Referenced by Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), and Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats().
|
inlineprivate |
Definition at line 1629 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::fill_leaves_recursive(), and Aleph::Segment_Tree_Beats< T >::init_leaf().
Referenced by Aleph::Segment_Tree_Beats< T >::fill_and_build(), and Aleph::Segment_Tree_Beats< T >::fill_leaves_recursive().
|
inline |
Retrieve the value at position i.
O(log n).
Definition at line 1832 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::query_sum().
Referenced by scenario_3_balanceo_servidores(), TEST(), TEST(), Moved_From_Case< Segment_Tree_Beats< int > >::use(), and Aleph::Segment_Tree_Beats< T >::values().
|
inlineprivate |
Definition at line 1643 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::INF, Aleph::Segment_Tree_Beats< T >::NEG_INF, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), and Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats().
|
inlineprivate |
Definition at line 1406 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::INF, Aleph::Segment_Tree_Beats< T >::NEG_INF, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::fill_leaves_recursive().
|
inlineconstexprnoexcept |
True if the tree contains no elements.
Definition at line 1841 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::n.
Referenced by Moved_From_Case< Segment_Tree_Beats< int > >::is_empty(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
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 1754 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::swap().
|
inlineprivate |
Definition at line 1412 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::INF, l, Aleph::Segment_Tree_Beats< T >::NEG_INF, r, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::build(), Aleph::Segment_Tree_Beats< T >::chmax_impl(), and Aleph::Segment_Tree_Beats< T >::chmin_impl().
|
inlineprivate |
Definition at line 1485 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::chmax_impl(), and Aleph::Segment_Tree_Beats< T >::push_down().
|
inlineprivate |
Definition at line 1463 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::chmin_impl(), and Aleph::Segment_Tree_Beats< T >::push_down().
|
inlineprivate |
Definition at line 1506 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::INF, Aleph::Segment_Tree_Beats< T >::NEG_INF, Aleph::Segment_Tree_Beats< T >::push_chmax_to_child(), Aleph::Segment_Tree_Beats< T >::push_chmin_to_child(), and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::chmax_impl(), Aleph::Segment_Tree_Beats< T >::chmin_impl(), Aleph::Segment_Tree_Beats< T >::query_max_impl(), Aleph::Segment_Tree_Beats< T >::query_min_impl(), and Aleph::Segment_Tree_Beats< T >::query_sum_impl().
|
inline |
Range max query over [l, r].
| std::out_of_range | if l > r or r >= size(). |
Definition at line 1807 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, l, Aleph::Segment_Tree_Beats< T >::n, Aleph::Segment_Tree_Beats< T >::query_max_impl(), and r.
Referenced by scenario_3_balanceo_servidores(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
inlineprivate |
Definition at line 1591 of file tpl_segment_tree.H.
References Aleph::and, l, Aleph::Segment_Tree_Beats< T >::NEG_INF, Aleph::Segment_Tree_Beats< T >::push_down(), Aleph::Segment_Tree_Beats< T >::query_max_impl(), r, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::query_max(), and Aleph::Segment_Tree_Beats< T >::query_max_impl().
|
inline |
Range min query over [l, r].
| std::out_of_range | if l > r or r >= size(). |
Definition at line 1821 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, l, Aleph::Segment_Tree_Beats< T >::n, Aleph::Segment_Tree_Beats< T >::query_min_impl(), and r.
Referenced by scenario_3_balanceo_servidores(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
inlineprivate |
Definition at line 1606 of file tpl_segment_tree.H.
References Aleph::and, Aleph::Segment_Tree_Beats< T >::INF, l, Aleph::Segment_Tree_Beats< T >::push_down(), Aleph::Segment_Tree_Beats< T >::query_min_impl(), r, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::query_min(), and Aleph::Segment_Tree_Beats< T >::query_min_impl().
|
inline |
Range sum query over [l, r].
| std::out_of_range | if l > r or r >= size(). |
Definition at line 1793 of file tpl_segment_tree.H.
References ah_out_of_range_error_if, l, Aleph::Segment_Tree_Beats< T >::n, Aleph::Segment_Tree_Beats< T >::query_sum_impl(), and r.
Referenced by Aleph::Segment_Tree_Beats< T >::get(), scenario_3_balanceo_servidores(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
inlineprivate |
Definition at line 1576 of file tpl_segment_tree.H.
References Aleph::and, l, Aleph::Segment_Tree_Beats< T >::push_down(), Aleph::Segment_Tree_Beats< T >::query_sum_impl(), r, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::query_sum(), and Aleph::Segment_Tree_Beats< T >::query_sum_impl().
|
inlineconstexprnoexcept |
Number of logical elements.
Definition at line 1838 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::n.
Referenced by scenario_3_balanceo_servidores(), TEST(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
inlinenoexcept |
Swap this tree with other in O(1).
Definition at line 1853 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::n, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by Aleph::Segment_Tree_Beats< T >::operator=(), and TEST().
|
inline |
Reconstruct all values into an Array.
O(n log n).
Definition at line 1844 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::Segment_Tree_Beats< T >::get(), and Aleph::Segment_Tree_Beats< T >::n.
Referenced by Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), TEST(), and Moved_From_Case< Segment_Tree_Beats< int > >::use().
|
staticconstexprprivate |
Definition at line 1403 of file tpl_segment_tree.H.
Referenced by Aleph::Segment_Tree_Beats< T >::init_all_nodes(), Aleph::Segment_Tree_Beats< T >::init_leaf(), Aleph::Segment_Tree_Beats< T >::pull_up(), Aleph::Segment_Tree_Beats< T >::push_down(), and Aleph::Segment_Tree_Beats< T >::query_min_impl().
|
private |
Definition at line 1401 of file tpl_segment_tree.H.
Referenced by Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::Segment_Tree_Beats(), Aleph::Segment_Tree_Beats< T >::chmax(), Aleph::Segment_Tree_Beats< T >::chmin(), Aleph::Segment_Tree_Beats< T >::fill_and_build(), Aleph::Segment_Tree_Beats< T >::is_empty(), Aleph::Segment_Tree_Beats< T >::query_max(), Aleph::Segment_Tree_Beats< T >::query_min(), Aleph::Segment_Tree_Beats< T >::query_sum(), Aleph::Segment_Tree_Beats< T >::size(), Aleph::Segment_Tree_Beats< T >::swap(), and Aleph::Segment_Tree_Beats< T >::values().
|
staticconstexprprivate |
Definition at line 1404 of file tpl_segment_tree.H.
Referenced by Aleph::Segment_Tree_Beats< T >::init_all_nodes(), Aleph::Segment_Tree_Beats< T >::init_leaf(), Aleph::Segment_Tree_Beats< T >::pull_up(), Aleph::Segment_Tree_Beats< T >::push_down(), and Aleph::Segment_Tree_Beats< T >::query_max_impl().
|
mutableprivate |
Definition at line 1400 of file tpl_segment_tree.H.
Referenced by Aleph::Segment_Tree_Beats< T >::chmax_impl(), Aleph::Segment_Tree_Beats< T >::chmin_impl(), Aleph::Segment_Tree_Beats< T >::init_all_nodes(), Aleph::Segment_Tree_Beats< T >::init_leaf(), Aleph::Segment_Tree_Beats< T >::pull_up(), Aleph::Segment_Tree_Beats< T >::push_chmax_to_child(), Aleph::Segment_Tree_Beats< T >::push_chmin_to_child(), Aleph::Segment_Tree_Beats< T >::push_down(), Aleph::Segment_Tree_Beats< T >::query_max_impl(), Aleph::Segment_Tree_Beats< T >::query_min_impl(), Aleph::Segment_Tree_Beats< T >::query_sum_impl(), and Aleph::Segment_Tree_Beats< T >::swap().