TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
sparse_table.cpp
Go to the documentation of this file.
1
24
25
#include <array>
26
#include <cassert>
27
#include <cstdint>
28
#include <iostream>
29
34
namespace
data_structures
{
35
41
namespace
sparse_table
{
42
48
constexpr
uint32_t
N
= 12345;
49
constexpr
uint8_t
M
= 14;
50
51
struct
Sparse_table
{
52
size_t
n
= 0;
53
56
57
std::array<int64_t, N>
A
= {};
58
std::array<std::array<int64_t, N>,
M
>
59
ST
{};
60
std::array<int64_t, N>
LOG
= {};
61
69
void
buildST() {
70
LOG
[0] = -1;
71
72
for
(
size_t
i = 0; i <
n
; ++i) {
73
ST
[0][i] =
static_cast<
int64_t
>
(i);
74
LOG
[i + 1] =
LOG
[i] + !(i & (i + 1));
75
}
76
77
for
(
size_t
j = 1;
static_cast<
size_t
>
(1 << j) <=
n
; ++j) {
78
for
(
size_t
i = 0;
static_cast<
size_t
>
(i + (1 << j)) <=
n
; ++i) {
87
88
int64_t x =
ST
[j - 1][i];
90
int64_t y =
91
ST
[j - 1]
92
[i + (1 << (j - 1))];
94
95
ST
[j][i] =
96
(
A
[x] <=
A
[y] ? x : y);
98
}
99
}
100
}
101
110
int64_t
query
(int64_t l, int64_t r) {
111
int64_t g =
LOG
[r - l + 1];
112
int64_t x =
ST
[g][l];
114
int64_t y =
115
ST
[g][r - (1 << g) + 1];
117
118
return
(
A
[x] <=
A
[y] ? x : y);
120
}
121
};
122
}
// namespace sparse_table
123
}
// namespace data_structures
124
129
static
void
test
() {
130
/* We take an array as an input on which we need to perform the ranged
131
* minimum queries[RMQ](https://en.wikipedia.org/wiki/Range_minimum_query).
132
*/
133
std::array<int64_t, 10> testcase = {
134
1, 2, 3, 4, 5,
135
6, 7, 8, 9, 10};
136
size_t
testcase_size =
137
sizeof
(testcase) /
sizeof
(testcase[0]);
138
139
data_structures::sparse_table::Sparse_table
140
st{};
141
142
std::copy(std::begin(testcase), std::end(testcase),
143
std::begin(st.
A
));
144
st.
n
= testcase_size;
145
146
st.buildST();
147
148
// pass queries of the form: [l,r]
149
assert(st.
query
(1, 9) == 1);
150
assert(st.
query
(2, 6) == 2);
151
assert(st.
query
(3, 8) == 3);
152
153
std::cout <<
"Self-test implementations passed!"
<< std::endl;
154
}
155
160
int
main
() {
161
test
();
// run self-test implementations
162
return
0;
163
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data_structures
for IO operations
sparse_table
Functions for Implementation of Sparse Table.
data_structures::sparse_table::N
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
Definition
sparse_table.cpp:48
data_structures::sparse_table::M
constexpr uint8_t M
ceil(log2(N)).
Definition
sparse_table.cpp:49
data_structures::sparse_table::Sparse_table
Definition
sparse_table.cpp:51
data_structures::sparse_table::Sparse_table::query
int64_t query(int64_t l, int64_t r)
Queries the sparse table for the value of the interval [l, r] (i.e. from l to r inclusive).
Definition
sparse_table.cpp:110
data_structures::sparse_table::Sparse_table::LOG
std::array< int64_t, N > LOG
where floor(log2(i)) are precomputed.
Definition
sparse_table.cpp:60
data_structures::sparse_table::Sparse_table::A
std::array< int64_t, N > A
input array to perform RMQ.
Definition
sparse_table.cpp:57
data_structures::sparse_table::Sparse_table::ST
std::array< std::array< int64_t, N >, M > ST
the sparse table storing min() values for given interval.
Definition
sparse_table.cpp:59
data_structures::sparse_table::Sparse_table::n
size_t n
size of input array.
Definition
sparse_table.cpp:52
data_structures
sparse_table.cpp
Generated by
1.18.0