mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-08 21:16:26 +08:00
changed int data type for non-negative numbers
This commit is contained in:
@@ -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<int, N> A; ///< input array to perform RMQ.
|
||||
std::array<std::array<int, N>, M>
|
||||
ST; ///< the sparse table storing `min()` values for given interval.
|
||||
std::array<int, 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.
|
||||
std::array<int64_t, N> 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<int, 10> testcase = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
int testcase_size = sizeof(testcase) / sizeof(testcase[0]);
|
||||
std::array<int64_t, 10> 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{};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user