A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d2/d96/sparse__table__range__queries_8cpp_source.html below:

TheAlgorithms/C++: range_queries/sparse_table_range_queries.cpp Source File

31namespace

sparse_table {

38

std::vector<T> computeLogs(

const

std::vector<T>& A) {

40

std::vector<T> logs(n);

42 for

(

int

i = 2; i < n; i++) {

43

logs[i] = logs[i / 2] + 1;

56

std::vector<std::vector<T> > buildTable(

const

std::vector<T>& A,

57 const

std::vector<T>& logs) {

59

std::vector<std::vector<T> > table(20, std::vector<T>(n + 5, 0));

61 for

(

int

i = 0; i <= logs[n]; i++) {

63 for

(

int

j = 0; j + curLen < n; j++) {

68

std::min(table[i - 1][j], table[i - 1][j + curLen / 2]);

84int

getMinimum(

int

beg,

int

end,

const

std::vector<T>& logs,

85 const

std::vector<std::vector<T> >& table) {

86 int

p = logs[end - beg + 1];

88 return

std::min(table[p][beg], table[p][end - pLen + 1]);

97

std::vector<int> A{1, 2, 0, 3, 9};

98

std::vector<int> logs = range_queries::sparse_table::computeLogs(A);

99

std::vector<std::vector<int> > table =

100

range_queries::sparse_table::buildTable(A, logs);

101

assert(range_queries::sparse_table::getMinimum(0, 0, logs, table) == 1);

102

assert(range_queries::sparse_table::getMinimum(0, 4, logs, table) == 0);

103

assert(range_queries::sparse_table::getMinimum(2, 4, logs, table) == 0);

int main()

Main function.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4