48constexpr uint32_t
N = 12345;
49constexpr uint8_t
M = 14;
57 std::array<int64_t, N>
A = {};
58 std::array<std::array<int64_t, N>,
M>
60 std::array<int64_t, N>
LOG = {};
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));
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) {
88 int64_t x =
ST[j - 1][i];
96 (
A[x] <=
A[y] ? x : y);
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];
115 ST[g][r - (1 << g) + 1];
118 return (
A[x] <=
A[y] ? x : y);
133 std::array<int64_t, 10> testcase = {
136 size_t testcase_size =
137 sizeof(testcase) /
sizeof(testcase[0]);
142 std::copy(std::begin(testcase), std::end(testcase),
144 st.n = testcase_size;
149 assert(st.query(1, 9) == 1);
150 assert(st.query(2, 6) == 2);
151 assert(st.query(3, 8) == 3);
153 std::cout <<
"Self-test implementations passed!" << std::endl;
162int main(
int argc,
char *argv[]) {
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.
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.