serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: 二項差での累積和
(library/dp/cumulative_sum/cumulative_sum_diff.hpp)

二項差での累積和

できること

計算量

$O(N)$

使い方

vector<long long> S = cumulative_sum_diff(A);

Verified with

Code

#pragma once
template <typename T>
vector<long long> cumulative_sum_diff(const vector<T> &A) {
    int N = A.size();
    vector<long long> S(N + 1);
    for (int i = 0; i < N; ++i) {
        S[i + 1] = S[i];
        if (i & 1) S[i + 1] += abs(A[i] - A[i - 1]);
    }
    return S;
}
#line 2 "library/dp/cumulative_sum/cumulative_sum_diff.hpp"
template <typename T>
vector<long long> cumulative_sum_diff(const vector<T> &A) {
    int N = A.size();
    vector<long long> S(N + 1);
    for (int i = 0; i < N; ++i) {
        S[i + 1] = S[i];
        if (i & 1) S[i + 1] += abs(A[i] - A[i - 1]);
    }
    return S;
}
Back to top page