競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
境界順に並べた整数頂点を前処理し、2頂点を弦で結んで切り出した部分の面積と重心を求めます。座標の絶対値は $10^9$ 以下です。本体
| 操作 | 内容 | 時間 |
| — | — | — |
| PolygonChordMoments(points) | 凸多角形の頂点列を前処理する | $O(n)$ |
| slice(from, to) | from から入力順に to まで進み、弦で閉じた部分を求める | $O(1)$ |
| total() | 多角形全体の面積と重心を求める | $O(1)$ |
頂点は0始まりです。反時計回りの入力なら slice は弦を from から to に見た右側を返します。時計回りなら左側です。結果の signed_area2 は符号付き面積の2倍で、centroid は double の座標対です。面積が0のときは std::nullopt になります。前処理領域は $O(n)$ です。
const std::array<sirkit::PolygonChordMoments::Point, 4> square{
{{0, 0}, {6, 0}, {6, 6}, {0, 6}}};
const sirkit::PolygonChordMoments polygon(square);
assert(polygon.slice(0, 2).centroid == (std::pair{4., 2.}));
PolygonChordMoments(points): 境界順の整数頂点を前処理。O(n)時間・領域。 slice(from,to): fromからtoまで入力の順に進み、to→fromの辺で閉じる部分の符号付き面積・重心。O(1)。 total(): 元の多角形の符号付き面積・重心。O(1)。size(): 頂点数。O(1)。 結果のsigned_area2は符号付き面積の2倍(正確なint64_t)、centroidは重心座標。面積0はnullopt。 頂点はpair<int64_t,int64_t>で|座標|<=10^9。凸多角形を周回順に渡す。時計回りも可。 slice(i,i)は面積0。最後の頂点に先頭を再掲しない。丸めは重心計算時のみ。 コピーはO(1)で前処理を共有。移動元は空。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| ABC472 F - Centroid of a Slice | polygon_chord_moments.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考: https://atcoder.jp/contests/abc472/editorial/24789
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/polygon_chord_moments.md
//
// API一覧
// PolygonChordMoments(points): 境界順の整数頂点を前処理。O(n)時間・領域。
// slice(from,to): fromからtoまで入力の順に進み、to→fromの辺で閉じる部分の符号付き面積・重心。O(1)。
// total(): 元の多角形の符号付き面積・重心。O(1)。size(): 頂点数。O(1)。
// 結果のsigned_area2は符号付き面積の2倍(正確なint64_t)、centroidは重心座標。面積0はnullopt。
// 頂点はpair<int64_t,int64_t>で|座標|<=10^9。凸多角形を周回順に渡す。時計回りも可。
// slice(i,i)は面積0。最後の頂点に先頭を再掲しない。丸めは重心計算時のみ。
// コピーはO(1)で前処理を共有。移動元は空。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <bit>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <span>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_POLYGON_CHORD_MOMENTS_DEFINED
#define SIRKIT_POLYGON_CHORD_MOMENTS_DEFINED
namespace detail
{
struct PolygonMomentInteger
{
std::uint64_t low = 0;
std::uint64_t high = 0;
PolygonMomentInteger operator+(const PolygonMomentInteger &other) const noexcept
{
const auto sum = low + other.low;
return {sum, high + other.high + static_cast<unsigned>(sum < low)};
}
PolygonMomentInteger operator-(const PolygonMomentInteger &other) const noexcept
{
return {low - other.low, high - other.high - static_cast<unsigned>(low < other.low)};
}
static PolygonMomentInteger FromSigned(std::int64_t value) noexcept
{
return {static_cast<std::uint64_t>(value),
value < 0 ? std::numeric_limits<std::uint64_t>::max() : 0};
}
static PolygonMomentInteger Product(std::int64_t a, std::int64_t b) noexcept
{
const auto x = Magnitude(a);
const auto y = Magnitude(b); // |b| <= 2e9
const auto low_product = (x & 0xffffffffULL) * y;
const auto high_product = (x >> 32) * y;
const auto product = PolygonMomentInteger{low_product, 0} +
PolygonMomentInteger{high_product << 32, high_product >> 32};
return (a < 0) == (b < 0) ? product : PolygonMomentInteger{} - product;
}
std::int64_t ToSigned() const
{
const auto value = std::bit_cast<std::int64_t>(low);
if (high != FromSigned(value).high)
{
throw std::overflow_error("PolygonChordMoments: twice area outside int64");
}
return value;
}
double ToDouble() const noexcept
{
const bool negative = (high >> 63) != 0;
const auto magnitude = negative ? PolygonMomentInteger{} - *this : *this;
const double result = std::ldexp(static_cast<double>(magnitude.high), 64) +
static_cast<double>(magnitude.low);
return negative ? -result : result;
}
private:
static std::uint64_t Magnitude(std::int64_t value) noexcept
{
const auto bits = static_cast<std::uint64_t>(value);
return value < 0 ? std::uint64_t{0} - bits : bits;
}
};
}
class PolygonChordMoments
{
public:
using Point = std::pair<std::int64_t, std::int64_t>;
struct Result
{
std::int64_t signed_area2;
std::optional<std::pair<double, double>> centroid;
};
explicit PolygonChordMoments(std::span<const Point> points)
: state_(std::make_shared<const Storage>(points))
{
}
[[nodiscard]] std::size_t size() const noexcept
{
return state_ ? state_->points.size() : 0;
}
[[nodiscard]] Result total() const
{
return state_ ? Summarize(state_->prefix.back()) : Result{0, std::nullopt};
}
[[nodiscard]] Result slice(std::size_t from, std::size_t to) const
{
#ifndef SKIP_RUNTIME_CHECKS
if (from >= size() || to >= size())
{
throw std::out_of_range("PolygonChordMoments: vertex index");
}
#endif
const auto &prefix = state_->prefix;
auto moments = prefix[to] - prefix[from];
if (from > to)
{
moments = moments + prefix.back();
}
return Summarize(moments + EdgeMoments(state_->points[to], state_->points[from]));
}
private:
using Integer = detail::PolygonMomentInteger;
struct Moments
{
Integer area;
Integer x;
Integer y;
Moments operator+(const Moments &other) const noexcept
{
return {area + other.area, x + other.x, y + other.y};
}
Moments operator-(const Moments &other) const noexcept
{
return {area - other.area, x - other.x, y - other.y};
}
};
struct Storage
{
std::vector<Point> points;
std::vector<Moments> prefix;
explicit Storage(std::span<const Point> input) : points(input.begin(), input.end())
{
for (const auto &[x, y] : points)
{
CheckCoordinate(x);
CheckCoordinate(y);
}
prefix.reserve(points.size() + 1);
prefix.emplace_back();
for (std::size_t to = 1; to < points.size(); ++to)
{
prefix.push_back(prefix.back() + EdgeMoments(points[to - 1], points[to]));
}
if (!points.empty())
{
prefix.push_back(prefix.back() + EdgeMoments(points.back(), points.front()));
}
}
};
std::shared_ptr<const Storage> state_;
static void CheckCoordinate(std::int64_t value)
{
constexpr std::int64_t kLimit = 1000000000;
if (value < -kLimit || value > kLimit)
{
throw std::out_of_range("PolygonChordMoments: coordinate magnitude");
}
}
static Moments EdgeMoments(const Point &from, const Point &to) noexcept
{
const auto cross = from.first * to.second - from.second * to.first;
return {Integer::FromSigned(cross),
Integer::Product(cross, from.first + to.first),
Integer::Product(cross, from.second + to.second)};
}
static Result Summarize(const Moments &moments)
{
const auto area = moments.area.ToSigned();
if (area == 0)
{
return {0, std::nullopt};
}
const double divisor = 3 * static_cast<double>(area);
return {area, std::pair{moments.x.ToDouble() / divisor, moments.y.ToDouble() / divisor}};
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1