77#include < vector>
88
99namespace weilycoder {
10+ /* *
11+ * @brief Static Range Sum using Prefix Sums
12+ * @tparam Group A group defining the operation and identity element,
13+ * must be associative and invertible (i.e. Group).
14+ */
1015template <typename Group> struct StaticRangeSum {
1116 using value_type = typename Group::value_type;
1217 using T = value_type;
@@ -15,12 +20,23 @@ template <typename Group> struct StaticRangeSum {
1520 std::vector<T> prefix_sum;
1621
1722public:
23+ /* *
24+ * @brief Constructs a StaticRangeSum for n elements initialized to the
25+ * identity element
26+ * @param n Number of elements
27+ */
1828 explicit StaticRangeSum (const std::vector<T> &data)
1929 : prefix_sum(data.size() + 1, Group::identity()) {
2030 for (size_t i = 1 ; i <= data.size (); ++i)
2131 prefix_sum[i] = Group::operation (prefix_sum[i - 1 ], data[i - 1 ]);
2232 }
2333
34+ /* *
35+ * @brief Constructs a StaticRangeSum from an initial range
36+ * @tparam InputIt Input iterator type
37+ * @param first Beginning of the range
38+ * @param last End of the range
39+ */
2440 template <typename InputIt>
2541 StaticRangeSum (InputIt first, InputIt last)
2642 : prefix_sum(std::distance(first, last) + 1 , Group::identity()) {
@@ -29,8 +45,19 @@ template <typename Group> struct StaticRangeSum {
2945 prefix_sum[i] = Group::operation (prefix_sum[i - 1 ], *it);
3046 }
3147
48+ /* *
49+ * @brief Returns the number of elements
50+ * @return Number of elements
51+ */
3252 size_t size () const { return prefix_sum.size () - 1 ; }
3353
54+ /* *
55+ * @brief Queries the sum in the range [l, r)
56+ * @param l Left index (inclusive)
57+ * @param r Right index (exclusive)
58+ * @return Sum in the range [l, r)
59+ * @throws std::out_of_range if the range is invalid
60+ */
3461 T query (size_t l, size_t r) const {
3562 if (l > r || r > size ())
3663 throw std::out_of_range (" Invalid range for StaticRangeSum query" );
0 commit comments