C++ アルゴリズムとデータ構造のライブラリ
#include "library/dp/cumulative_sum/cumulative_sum_diff.hpp"A: 1 4 2 8 2 9
増分 --> 3 --> 6 --> 7 -->
S: 0 0 3 3 9 9 16
$O(N)$
vector<long long> S = cumulative_sum_diff(A);
#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;
}