|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
O(1) static range queries via the Cartesian Tree reduction. More...
#include <tpl_cartesian_tree.H>
Public Types | |
| using | Item_Type = T |
| The type of the element. | |
Public Member Functions | |
| Gen_Cartesian_Tree_RMQ (const Array< T > &values, Comp c=Comp()) | |
| Construct from an Array<T>. | |
| Gen_Cartesian_Tree_RMQ (const std::vector< T > &values, Comp c=Comp()) | |
| Construct from a std::vector<T>. | |
| Gen_Cartesian_Tree_RMQ (std::initializer_list< T > il, Comp c=Comp()) | |
| Construct from an initializer_list<T>. | |
| Gen_Cartesian_Tree_RMQ (const DynList< T > &values, Comp c=Comp()) | |
| Construct from a DynList<T>. | |
| Gen_Cartesian_Tree_RMQ (const size_t num, const T &init, Comp c=Comp()) | |
Construct with num elements all equal to init. | |
| T | query (const size_t l, const size_t r) const |
Range query over [l, r] in O(1). | |
| size_t | query_idx (const size_t l, const size_t r) const |
Return the index of the optimal element in a[l..r]. | |
| T | get (const size_t i) const |
Retrieve the value at position i in O(1). | |
| constexpr size_t | size () const noexcept |
| Number of elements. | |
| constexpr bool | is_empty () const noexcept |
| True if empty. | |
| Array< T > | values () const |
| Copy of the original values. | |
| const Gen_Euler_Tour_LCA< T, Comp > & | lca_engine () const noexcept |
| Const reference to the internal LCA engine. | |
Private Attributes | |
| Gen_Euler_Tour_LCA< T, Comp > | lca_ |
O(1) static range queries via the Cartesian Tree reduction.
Wraps Gen_Euler_Tour_LCA and exposes a range query interface compatible with Gen_Sparse_Table:
query(l, r) returns the optimal value in a[l..r] under Comp. query_idx(l, r) returns the index of that optimal value.
The key insight: RMQ(l, r) = data_at(LCA(l, r)) because the Cartesian Tree's root in any subrange is the position of the optimal element.
| T | element type. |
| Comp | comparator for the heap property. |
Definition at line 829 of file tpl_cartesian_tree.H.
The type of the element.
Definition at line 835 of file tpl_cartesian_tree.H.
|
inline |
Construct from an Array<T>.
Definition at line 838 of file tpl_cartesian_tree.H.
|
inline |
Construct from a std::vector<T>.
Definition at line 842 of file tpl_cartesian_tree.H.
|
inline |
Construct from an initializer_list<T>.
Definition at line 846 of file tpl_cartesian_tree.H.
|
inline |
Construct from a DynList<T>.
Definition at line 850 of file tpl_cartesian_tree.H.
|
inline |
Construct with num elements all equal to init.
Definition at line 854 of file tpl_cartesian_tree.H.
|
inline |
Retrieve the value at position i in O(1).
| i | 0-based index into the original array. |
i. | std::out_of_range | if i >= size(). |
Definition at line 902 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_.
|
inlineconstexprnoexcept |
True if empty.
Definition at line 914 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_.
|
inlinenoexcept |
Const reference to the internal LCA engine.
Definition at line 926 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_.
Range query over [l, r] in O(1).
Returns the optimal value (min or max depending on Comp) in positions l..r of the original array.
| 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 867 of file tpl_cartesian_tree.H.
References ah_out_of_range_error_if, l, Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_, r, and Aleph::HTList::size().
|
inline |
Return the index of the optimal element in a[l..r].
| l | 0-based left index (inclusive). |
| r | 0-based right index (inclusive). |
| std::out_of_range | if l > r or r >= size(). |
When the optimal value occurs multiple times in the range, this returns the leftmost occurrence (stable tie-break).
Definition at line 886 of file tpl_cartesian_tree.H.
References ah_out_of_range_error_if, l, Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_, r, and Aleph::HTList::size().
Referenced by TEST().
|
inlineconstexprnoexcept |
Number of elements.
Definition at line 908 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_.
|
inline |
Copy of the original values.
Definition at line 920 of file tpl_cartesian_tree.H.
References Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_.
|
private |
Definition at line 831 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::get(), Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::is_empty(), Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::lca_engine(), Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::query(), Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::query_idx(), Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::size(), and Aleph::Gen_Cartesian_Tree_RMQ< T, Comp >::values().