library

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

View the Project on GitHub gyouzasushi/library

:heavy_check_mark: 累積和
(datastructure/cumulative_sum.hpp)

概要

使い方

計算量

Verified with

Code

#pragma once
#include <vector>
template <typename T>
struct cumulative_sum {
    int n;
    std::vector<T> cumsum;
    cumulative_sum() {
    }
    cumulative_sum(std::vector<T>& d) : n(d.size()), cumsum(n + 1) {
        for (int i = 0; i < n; i++) {
            cumsum[i + 1] = cumsum[i] + d[i];
        }
    }
    T sum(int l, int r) {
        // 0-indexed
        // [l, r)
        assert(0 <= l);
        assert(l <= r);
        assert(r <= n);
        return cumsum[r] - cumsum[l];
    }
};
#line 2 "datastructure/cumulative_sum.hpp"
#include <vector>
template <typename T>
struct cumulative_sum {
    int n;
    std::vector<T> cumsum;
    cumulative_sum() {
    }
    cumulative_sum(std::vector<T>& d) : n(d.size()), cumsum(n + 1) {
        for (int i = 0; i < n; i++) {
            cumsum[i + 1] = cumsum[i] + d[i];
        }
    }
    T sum(int l, int r) {
        // 0-indexed
        // [l, r)
        assert(0 <= l);
        assert(l <= r);
        assert(r <= n);
        return cumsum[r] - cumsum[l];
    }
};
Back to top page