競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
各IDの優先度を更新できるヒープです。一つのIDにつき一つの優先度を保持します。Key は整数型です。Largest を true にすると大きい優先度から取り出せます。同じ優先度では小さいIDが先です。
IndexedPriorityQueueexplicit IndexedPriorityQueue(std::size_t id_limit);
IDを 0 以上 id_limit 未満に指定します。空で構築する時間と領域は O(id_limit+1) です。Key の既定値は int、Largest は false です。
setvoid set(std::size_t id, Key value);
未登録のIDを追加するか、登録済みの優先度を置き換えます。登録数を n として O(log(n+1)) 時間です。
erasebool erase(std::size_t id);
IDを削除し、削除できた場合に true を返します。O(log(n+1)) 時間です。
topstd::pair<std::size_t, Key> top() const;
先頭のIDと優先度を返します。O(1) 時間です。top() と pop() は非空が前提です。
popstd::pair<std::size_t, Key> pop();
先頭のIDと優先度を取り出します。O(log(n+1)) 時間です。
priorityKey priority(std::size_t id) const;
登録済みの優先度を返します。O(1) 時間です。
containsbool contains(std::size_t id) const;
IDが登録されているかを判定します。O(1) 時間です。
id_limit()、size()、empty() はIDの上限、登録数、空判定です。各 O(1) 時間です。clear() は登録をすべて取り除きます。O(n) 時間です。検査有効時は範囲外のID、空の top() と pop()、未登録の priority() で std::out_of_range を送出します。
sirkit::IndexedPriorityQueue<int> queue(5);
queue.set(2, 7);
queue.set(4, 4);
queue.set(2, 1);
const auto [id, priority] = queue.pop();
assert(id == 2 && priority == 1);
IndexedPriorityQueue<Key,Largest>(id_limit): IDが0以上id_limit未満の空のキュー。O(id_limit+1)。
Keyは整数型。Largestがfalseなら最小、trueなら最大を先に取り出す。同じ優先度は小さいIDを先に扱う。
set(id,priority): 未登録なら追加、登録済みなら優先度を更新。O(log(n+1))。
erase(id): 削除できたかを返す。O(log(n+1))。
top()/pop(): {ID,優先度}の取得・取り出し。O(1)/O(log(n+1))。
priority(id)/contains(id): 登録済みの優先度・登録済みか。各O(1)。
id_limit()/size()/empty(): ID上限・登録数・空判定。O(1)。clear(): 全削除。O(n)。
nは登録数。領域O(id_limit+1)。入力検査有効時は範囲外のID、空のtop/pop、未登録のpriorityでout_of_range。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Shortest Path(ID付き優先度付きキュー) | indexed_priority_queue_shortest_path.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 未測定 |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/indexed_priority_queue.md
//
// API一覧
// IndexedPriorityQueue<Key,Largest>(id_limit): IDが0以上id_limit未満の空のキュー。O(id_limit+1)。
// Keyは整数型。Largestがfalseなら最小、trueなら最大を先に取り出す。同じ優先度は小さいIDを先に扱う。
// set(id,priority): 未登録なら追加、登録済みなら優先度を更新。O(log(n+1))。
// erase(id): 削除できたかを返す。O(log(n+1))。
// top()/pop(): {ID,優先度}の取得・取り出し。O(1)/O(log(n+1))。
// priority(id)/contains(id): 登録済みの優先度・登録済みか。各O(1)。
// id_limit()/size()/empty(): ID上限・登録数・空判定。O(1)。clear(): 全削除。O(n)。
// nは登録数。領域O(id_limit+1)。入力検査有効時は範囲外のID、空のtop/pop、未登録のpriorityでout_of_range。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <concepts>
#include <cstddef>
#include <limits>
#include <stdexcept>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_INDEXED_PRIORITY_QUEUE_DEFINED
#define SIRKIT_INDEXED_PRIORITY_QUEUE_DEFINED
template <std::integral Key = int, bool Largest = false>
class IndexedPriorityQueue
{
public:
explicit IndexedPriorityQueue(std::size_t id_limit)
: positions_(id_limit, kMissing), keys_(id_limit)
{
heap_.reserve(id_limit);
}
IndexedPriorityQueue(const IndexedPriorityQueue &other)
: positions_(other.positions_), keys_(other.keys_), heap_(other.heap_)
{
heap_.reserve(id_limit());
}
IndexedPriorityQueue(IndexedPriorityQueue &&) noexcept = default;
IndexedPriorityQueue &operator=(IndexedPriorityQueue other) noexcept
{
positions_.swap(other.positions_);
keys_.swap(other.keys_);
heap_.swap(other.heap_);
return *this;
}
void set(std::size_t id, Key value)
{
CheckId(id);
auto position = positions_[id];
if (position == kMissing)
{
position = heap_.size();
heap_.push_back(id);
positions_[id] = position;
keys_[id] = value;
Swim(position);
return;
}
const Key previous = keys_[id];
keys_[id] = value;
if (Precedes(value, previous))
{
Swim(position);
}
else
{
Sink(position);
}
}
bool erase(std::size_t id)
{
CheckId(id);
if (positions_[id] == kMissing)
{
return false;
}
Remove(id);
return true;
}
[[nodiscard]] std::pair<std::size_t, Key> top() const
{
#ifndef SKIP_RUNTIME_CHECKS
if (empty())
{
throw std::out_of_range("IndexedPriorityQueue: empty queue");
}
#endif
const auto id = heap_.front();
return {id, static_cast<Key>(keys_[id])};
}
[[nodiscard]] std::pair<std::size_t, Key> pop()
{
const auto result = top();
Remove(result.first);
return result;
}
[[nodiscard]] Key priority(std::size_t id) const
{
CheckId(id);
#ifndef SKIP_RUNTIME_CHECKS
if (positions_[id] == kMissing)
{
throw std::out_of_range("IndexedPriorityQueue: inactive ID");
}
#endif
return keys_[id];
}
[[nodiscard]] bool contains(std::size_t id) const
{
CheckId(id);
return positions_[id] != kMissing;
}
[[nodiscard]] std::size_t id_limit() const noexcept
{
return positions_.size();
}
[[nodiscard]] std::size_t size() const noexcept
{
return heap_.size();
}
[[nodiscard]] bool empty() const noexcept
{
return heap_.empty();
}
void clear() noexcept
{
for (const auto id : heap_)
{
positions_[id] = kMissing;
}
heap_.clear();
}
private:
static constexpr std::size_t kMissing = std::numeric_limits<std::size_t>::max();
[[nodiscard]] static bool Precedes(Key lhs, Key rhs) noexcept
{
if constexpr (Largest)
{
return lhs > rhs;
}
else
{
return lhs < rhs;
}
}
[[nodiscard]] bool Earlier(std::size_t lhs, std::size_t rhs) const noexcept
{
const Key left = keys_[lhs], right = keys_[rhs];
return left == right ? lhs < rhs : Precedes(left, right);
}
void CheckId([[maybe_unused]] std::size_t id) const
{
#ifndef SKIP_RUNTIME_CHECKS
if (id >= id_limit())
{
throw std::out_of_range("IndexedPriorityQueue: ID out of bounds");
}
#endif
}
void SwapEntries(std::size_t a, std::size_t b) noexcept
{
std::swap(heap_[a], heap_[b]);
positions_[heap_[a]] = a;
positions_[heap_[b]] = b;
}
void Swim(std::size_t at) noexcept
{
while (at != 0)
{
const auto parent = (at - 1) / 2;
if (!Earlier(heap_[at], heap_[parent]))
{
break;
}
SwapEntries(at, parent);
at = parent;
}
}
void Sink(std::size_t at) noexcept
{
while (at < heap_.size() / 2)
{
auto child = 2 * at + 1;
if (child + 1 < heap_.size() && Earlier(heap_[child + 1], heap_[child]))
{
++child;
}
if (!Earlier(heap_[child], heap_[at]))
{
break;
}
SwapEntries(at, child);
at = child;
}
}
void Remove(std::size_t id) noexcept
{
const auto at = positions_[id];
const auto last = heap_.back();
heap_.pop_back();
positions_[id] = kMissing;
if (at == heap_.size())
{
return;
}
heap_[at] = last;
positions_[last] = at;
if (at != 0 && Earlier(last, heap_[(at - 1) / 2]))
{
Swim(at);
}
else
{
Sink(at);
}
}
std::vector<std::size_t> positions_;
std::vector<Key> keys_;
std::vector<std::size_t> heap_;
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1