serna37's Library

Logo

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

View the Project on GitHub serna37/library-cpp

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

二項差での累積和 反転

できること

計算量

$O(N)$

使い方

vector<long long> R = cumulative_sum_rev_diff(A);

Verified with

Code

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