Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Implementation of Sparse Table data structure. More...
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
Namespaces | |
namespace | range_queries |
for std::vector | |
namespace | sparse_table |
Functions for Implementation of Sparse Table | |
Functions | |
template<typename T > | |
std::vector< T > | range_queries::sparse_table::computeLogs (const std::vector< T > &A) |
template<typename T > | |
std::vector< std::vector< T > > | range_queries::sparse_table::buildTable (const std::vector< T > &A, const std::vector< T > &logs) |
template<typename T > | |
int | range_queries::sparse_table::getMinimum (int beg, int end, const std::vector< T > &logs, const std::vector< std::vector< T > > &table) |
int | main () |
Implementation of Sparse Table data structure.
Sparse Table is a data structure, that allows answering range queries. It can answer most range queries in O(logn), but its true power is answering range minimum queries or equivalent range maximum queries). For those queries it can compute the answer in O(1) time.
std::vector< std::vector< T > > range_queries::sparse_table::buildTable | ( | const std::vector< T > & | A, |
const std::vector< T > & | logs ) |
This functions builds the primary data structure sparse table
n | value of the size of the input array |
A | array of the input integers |
logs | array of the log table |
std::vector< T > range_queries::sparse_table::computeLogs | ( | const std::vector< T > & | A | ) |
This function precomputes intial log table for further use.
n | value of the size of the input array |
int range_queries::sparse_table::getMinimum | ( | int | beg, |
int | end, | ||
const std::vector< T > & | logs, | ||
const std::vector< std::vector< T > > & | table ) |
This function is the query function to get the range minimum value
beg | beginning index of the query range |
end | ending index of the query range |
logs | array of the log table |
table | sparse table data structure for the input array |
int main | ( | void | ) |
Main function