shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: 永続キュー
(src/data_structures/persistent-queue.hpp)

過去の版を残したまま更新できるキューです。push・pop は元の版を変えずに新しい版を返します。

PersistentQueue<int> q0;
auto q1 = q0.push(1).push(2);   // q0 はそのまま
q1.front();                     // 1
auto q2 = q1.pop();
q2.front();                     // 2
q1.front();                     // 1 (q1 もそのまま)

// 版を配列に並べる (クエリ i の後の版を que[i + 1] に置く)
std::vector<PersistentQueue<int>> que(q + 1);
que[i + 1] = que[t + 1].push(x);

要素を木の根から末尾へのパスとして持ちます。pop は要素数を減らすだけで、front は末尾から祖先をたどって求めます。祖先へのジャンプポインタは skew binary の長さで張るので、ノード 1 つあたりのメモリは $O(1)$ で、祖先は $O(\log n)$ でたどれます。

同じキューから作った版はノードを共有します。ノードは削除しないので、メモリは push の回数に比例して増えます。

メンバ関数

関数 説明 計算量
PersistentQueue<T>() 空のキュー $O(1)$
size() 要素数 $O(1)$
empty() 空か $O(1)$
front() 先頭の要素 $O(\log n)$
back() 末尾の要素 $O(1)$
push(x) 末尾に x を加えた新しい版を返す $O(1)$
pop() 先頭を取り除いた新しい版を返す $O(1)$
to_vector() すべての要素を先頭から順に並べた std::vector<T> $O(n)$

$n$ は、これまでに push した回数です。

関連

Verified with

Code

#pragma once

/**
 * @file persistent-queue.hpp
 * @brief 永続キュー
 */

#include <cassert>
#include <memory>
#include <utility>
#include <vector>

/**
 * @brief 永続キュー
 *
 * 更新すると新しい版を返し、元の版は変わらない。同じキューから作った版はノードを共有する
 * 要素を木の根から末尾へのパスとして持ち、先頭はジャンプポインタで祖先をたどって求める
 * 例:
 * PersistentQueue<int> q0;
 * auto q1 = q0.push(1).push(2);  // q0 はそのまま
 * q1.front();                    // 1
 * q1.pop().front();              // 2
 *
 * @tparam T 要素の型
 */
template <typename T>
class PersistentQueue {
    // すべての版のノードを持つ。ノードは削除しない。ノード 0 は根 (要素を持たない)
    struct Pool {
        std::vector<T> val;
        std::vector<int> parent, jump, depth;
        Pool() : val(1), parent{0}, jump{0}, depth{0} {}
        int make_node(const T& x, int p) {
            // ジャンプの長さが 1, 1, 3, 1, 1, 3, 7, ... (skew binary) になるように張る
            int j = jump[p];
            int nj = depth[p] - depth[j] == depth[j] - depth[jump[j]] ? jump[j] : p;
            val.push_back(x);
            parent.push_back(p);
            jump.push_back(nj);
            depth.push_back(depth[p] + 1);
            return static_cast<int>(val.size()) - 1;
        }
        // t の祖先で深さが d のもの
        int ancestor(int t, int d) const {
            while (depth[t] > d) t = depth[jump[t]] >= d ? jump[t] : parent[t];
            return t;
        }
    };

    std::shared_ptr<Pool> pool;  // 一度も push していなければ nullptr
    int back_node, n;            // 要素は back_node から n 個上までのノード

    PersistentQueue(std::shared_ptr<Pool> pool, int back_node, int n) : pool(std::move(pool)), back_node(back_node), n(n) {}

   public:
    /**
     * @brief 空のキューを作る
     */
    PersistentQueue() : pool(nullptr), back_node(0), n(0) {}

    /**
     * @brief 要素数を返す
     */
    long long size() const { return n; }
    /**
     * @brief 空かどうかを返す
     */
    bool empty() const { return n == 0; }

    /**
     * @brief 先頭の要素を返す
     */
    T front() const {
        assert(!empty());
        return pool->val[pool->ancestor(back_node, pool->depth[back_node] - n + 1)];
    }
    /**
     * @brief 末尾の要素を返す
     */
    T back() const {
        assert(!empty());
        return pool->val[back_node];
    }
    /**
     * @brief 末尾にxを加えた新しい版を返す
     *
     * @param x 加える値
     * @return PersistentQueue 新しい版 (自身は変わらない)
     */
    PersistentQueue push(const T& x) const {
        std::shared_ptr<Pool> p = pool ? pool : std::make_shared<Pool>();
        int t = p->make_node(x, back_node);
        return PersistentQueue(std::move(p), t, n + 1);
    }
    /**
     * @brief 先頭の要素を取り除いた新しい版を返す
     *
     * @return PersistentQueue 新しい版 (自身は変わらない)
     */
    PersistentQueue pop() const {
        assert(!empty());
        return PersistentQueue(pool, back_node, n - 1);
    }

    /**
     * @brief すべての要素を先頭から順に並べた列を返す
     */
    std::vector<T> to_vector() const {
        std::vector<T> res(n);
        int t = back_node;
        for (int i = n - 1; i >= 0; i--) {
            res[i] = pool->val[t];
            t = pool->parent[t];
        }
        return res;
    }
};
#line 2 "src/data_structures/persistent-queue.hpp"

/**
 * @file persistent-queue.hpp
 * @brief 永続キュー
 */

#include <cassert>
#include <memory>
#include <utility>
#include <vector>

/**
 * @brief 永続キュー
 *
 * 更新すると新しい版を返し、元の版は変わらない。同じキューから作った版はノードを共有する
 * 要素を木の根から末尾へのパスとして持ち、先頭はジャンプポインタで祖先をたどって求める
 * 例:
 * PersistentQueue<int> q0;
 * auto q1 = q0.push(1).push(2);  // q0 はそのまま
 * q1.front();                    // 1
 * q1.pop().front();              // 2
 *
 * @tparam T 要素の型
 */
template <typename T>
class PersistentQueue {
    // すべての版のノードを持つ。ノードは削除しない。ノード 0 は根 (要素を持たない)
    struct Pool {
        std::vector<T> val;
        std::vector<int> parent, jump, depth;
        Pool() : val(1), parent{0}, jump{0}, depth{0} {}
        int make_node(const T& x, int p) {
            // ジャンプの長さが 1, 1, 3, 1, 1, 3, 7, ... (skew binary) になるように張る
            int j = jump[p];
            int nj = depth[p] - depth[j] == depth[j] - depth[jump[j]] ? jump[j] : p;
            val.push_back(x);
            parent.push_back(p);
            jump.push_back(nj);
            depth.push_back(depth[p] + 1);
            return static_cast<int>(val.size()) - 1;
        }
        // t の祖先で深さが d のもの
        int ancestor(int t, int d) const {
            while (depth[t] > d) t = depth[jump[t]] >= d ? jump[t] : parent[t];
            return t;
        }
    };

    std::shared_ptr<Pool> pool;  // 一度も push していなければ nullptr
    int back_node, n;            // 要素は back_node から n 個上までのノード

    PersistentQueue(std::shared_ptr<Pool> pool, int back_node, int n) : pool(std::move(pool)), back_node(back_node), n(n) {}

   public:
    /**
     * @brief 空のキューを作る
     */
    PersistentQueue() : pool(nullptr), back_node(0), n(0) {}

    /**
     * @brief 要素数を返す
     */
    long long size() const { return n; }
    /**
     * @brief 空かどうかを返す
     */
    bool empty() const { return n == 0; }

    /**
     * @brief 先頭の要素を返す
     */
    T front() const {
        assert(!empty());
        return pool->val[pool->ancestor(back_node, pool->depth[back_node] - n + 1)];
    }
    /**
     * @brief 末尾の要素を返す
     */
    T back() const {
        assert(!empty());
        return pool->val[back_node];
    }
    /**
     * @brief 末尾にxを加えた新しい版を返す
     *
     * @param x 加える値
     * @return PersistentQueue 新しい版 (自身は変わらない)
     */
    PersistentQueue push(const T& x) const {
        std::shared_ptr<Pool> p = pool ? pool : std::make_shared<Pool>();
        int t = p->make_node(x, back_node);
        return PersistentQueue(std::move(p), t, n + 1);
    }
    /**
     * @brief 先頭の要素を取り除いた新しい版を返す
     *
     * @return PersistentQueue 新しい版 (自身は変わらない)
     */
    PersistentQueue pop() const {
        assert(!empty());
        return PersistentQueue(pool, back_node, n - 1);
    }

    /**
     * @brief すべての要素を先頭から順に並べた列を返す
     */
    std::vector<T> to_vector() const {
        std::vector<T> res(n);
        int t = back_node;
        for (int i = n - 1; i >= 0; i--) {
            res[i] = pool->val[t];
            t = pool->parent[t];
        }
        return res;
    }
};
Back to top page