From ed1d09f71b6b74c934b7c0dd7b46bc0dad7caacc Mon Sep 17 00:00:00 2001 From: Mann Patel Date: Sun, 6 Jun 2021 04:35:35 +0000 Subject: [PATCH] changed int data type for non-negative numbers --- data_structures/sparse_table.cpp | 48 +++++++++++++++----------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/data_structures/sparse_table.cpp b/data_structures/sparse_table.cpp index d5d1d6784..d17b802bb 100644 --- a/data_structures/sparse_table.cpp +++ b/data_structures/sparse_table.cpp @@ -32,27 +32,28 @@ namespace data_structures { /** * @namespace sparse_table - * @brief Functions for Implementation of [Sparse Table](https://brilliant.org/wiki/sparse-table/) + * @brief Functions for Implementation of [Sparse + * Table](https://brilliant.org/wiki/sparse-table/) */ namespace sparse_table { /** * @brief A struct to represent sparse table for `min()` as their invariant - * function, for the given array `A`. The answer to queries are stored in the array - * ST. + * function, for the given array `A`. The answer to queries are stored in the + * array ST. */ struct Sparse_table { - const static int N = 12345; ///< the maximum size of the array. - const static int M = 14; ///< ceil(log2(N)). - int n = 0; ///< size of input array. + const static uint32_t N = 12345; ///< the maximum size of the array. + const static uint8_t M = 14; ///< ceil(log2(N)). + uint32_t n = 0; ///< size of input array. /** @warning check if `N` is not less than `n`. if so, manually increase the * value of N */ - std::array A; ///< input array to perform RMQ. - std::array, M> - ST; ///< the sparse table storing `min()` values for given interval. - std::array LOG; ///< where floor(log2(i)) are precomputed. + std::array A = {}; ///< input array to perform RMQ. + std::array, M> ST = + {}; ///< the sparse table storing `min()` values for given interval. + std::array LOG = {}; ///< where floor(log2(i)) are precomputed. /** * @brief Builds the sparse table for computing min/max/gcd/lcm/...etc @@ -64,15 +65,15 @@ struct Sparse_table { void buildST() { LOG[0] = -1; - for (int i = 0; i < n; ++i) { + for (uint32_t i = 0; i < n; ++i) { ST[0][i] = i; LOG[i + 1] = LOG[i] + !(i & (i + 1)); } - for (int j = 1; (1 << j) <= n; ++j) { - for (int i = 0; (i + (1 << j)) <= n; ++i) { - int x = ST[j - 1][i]; - int y = ST[j - 1][i + (1 << (j - 1))]; + for (uint32_t j = 1; (1 << j) <= n; ++j) { + for (uint32_t i = 0; (i + (1 << j)) <= n; ++i) { + int64_t x = ST[j - 1][i]; + int64_t y = ST[j - 1][i + (1 << (j - 1))]; ST[j][i] = (A[x] <= A[y] ? x : y); } @@ -82,18 +83,15 @@ struct Sparse_table { /** * @brief Queries the sparse table for the value of the interval [l, r] * (i.e. from l to r inclusive). - * - * @complexity: O(1) - * * @param l the left index of the range (inclusive). * @param r the right index of the range (inclusive). - * * @return the computed value of the given interval. + * @complexity: O(1) */ - int query(int l, int r) { - int g = LOG[r - l + 1]; - int x = ST[g][l]; - int y = ST[g][r - (1 << g) + 1]; + int64_t query(int64_t l, int64_t r) { + int64_t g = LOG[r - l + 1]; + int64_t x = ST[g][l]; + int64_t y = ST[g][r - (1 << g) + 1]; return (A[x] <= A[y] ? x : y); } }; @@ -105,8 +103,8 @@ struct Sparse_table { * @returns void */ static void test() { - std::array testcase = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int testcase_size = sizeof(testcase) / sizeof(testcase[0]); + std::array testcase = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + uint32_t testcase_size = sizeof(testcase) / sizeof(testcase[0]); data_structures::sparse_table::Sparse_table st{};