From bbec149f6dbee08cfd95f6dcbf27cdea1e65727a Mon Sep 17 00:00:00 2001 From: Paulo Vitor Lima Borges <31678236+PauloVLB@users.noreply.github.com> Date: Fri, 15 Oct 2021 13:17:10 -0300 Subject: [PATCH] feat: add prefix sum array data structure (#1663) * feat: add prefix sum array data structure * feat: add prefix_sum_array data structure * updating DIRECTORY.md * feat: Add Prefix Sum Array datastructure * feat: Add Prefix Sum Array data structure * Update range_queries/prefix_sum_array.cpp Co-authored-by: David Leal * Update prefix_sum_array.cpp * Update prefix_sum_array.cpp * Update range_queries/prefix_sum_array.cpp Co-authored-by: David Leal * Update range_queries/prefix_sum_array.cpp Co-authored-by: David Leal * Update range_queries/prefix_sum_array.cpp Co-authored-by: David Leal * Update range_queries/prefix_sum_array.cpp * Update range_queries/prefix_sum_array.cpp Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal --- DIRECTORY.md | 1 + range_queries/prefix_sum_array.cpp | 83 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 range_queries/prefix_sum_array.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index c6ea201b5..207692b7e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -276,6 +276,7 @@ * [Heavy Light Decomposition](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/heavy_light_decomposition.cpp) * [Mo](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/mo.cpp) * [Persistent Seg Tree Lazy Prop](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/persistent_seg_tree_lazy_prop.cpp) + * [Prefix Sum Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/prefix_sum_array.cpp) * [Segtree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/segtree.cpp) * [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/range_queries/sparse_table.cpp) diff --git a/range_queries/prefix_sum_array.cpp b/range_queries/prefix_sum_array.cpp new file mode 100644 index 000000000..1ddb90eaf --- /dev/null +++ b/range_queries/prefix_sum_array.cpp @@ -0,0 +1,83 @@ +/** + * @file + * @brief + * [Prefix Sum + * Array](https://en.wikipedia.org/wiki/Prefix_sum) data structure + * implementation. + * + * @details + * Prefix Sum Array is a data structure, that allows answering sum in some range + * queries. It can answer most sum range queries in O(1), with a build time + * complexity of O(N). But it hasn't an update querie. + * + * * Running Time Complexity \n + * * Build : O(N) \n + * * Range Query : O(1) \n + * @author [Paulo Vitor Lima Borges](https://github.com/PauloVLB) + */ + +#include /// for assert +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace range_queries + * @brief Range Queries algorithms + */ +namespace range_queries { +/** + * @namespace prefix_sum_array + * @brief Range sum queries using prefix-sum-array + */ +namespace prefix_sum_array { + +std::vector PSA(1, 0); + +/** + * @brief function that builds the PSA + * @param original_array original array of values + * @returns void + */ +void build(std::vector original_array) { + for (int i = 1; i <= static_cast(original_array.size()); i++) { + PSA.push_back(PSA[i - 1] + original_array[i]); + } +} +/** + * @brief query function + * @param beg begin of the interval to sum + * @param end end of the interval to sum + * @returns sum of the range [beg, end] + */ +int64_t query(int64_t beg, int64_t end) { return PSA[end] - PSA[beg - 1]; } + +} // namespace prefix_sum_array +} // namespace range_queries + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + std::vector values{0, 123, 0, 2, -2, 5, + 24, 0, 23, -1, -1}; // original array + + range_queries::prefix_sum_array::build(values); + // queries are of the type: sum of the range [a, b] = psa[b] - psa[a-1] + + assert(range_queries::prefix_sum_array::query(1, 10) == + 173); // sum of the entire array + assert(range_queries::prefix_sum_array::query(4, 6) == + 27); // the sum of the interval [4, 6] + assert(range_queries::prefix_sum_array::query(5, 9) == + 51); // the sum of the interval [5, 9] +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +}