algorithm

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub gtnao/algorithm

:heavy_check_mark: src/dp/cumulative_sum.hpp

Verified with

Code

#pragma once

#include <bits/stdc++.h>
using namespace std;

template <typename T> struct CumulativeSum {
private:
  vector<T> s;

public:
  CumulativeSum(const vector<T> &nums) {
    int n = nums.size();
    s.resize(n + 1, 0);
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + nums[i];
    }
  }

  // [l, r), l and r is 0-indexed.
  T sum(int l, int r) { return s[r] - s[l]; }
};
#line 2 "src/dp/cumulative_sum.hpp"

#include <bits/stdc++.h>
using namespace std;

template <typename T> struct CumulativeSum {
private:
  vector<T> s;

public:
  CumulativeSum(const vector<T> &nums) {
    int n = nums.size();
    s.resize(n + 1, 0);
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + nums[i];
    }
  }

  // [l, r), l and r is 0-indexed.
  T sum(int l, int r) { return s[r] - s[l]; }
};
Back to top page