cp_library

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

View the Project on GitHub SSRS-cp/cp_library

:heavy_check_mark: test/library_checker/data_structure/static_range_sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"
#include <bits/stdc++.h>
using namespace std;
#include "../../../data_structure/sequence/cumulative_sum.hpp"
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<long long> a(N);
  for (int i = 0; i < N; i++){
    cin >> a[i];
  }
  cumulative_sum<long long> S(a, plus<long long>(), 0);
  for (int i = 0; i < Q; i++){
    int l, r;
    cin >> l >> r;
    cout << S.get(r) - S.get(l) << endl;
  }
}
#line 1 "test/library_checker/data_structure/static_range_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"
#include <bits/stdc++.h>
using namespace std;
#line 2 "data_structure/sequence/cumulative_sum.hpp"
/**
 * @brief 累積和
*/
template <typename T>
struct cumulative_sum{
  vector<T> S;
  function<T(T, T)> f;
  T E;
  cumulative_sum(){
  }
  cumulative_sum(vector<T> A, function<T(T, T)> f, T E): f(f), E(E){
    int N = A.size();
    S = vector<T>(N + 1);
    S[0] = E;
    for (int i = 0; i < N; i++){
      S[i + 1] = f(S[i], A[i]);
    }
  }
  T get(int i){
    return S[i];
  }
};
#line 5 "test/library_checker/data_structure/static_range_sum.test.cpp"
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<long long> a(N);
  for (int i = 0; i < N; i++){
    cin >> a[i];
  }
  cumulative_sum<long long> S(a, plus<long long>(), 0);
  for (int i = 0; i < Q; i++){
    int l, r;
    cin >> l >> r;
    cout << S.get(r) - S.get(l) << endl;
  }
}
Back to top page