競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
整数列の連続区間に同じ値を足しながら、隣り合う2値の差の絶対値をすべて合計した値を求めます。
RangeAddTotalVariation(values)std::int64_t の列から構築します。列長を $n$ とすると $O(n+1)$ 時間・領域です。
range_add(left, right, delta)0始まりの半開区間 [left, right) に delta を足します。変わる隣接差は区間の両端だけなので $O(1)$ です。
total_variation()隣接差の絶対値和を std::uint64_t で返します。構築時と各更新後の合計をこの型に収めてください。$O(1)$ です。
size()列の長さを $O(1)$ で返します。
const std::array<std::int64_t, 3> values{1, 2, 3};
sirkit::RangeAddTotalVariation variation(values);
variation.range_add(1, 3, 1);
assert(variation.total_variation() == 3);
RangeAddTotalVariation(values={}): int64_t列から構築。O(n+1)時間・領域。
range_add(left,right,delta): 0始まりの半開区間にint64_tのdeltaを加える。O(1)。
total_variation() const: 隣り合う値の差の絶対値の合計をuint64_tで返す。O(1)。
size() const: 列長。O(1)。コピーは独立、move元の列長は0。
初期化時と各加算後に隣接差の絶対値和がuint64_tに収まること。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Uplift | range_add_total_variation.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(隣接差の絶対値和): https://atcoder.jp/contests/typical90/tasks/typical90_bl
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/range_add_total_variation.md
//
// API一覧
// RangeAddTotalVariation(values={}): int64_t列から構築。O(n+1)時間・領域。
// range_add(left,right,delta): 0始まりの半開区間にint64_tのdeltaを加える。O(1)。
// total_variation() const: 隣り合う値の差の絶対値の合計をuint64_tで返す。O(1)。
// size() const: 列長。O(1)。コピーは独立、move元の列長は0。
// 初期化時と各加算後に隣接差の絶対値和がuint64_tに収まること。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <cstdint>
#include <limits>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_RANGE_ADD_TOTAL_VARIATION_DEFINED
#define SIRKIT_RANGE_ADD_TOTAL_VARIATION_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/
{
struct VariationDifference
{
std::uint64_t magnitude = 0;
bool negative = false;
};
inline std::uint64_t AddVariationMagnitude(std::uint64_t a, std::uint64_t b)
{
#ifndef SKIP_RUNTIME_CHECKS
if (b > std::numeric_limits<std::uint64_t>::max() - a)
{
throw std::overflow_error("RangeAddTotalVariation: variation overflow");
}
#endif
return a + b;
}
inline VariationDifference VariationBetween(std::int64_t left, std::int64_t right) noexcept
{
const auto a = static_cast<std::uint64_t>(left);
const auto b = static_cast<std::uint64_t>(right);
return left <= right ? VariationDifference{b - a, false} : VariationDifference{a - b, true};
}
inline VariationDifference ShiftVariationDifference(VariationDifference before, VariationDifference step)
{
if (before.negative == step.negative)
{
return {AddVariationMagnitude(before.magnitude, step.magnitude), before.negative};
}
if (before.magnitude < step.magnitude)
{
return {step.magnitude - before.magnitude, step.negative};
}
return {before.magnitude - step.magnitude, before.negative};
}
} // namespace reinsirk::detail
class RangeAddTotalVariation
{
using Difference = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::VariationDifference;
std::vector<Difference> differences_;
std::size_t size_ = 0;
std::uint64_t variation_ = 0;
public:
explicit RangeAddTotalVariation(std::span<const std::int64_t> values = {}) : size_(values.size())
{
differences_.reserve(size_ == 0 ? 0 : size_ - 1);
for (std::size_t i = 1; i < size_; ++i)
{
const auto difference = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::VariationBetween(values[i - 1], values[i]);
variation_ = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AddVariationMagnitude(variation_, difference.magnitude);
differences_.push_back(difference);
}
}
RangeAddTotalVariation(const RangeAddTotalVariation &) = default;
RangeAddTotalVariation(RangeAddTotalVariation &&other) noexcept
: differences_(std::move(other.differences_)), size_(std::exchange(other.size_, 0)),
variation_(std::exchange(other.variation_, 0))
{
}
RangeAddTotalVariation &operator=(RangeAddTotalVariation other) noexcept
{
differences_.swap(other.differences_);
std::swap(size_, other.size_);
std::swap(variation_, other.variation_);
return *this;
}
[[nodiscard]] std::size_t size() const noexcept
{
return size_;
}
[[nodiscard]] std::uint64_t total_variation() const noexcept
{
return variation_;
}
void range_add(std::size_t left, std::size_t right, std::int64_t delta)
{
#ifndef SKIP_RUNTIME_CHECKS
if (left > right || right > size_)
{
throw std::out_of_range("RangeAddTotalVariation: invalid range");
}
#endif
if (left == right || delta == 0)
{
return;
}
const auto step = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::VariationBetween(0, delta);
const bool has_left = left != 0;
const bool has_right = right != size_;
const Difference old_left = has_left ? differences_[left - 1] : Difference{};
const Difference old_right = has_right ? differences_[right - 1] : Difference{};
const Difference next_left = has_left ? /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::ShiftVariationDifference(old_left, step) : Difference{};
const Difference next_right = has_right
? /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::ShiftVariationDifference(old_right, {step.magnitude, !step.negative}) : Difference{};
// 変化する両端を計算してから更新する。合計も古い両端を外してから足し直す。
auto next = variation_ - old_left.magnitude - old_right.magnitude;
next = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AddVariationMagnitude(next, next_left.magnitude);
next = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AddVariationMagnitude(next, next_right.magnitude);
if (has_left)
{
differences_[left - 1] = next_left;
}
if (has_right)
{
differences_[right - 1] = next_right;
}
variation_ = next;
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1