sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

加算と上下限の合成

Algorithm/Clamped_Translation.cpp · ソース · 文書 Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

加算、下限の適用、上限の適用を並べた関数を、合計の加算量と上下限で保持します。同じ操作列を多数の値に適用するときに使います。

ClampedTranslation()

何も変更しない恒等関数を作ります。

then_add(delta)

現在の出力に delta を加える操作を追加します。

then_chmax(low)

現在の出力が low 未満なら、low に置き換える操作を追加します。

then_chmin(high)

現在の出力が high を超えていれば、high に置き換える操作を追加します。

then(after)

現在の関数を適用し、その結果に after を適用する新しい関数を返します。元の状態は更新しません。

apply(x)

合成した関数に x を入れた結果を返します。

各操作は $O(1)$ 時間・領域です。有限の上下限、加算量の合計、返り値は std::int64_t に収めてください。上下限の適用で返り値が収まる場合は、途中の加算が型の範囲を超えても構いません。

使用例

sirkit::ClampedTranslation filter;
filter.then_add(3).then_chmax(0).then_chmin(10);
assert(filter.apply(-6) == 0);
assert(filter.apply(1) == 4);
assert(filter.apply(20) == 10);

実装・使用例の全体・Verify

ソースのAPI注記
ClampedTranslation(): 恒等関数。
then_add(delta): 最後にx+deltaを追加する。
then_chmax(low): 最後にmax(x,low)を追加する。
then_chmin(high): 最後にmin(x,high)を追加する。
then(after) const: この写像を適用した後にafterを適用する新しい写像を返す。
apply(x) const: 合成した写像の値。
すべてO(1)時間・領域。有限の境界、合計の加算量、返り値はint64_tに収まること。
最終結果を境界内に丸められる場合、x+deltaの途中の範囲超過は許される。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Filters clamped_translation.cpp 未提出 —

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 無し
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考(操作の合成): https://atcoder.jp/contests/abc196/tasks/abc196_e
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/clamped_translation.md
//
// API一覧
// ClampedTranslation(): 恒等関数。
// then_add(delta): 最後にx+deltaを追加する。
// then_chmax(low): 最後にmax(x,low)を追加する。
// then_chmin(high): 最後にmin(x,high)を追加する。
// then(after) const: この写像を適用した後にafterを適用する新しい写像を返す。
// apply(x) const: 合成した写像の値。
// すべてO(1)時間・領域。有限の境界、合計の加算量、返り値はint64_tに収まること。
// 最終結果を境界内に丸められる場合、x+deltaの途中の範囲超過は許される。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstdint>
#include <limits>
#include <optional>
#include <stdexcept>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_CLAMPED_TRANSLATION_DEFINED
#define SIRKIT_CLAMPED_TRANSLATION_DEFINED

class ClampedTranslation
{
    using Value = std::int64_t;
    using Bound = std::optional<Value>;

    Value shift_ = 0;
    Bound lower_;
    Bound upper_;

    ClampedTranslation(Value shift, Bound lower, Bound upper) noexcept
        : shift_(shift), lower_(lower), upper_(upper)
    {
    }

    static Value evaluate(Value value, Value shift, Bound below, Bound above)
    {
        constexpr Value kMin = std::numeric_limits<Value>::min();
        constexpr Value kMax = std::numeric_limits<Value>::max();
        if (shift > 0 && value > kMax - shift)
        {
            if (above)
            {
                return *above;
            }
#ifndef SKIP_RUNTIME_CHECKS
            throw std::overflow_error("ClampedTranslation: value is too large");
#endif
        }
        if (shift < 0 && value < kMin - shift)
        {
            if (below)
            {
                return *below;
            }
#ifndef SKIP_RUNTIME_CHECKS
            throw std::overflow_error("ClampedTranslation: value is too small");
#endif
        }

        // 符号なし加算で表現し、検査を省いた構成でも符号付きオーバーフローを起こさない。
        Value translated = static_cast<Value>(static_cast<std::uint64_t>(value) + static_cast<std::uint64_t>(shift));
        if (below)
        {
            translated = std::max(translated, *below);
        }
        if (above)
        {
            translated = std::min(translated, *above);
        }
        return translated;
    }

public:
    ClampedTranslation() = default;

    [[nodiscard]] Value apply(Value value) const
    {
        return evaluate(value, shift_, lower_, upper_);
    }

    [[nodiscard]] ClampedTranslation then(const ClampedTranslation &after) const
    {
        const Bound lower = lower_ ? Bound{after.apply(*lower_)} : after.lower_;
        const Bound upper = upper_ ? Bound{after.apply(*upper_)} : after.upper_;
        if (lower && lower == upper)
        {
            return {0, lower, upper};
        }
        return {evaluate(shift_, after.shift_, {}, {}), lower, upper};
    }

    ClampedTranslation &then_add(Value difference)
    {
        *this = then({difference, {}, {}});
        return *this;
    }

    ClampedTranslation &then_chmax(Value lower)
    {
        *this = then({0, lower, {}});
        return *this;
    }

    ClampedTranslation &then_chmin(Value upper)
    {
        *this = then({0, {}, upper});
        return *this;
    }
};

#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page