競技プログラミング用 C++ ライブラリ
data_structure/persistent_queue.hpp · GitHub で見る
共通ページ
過去版からの末尾追加・先頭削除・先頭取得を行います。任意の位置の要素も取得できます。両端の更新や列の分割・連結には 永続 Deque を使います。
#include "lib/data_structure/persistent_queue.hpp"
#include <cassert>
int main() {
PersistentQueue<int> queue;
auto v0 = queue.initial_root();
auto v1 = queue.push(v0, 10);
auto v2 = queue.push(v1, 20);
auto v3 = queue.pop(v2);
auto v4 = queue.push(v1, 30);
assert(queue.front(v3) == 20 && queue.front(v2) == 10);
assert(queue.back(v4) == 30 && queue.get(v4, 1) == 30);
}
各更新は新しい root_type を返し、引数の版は変わりません。initial_root() が最初の版です。ルートをコピーするだけなら時間・追加領域ともに \(O(1)\) で、任意の過去版から分岐できます。同じオブジェクトから得たルートだけを渡してください。版ごとのノード解放は行いません。オブジェクト自体のコピーは全ノードの複製になります。
| 操作 | 内容 | 計算量 |
|---|---|---|
size(root) / empty(root)
|
要素数/空か | \(O(1)\) |
push(root,x) |
x を末尾に追加した版 |
\(O(1)\) |
pop(root) |
先頭を削除した版。非空が必要 | \(O(1)\) |
front(root) |
先頭要素。非空が必要 | \(O(\log u)\) |
back(root) |
末尾要素。非空が必要 | \(O(1)\) |
get(root,p) |
先頭から 0 始まり p 番目の要素 |
\(O(\log u)\) |
get は 0 <= p < size(root) とします。u は空になるまでの同じ分岐上での push 回数です。u <= INT_MAX とし、T はコピー可能な型とします。T のデフォルト構築は不要です。root_type のフィールドは書き換えず、そのまま保存してください。
front_many(roots) は std::vector<root_type> で渡した非空の版それぞれの先頭要素を、指定順の std::vector<T> として返します。同じ版を複数回指定できます。空の版リストには空の列を返します。版の数は INT_MAX 以下とします。版や格納済みのノードは変更しません。
reserve(count) は全版を合わせたノード数を予約します。node_count() は作成済みノード数、memory_usage() は予約済みノード領域のバイト数を返します。ルートは再確保後も有効です。値のコピー・演算は定数時間とします。更新の時間には領域確保の償却計算量を含みます。
push は償却 \(O(1)\)、pop・back・size は \(O(1)\)、front・添字アクセスは \(O(\log u)\) 時間です。push 1 回につき 1 ノードを追加します。他の操作はノードを作りません。過去版から何度分岐してもこれらの計算量を保ちます。空または 1 ノードの履歴に対する操作は定数時間です。
作成済みノード数を \(N\)、指定した版の数を \(Q\) とすると、front_many は \(O(N+Q)\) 時間・追加空間です。
#ifndef PERSISTENT_QUEUE_HPP
#define PERSISTENT_QUEUE_HPP
#include "persistent_stack.hpp"
#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>
template <class T>
class PersistentQueue {
PersistentStack<T> stack;
public:
using value_type = T;
struct root_type {
typename PersistentStack<T>::root_type tail = 0;
int length = 0;
bool operator==(const root_type &) const = default;
};
root_type initial_root() const { return {}; }
int size(root_type root) const { return root.length; }
bool empty(root_type root) const { return size(root) == 0; }
root_type push(root_type root, T value) { return {stack.push(root.tail, std::move(value)), root.length + 1}; }
root_type pop(root_type root) const {
assert(!empty(root));
return root.length == 1 ? root_type{} : root_type{root.tail, root.length - 1};
}
T front(root_type root) const { assert(!empty(root)); return stack.get(root.tail, root.length - 1); }
T back(root_type root) const { assert(!empty(root)); return stack.top(root.tail); }
T get(root_type root, int p) const { assert(0 <= p && p < size(root)); return stack.get(root.tail, root.length - 1 - p); }
std::vector<T> front_many(const std::vector<root_type> &roots) const {
assert(roots.size() <= std::size_t(std::numeric_limits<int>::max()));
std::uint64_t work = 0;
unsigned limit = std::bit_width(stack.node_count());
for (auto root : roots) { assert(!empty(root)); unsigned distance = unsigned(root.length - 1); work += distance < limit ? distance : limit; }
if (work <= 3 * std::uint64_t(stack.node_count())) {
std::vector<T> result;
result.reserve(roots.size());
for (auto root : roots) result.push_back(front(root));
return result;
}
std::vector<std::pair<typename PersistentStack<T>::root_type, int>> queries;
queries.reserve(roots.size());
for (auto root : roots) { assert(!empty(root)); queries.emplace_back(root.tail, root.length - 1); }
return stack.get_many(queries);
}
void reserve(std::size_t count) { stack.reserve(count); }
std::size_t node_count() const { return stack.node_count(); }
std::size_t memory_usage() const { return stack.memory_usage(); }
};
#endif
#line 1 "data_structure/persistent_queue.hpp"
#line 1 "data_structure/persistent_stack.hpp"
#include <bit>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
template <class T>
class PersistentStack {
public:
using value_type = T;
using root_type = std::uint32_t;
private:
using Stored = std::conditional_t<std::is_trivially_default_constructible_v<T> && std::is_trivially_copyable_v<T>, T, std::optional<T>>;
struct Node { Stored value; root_type next, jump; int size, jump_length; };
std::vector<Node> nodes{{{}, 0, 0, 0, 0}};
void check([[maybe_unused]] root_type root) const { assert(root < nodes.size()); }
const T &value_at(root_type root) const {
if constexpr (std::is_same_v<Stored, T>) return nodes[root].value;
else return *nodes[root].value;
}
public:
root_type initial_root() const { return 0; }
int size(root_type root) const { check(root); return nodes[root].size; }
bool empty(root_type root) const { return size(root) == 0; }
root_type push(root_type root, T value) {
check(root);
assert(size(root) < std::numeric_limits<int>::max() && nodes.size() < std::numeric_limits<root_type>::max());
auto a = nodes[root].jump;
int length = nodes[root].jump_length;
bool combine = length == nodes[a].jump_length;
auto jump = combine ? nodes[a].jump : root;
Node node{std::move(value), root, jump, size(root) + 1, combine ? 2 * length + 1 : 1};
auto result = static_cast<root_type>(nodes.size());
nodes.push_back(std::move(node));
return result;
}
root_type pop(root_type root) const { check(root); assert(root); return nodes[root].next; }
T top(root_type root) const { check(root); assert(root); return value_at(root); }
root_type ancestor(root_type root, int count) const {
check(root); assert(0 <= count && count <= size(root));
if (count == size(root)) return 0;
while (count) {
int length = nodes[root].jump_length;
if (length <= count) count -= length, root = nodes[root].jump;
else --count, root = nodes[root].next;
}
return root;
}
T get(root_type root, int k) const { assert(0 <= k && k < size(root)); return top(ancestor(root, k)); }
std::vector<T> get_many(const std::vector<std::pair<root_type, int>> &queries) const {
assert(queries.size() <= std::size_t(std::numeric_limits<int>::max()));
if (queries.empty()) return {};
std::uint64_t work = 0;
unsigned limit = std::bit_width(nodes.size());
for (auto [root, k] : queries) { check(root); assert(0 <= k && k < size(root)); work += unsigned(k) < limit ? unsigned(k) : limit; }
if (work <= 3 * std::uint64_t(nodes.size())) {
std::vector<T> result;
result.reserve(queries.size());
for (auto [root, k] : queries) result.push_back(get(root, k));
return result;
}
std::vector<root_type> first(nodes.size()), next(nodes.size()), selected(queries.size()), path;
std::vector<int> head(nodes.size(), -1), following(queries.size());
for (root_type v = 1; v < nodes.size(); ++v) {
root_type p = nodes[v].next;
next[v] = first[p]; first[p] = v;
}
for (int i = 0; i < int(queries.size()); ++i) {
root_type root = queries[i].first;
following[i] = head[root]; head[root] = i;
}
for (root_type v = first[0]; v;) {
path.push_back(v);
for (int i = head[v]; i != -1; i = following[i]) selected[i] = path[path.size() - 1 - queries[i].second];
if (first[v]) { v = first[v]; continue; }
while (v && !next[v]) { path.pop_back(); v = nodes[v].next; }
if (v) { path.pop_back(); v = next[v]; }
}
std::vector<T> result;
result.reserve(queries.size());
for (root_type v : selected) result.push_back(value_at(v));
return result;
}
void reserve(std::size_t count) { assert(count < std::numeric_limits<root_type>::max()); nodes.reserve(count + 1); }
std::size_t node_count() const { return nodes.size() - 1; }
std::size_t memory_usage() const { return nodes.capacity() * sizeof(Node); }
};
#line 9 "data_structure/persistent_queue.hpp"
template <class T>
class PersistentQueue {
PersistentStack<T> stack;
public:
using value_type = T;
struct root_type {
typename PersistentStack<T>::root_type tail = 0;
int length = 0;
bool operator==(const root_type &) const = default;
};
root_type initial_root() const { return {}; }
int size(root_type root) const { return root.length; }
bool empty(root_type root) const { return size(root) == 0; }
root_type push(root_type root, T value) { return {stack.push(root.tail, std::move(value)), root.length + 1}; }
root_type pop(root_type root) const {
assert(!empty(root));
return root.length == 1 ? root_type{} : root_type{root.tail, root.length - 1};
}
T front(root_type root) const { assert(!empty(root)); return stack.get(root.tail, root.length - 1); }
T back(root_type root) const { assert(!empty(root)); return stack.top(root.tail); }
T get(root_type root, int p) const { assert(0 <= p && p < size(root)); return stack.get(root.tail, root.length - 1 - p); }
std::vector<T> front_many(const std::vector<root_type> &roots) const {
assert(roots.size() <= std::size_t(std::numeric_limits<int>::max()));
std::uint64_t work = 0;
unsigned limit = std::bit_width(stack.node_count());
for (auto root : roots) { assert(!empty(root)); unsigned distance = unsigned(root.length - 1); work += distance < limit ? distance : limit; }
if (work <= 3 * std::uint64_t(stack.node_count())) {
std::vector<T> result;
result.reserve(roots.size());
for (auto root : roots) result.push_back(front(root));
return result;
}
std::vector<std::pair<typename PersistentStack<T>::root_type, int>> queries;
queries.reserve(roots.size());
for (auto root : roots) { assert(!empty(root)); queries.emplace_back(root.tail, root.length - 1); }
return stack.get_many(queries);
}
void reserve(std::size_t count) { stack.reserve(count); }
std::size_t node_count() const { return stack.node_count(); }
std::size_t memory_usage() const { return stack.memory_usage(); }
};