uint32_t
N= 12345;
49constexpruint8_t
M= 14;
57std::array<int64_t, N>
A= {};
58std::array<std::array<int64_t, N>,
M>
60std::array<int64_t, N>
LOG= {};
72 for(
size_ti = 0; i <
n; ++i) {
73 ST[0][i] =
static_cast<int64_t
>(i);
74 LOG[i + 1] =
LOG[i] + !(i & (i + 1));
77 for(
size_tj = 1;
static_cast<size_t>(1 << j) <=
n; ++j) {
78 for(
size_ti = 0;
static_cast<size_t>(i + (1 << j)) <=
n; ++i) {
88int64_t x =
ST[j - 1][i];
96(
A[x] <=
A[y] ? x : y);
110int64_t
query(int64_t l, int64_t r) {
111int64_t g =
LOG[r - l + 1];
112int64_t x =
ST[g][l];
115 ST[g][r - (1 << g) + 1];
118 return(
A[x] <=
A[y] ? x : y);
133std::array<int64_t, 10> testcase = {
136 size_ttestcase_size =
137 sizeof(testcase) /
sizeof(testcase[0]);
142std::copy(std::begin(testcase), std::end(testcase),
144st.
n= testcase_size;
149assert(st.
query(1, 9) == 1);
150assert(st.
query(2, 6) == 2);
151assert(st.
query(3, 8) == 3);
153std::cout <<
"Self-test implementations passed!"<< std::endl;
Functions for Implementation of Sparse Table
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
static void test()
Self-test implementations.
int main()
Main function.
constexpr uint8_t M
ceil(log2(N)).
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).
std::array< int64_t, N > LOG
where floor(log2(i)) are precomputed.
std::array< int64_t, N > A
input array to perform RMQ.
std::array< std::array< int64_t, N >, M > ST
the sparse table storing min() values for given interval.
size_t n
size of input array.
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