Skip to the content.

:heavy_check_mark: test/library-checker/DataStructure/StaticRangeSum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"
#include <bits/stdc++.h>

#include "library/datastructure/CumulativeSum.hpp"

int main() {
    int n, q;
    std::cin >> n >> q;
    std::vector<int> v(n);
    for (int i = 0; i < n; i++)
        std::cin >> v[i];
    auto wa = CumulativeSum(v);
    while (q--) {
        int l, r;
        std::cin >> l >> r;
        std::cout << wa.sum(l, r) << '\n';
    }
}
#line 1 "test/library-checker/DataStructure/StaticRangeSum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"
#include <bits/stdc++.h>

#line 1 "library/datastructure/CumulativeSum.hpp"
template <typename T> struct CumulativeSum {
    using U = std::conditional_t<std::is_same_v<T, int>, long long, T>;
    std::vector<U> A;
    CumulativeSum() : A(1, 0) {}
    CumulativeSum(const std::vector<T> &v) : A(v.size() + 1, 0) {
        for (int i = 0; i < v.size(); i++)
            A[i + 1] = A[i] + v[i];
    }
    void add(const T &a) { A.push_back(A.back() + a); }
    U sum(int l, int r) {
        assert(0 <= l and l <= r and r < A.size());
        return A[r] - A[l];
    }
    U sum() { return A.back(); }
};
#line 5 "test/library-checker/DataStructure/StaticRangeSum.test.cpp"

int main() {
    int n, q;
    std::cin >> n >> q;
    std::vector<int> v(n);
    for (int i = 0; i < n; i++)
        std::cin >> v[i];
    auto wa = CumulativeSum(v);
    while (q--) {
        int l, r;
        std::cin >> l >> r;
        std::cout << wa.sum(l, r) << '\n';
    }
}
Back to top page