|
ADS
Algorithms and Data Structures
|
the range minima of an array by the sparse table More...
#include <ADS/search/RangeMinimum.h>
Public types | |
| using | value_type = T |
| the type of the items | |
| using | size_type = std::size_t |
| the type of the sizes | |
| using | difference_type = std::ptrdiff_t |
| the type of the distances | |
| using | const_reference = const T & |
| a reference to an item | |
| using | reference = const_reference |
| the same, constant | |
| using | const_iterator = typename std::vector< T >::const_iterator |
| an iterator on the items | |
| using | iterator = const_iterator |
| the same, constant | |
Public member functions | |
| SparseTable (void)=default | |
| an empty table | |
| SparseTable (std::vector< T > A, Compare less={}) | |
| the table of \( A \), \( O( n \log n ) \) | |
| std::size_t | query (std::size_t i, std::size_t j) const |
| the place of the leftmost minimum of \( A[ i .. j ] \), \( O( 1 ) \) | |
| std::size_t | size (void) const |
| the number \( n \) of the items | |
| const T & | operator[] (std::size_t i) const |
| the item of place \( i \) | |
| const_iterator | begin (void) const |
| the first item | |
| const_iterator | end (void) const |
| past the last item | |
| const_iterator | cbegin (void) const |
| the first item | |
| const_iterator | cend (void) const |
| past the last item | |
the range minima of an array by the sparse table
| using ADS::SparseTable< T, Compare >::const_iterator = typename std::vector< T >::const_iterator |
an iterator on the items
| using ADS::SparseTable< T, Compare >::const_reference = const T & |
a reference to an item
| using ADS::SparseTable< T, Compare >::difference_type = std::ptrdiff_t |
the type of the distances
| using ADS::SparseTable< T, Compare >::iterator = const_iterator |
the same, constant
| using ADS::SparseTable< T, Compare >::reference = const_reference |
the same, constant
| using ADS::SparseTable< T, Compare >::size_type = std::size_t |
the type of the sizes
| using ADS::SparseTable< T, Compare >::value_type = T |
the type of the items
|
default |
an empty table
|
explicit |
the table of \( A \), \( O( n \log n ) \)
In pseudocode (Bender and Farach-Colton, beyond the book), \( M[ L ][ i ] \) the place of the leftmost minimum of \( A[ i .. i + 2^L - 1 ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}( A ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ M[ 0 ][ i ] \gets i \cr 3 & \quad \textbf{for}\ L \gets 1\ \textbf{to}\ \lfloor \log_2 n \rfloor \ \textbf{do} \cr 4 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 2^L\ \textbf{do} \cr 5 & \qquad\quad M[ L ][ i ] \gets \text{the better of } M[ L - 1 ][ i ] \text{ and } M[ L - 1 ][ i + 2^{L-1} ] \end{array}} \]
where the better place is the one of the smaller item, the left one on a tie.
|
inlinenodiscard |
the first item
|
inlinenodiscard |
the first item
|
inlinenodiscard |
past the last item
|
inlinenodiscard |
past the last item
|
nodiscard |
the item of place \( i \)
|
nodiscard |
the place of the leftmost minimum of \( A[ i .. j ] \), \( O( 1 ) \)
In pseudocode (Bender and Farach-Colton, beyond the book), two ranges of length \( 2^L \) covering \( [ i , j ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{query}( i , j ) \cr 2 & \quad L \gets \lfloor \log_2 ( j - i + 1 ) \rfloor \cr 3 & \quad \textbf{return}\ \text{the better of } M[ L ][ i ] \text{ and } M[ L ][ j - 2^L + 1 ] \end{array}} \]
|
inlinenodiscard |
the number \( n \) of the items