|
Aleph-w 3.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 &&) noexcept(std::is_nothrow_move_constructible_v< Array< Node > >)=default | |
| Segment_Tree_Beats & | operator= (const Segment_Tree_Beats &)=default |
| Segment_Tree_Beats & | operator= (Segment_Tree_Beats &&) noexcept(std::is_nothrow_move_assignable_v< Array< Node > >)=default |
| 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 1316 of file tpl_segment_tree.H.
Definition at line 1577 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 1585 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1598 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1615 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 1629 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 1643 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), 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 |
|
defaultnoexcept |
|
inlineprivate |
Definition at line 1452 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::build(), Aleph::divide_and_conquer_partition_dp(), 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().
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 1683 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 TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 1482 of file tpl_segment_tree.H.
References Aleph::and, Aleph::Segment_Tree_Beats< T >::chmax_impl(), Aleph::divide_and_conquer_partition_dp(), 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().
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 1669 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 TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 1463 of file tpl_segment_tree.H.
References Aleph::and, Aleph::Segment_Tree_Beats< T >::chmin_impl(), Aleph::divide_and_conquer_partition_dp(), 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 1547 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::build(), Aleph::divide_and_conquer_partition_dp(), 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 1554 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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().
Retrieve the value at position i.
O(log n).
Definition at line 1736 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::query_sum().
Referenced by scenario_3_balanceo_servidores(), TEST(), TEST(), and Aleph::Segment_Tree_Beats< T >::values().
|
inlineprivate |
Definition at line 1568 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1331 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 1745 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::n.
Referenced by TEST().
|
default |
|
defaultnoexcept |
|
inlineprivate |
Definition at line 1337 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 1410 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), and 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 1388 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), and 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 1431 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), 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 1711 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.
|
inlineprivate |
Definition at line 1516 of file tpl_segment_tree.H.
References Aleph::and, Aleph::divide_and_conquer_partition_dp(), 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 1725 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.
|
inlineprivate |
Definition at line 1531 of file tpl_segment_tree.H.
References Aleph::and, Aleph::divide_and_conquer_partition_dp(), 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 1697 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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 1501 of file tpl_segment_tree.H.
References Aleph::and, Aleph::divide_and_conquer_partition_dp(), 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 1742 of file tpl_segment_tree.H.
References Aleph::Segment_Tree_Beats< T >::n.
Referenced by TEST().
|
inlinenoexcept |
Swap this tree with other in O(1).
Definition at line 1757 of file tpl_segment_tree.H.
References Aleph::divide_and_conquer_partition_dp(), Aleph::Segment_Tree_Beats< T >::n, and Aleph::Segment_Tree_Beats< T >::tree.
Referenced by TEST().
Reconstruct all values into an Array.
O(n log n).
Definition at line 1748 of file tpl_segment_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), 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(), and TEST().
|
staticconstexprprivate |
Definition at line 1328 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 1326 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 1329 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().
Definition at line 1325 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().