競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
固定した座標の列をもとに、指定座標から何番目に近い点かを調べます。同じ座標にある点も別々に数えます。
NearestValueIndexNearestValueIndex();
explicit NearestValueIndex(std::span<const std::int64_t> values);
長さ $n$ の座標列をコピーして整列します。構築は $O(n\log(n+1)+1)$ 時間・$O(n+1)$ 領域です。
size点の数を $O(1)$ 時間で返します。
empty点がないかを $O(1)$ 時間で返します。
kth_neareststd::optional<NearestValue> kth_nearest(std::int64_t center, std::size_t rank) const;
center から近い順に並べ、1始まりの rank 番目を返します。同距離なら座標が小さい方を先に数えます。範囲外の順位では std::nullopt です。$O(\log(n+1)+1)$ 時間で、中心の左右にある2つの距離列を二分探索します。
NearestValuevalue は点の座標、distance は中心からの距離です。座標は std::int64_t、距離は両端の差も扱える std::uint64_t です。
count_withinstd::size_t count_within(std::int64_t center, std::uint64_t radius) const;
center からの距離が radius 以下の点を $O(\log(n+1)+1)$ 時間で数えます。
const std::array<std::int64_t, 4> values{-3, -1, 5, 6};
const sirkit::NearestValueIndex points(values);
const auto third = points.kth_nearest(-2, 3);
assert(third && third->distance == 7);
assert(points.count_within(-2, 1) == 2);
NearestValueIndex(values): int64_tの座標を保持。重複も個別に数える。O(n*log(n+1)+1)時間・O(n+1)領域。
size()/empty(): 座標数/空か。O(1)。空構築可。
kth_nearest(center,k): 距離順で1始まりk番目のNearestValue{value,distance}。同距離なら座標の小さい順。
k=0またはk>nではnullopt。O(log(n+1)+1)時間。distanceはuint64_t。
count_within(center,radius): 距離がradius以下の座標数。O(log(n+1)+1)時間。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| K-th Nearest | nearest_value_index.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(最近順位の用途): https://atcoder.jp/contests/abc364/tasks/abc364_d
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/nearest_value_index.md
//
// API一覧
// NearestValueIndex(values): int64_tの座標を保持。重複も個別に数える。O(n*log(n+1)+1)時間・O(n+1)領域。
// size()/empty(): 座標数/空か。O(1)。空構築可。
// kth_nearest(center,k): 距離順で1始まりk番目のNearestValue{value,distance}。同距離なら座標の小さい順。
// k=0またはk>nではnullopt。O(log(n+1)+1)時間。distanceはuint64_t。
// count_within(center,radius): 距離がradius以下の座標数。O(log(n+1)+1)時間。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <numeric>
#include <optional>
#include <span>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_NEAREST_VALUE_INDEX_DEFINED
#define SIRKIT_NEAREST_VALUE_INDEX_DEFINED
struct NearestValue
{
std::int64_t value = 0;
std::uint64_t distance = 0;
};
class NearestValueIndex
{
static constexpr std::uint64_t kSignBit = std::uint64_t{1} << 63;
std::vector<std::uint64_t> points_;
static constexpr std::uint64_t order_key(std::int64_t value) noexcept
{
return static_cast<std::uint64_t>(value) ^ kSignBit;
}
[[nodiscard]] NearestValue describe(std::size_t index, std::uint64_t center) const noexcept
{
const auto value = points_[index];
return {
static_cast<std::int64_t>(value ^ kSignBit),
std::max(center, value) - std::min(center, value),
};
}
public:
NearestValueIndex() = default;
explicit NearestValueIndex(std::span<const std::int64_t> values)
{
points_.reserve(values.size());
for (const auto value : values)
{
points_.push_back(order_key(value));
}
std::sort(points_.begin(), points_.end());
}
[[nodiscard]] std::size_t size() const noexcept
{
return points_.size();
}
[[nodiscard]] bool empty() const noexcept
{
return points_.empty();
}
[[nodiscard]] std::optional<NearestValue> kth_nearest(std::int64_t center, std::size_t rank) const noexcept
{
if (rank == 0 || rank > size())
{
return std::nullopt;
}
const auto key = order_key(center);
const auto split = static_cast<std::size_t>(std::lower_bound(points_.begin(), points_.end(), key) - points_.begin());
const auto right_size = size() - split;
auto low = rank > right_size ? rank - right_size : 0;
auto high = std::min(rank, split);
// 左側は手前から逆順、右側は順にたどると、どちらの距離列も昇順になる。
// 左側の次候補が右側の採用末尾以下なら、同距離のときも左側を優先する。
while (low < high)
{
const auto take_left = std::midpoint(low, high);
const auto take_right = rank - take_left;
const auto next_left = key - points_[split - take_left - 1];
const auto last_right = points_[split + take_right - 1] - key;
if (next_left <= last_right)
{
low = take_left + 1;
}
else
{
high = take_left;
}
}
if (low == 0)
{
return describe(split + rank - 1, key);
}
const auto last_left = describe(split - low, key);
if (low == rank)
{
return last_left;
}
const auto last_right = describe(split + (rank - low) - 1, key);
return last_left.distance > last_right.distance ? last_left : last_right;
}
[[nodiscard]] std::size_t count_within(std::int64_t center, std::uint64_t radius) const noexcept
{
constexpr auto kLastKey = std::numeric_limits<std::uint64_t>::max();
const auto key = order_key(center);
const auto low = key - std::min(radius, key);
const auto high = key + std::min(radius, kLastKey - key);
const auto first = std::lower_bound(points_.begin(), points_.end(), low);
return static_cast<std::size_t>(std::upper_bound(first, points_.end(), high) - first);
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1