shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: 削除可能な優先度付きキュー
(src/data_structures/erasable-priority-queue.hpp)

任意の要素を削除できる優先度付きキュー ErasablePriorityQueue<T, Compare = std::less<T>> です。std::priority_queue と同じく、既定では最大の要素を取り出します。

削除する要素を別のヒープに入れておき、それが先頭に来たときに取り除きます。std::multiset より定数倍が軽いです。

ErasablePriorityQueue<int> pq;                        // 最大を取り出す
ErasablePriorityQueue<int, std::greater<int>> pq2;    // 最小を取り出す
pq.push(3);
pq.push(5);
pq.erase(5);
pq.top();  // 3

メンバ関数

関数 説明 計算量
ErasablePriorityQueue(comp = Compare()) 空のキューを作る $O(1)$
push(x) x を追加 償却 $O(\log n)$
erase(x) x を 1 つ削除。x はキューに含まれていること (含まれていない要素を削除すると壊れる。空なら assert で落ちる) 償却 $O(\log n)$
top() 先頭 (既定では最大) の要素。空なら assert で落ちる $O(1)$
pop() 先頭の要素を削除。空なら assert で落ちる 償却 $O(\log n)$
size()、empty() 要素数、空か $O(1)$

Compare で等しい要素 (!comp(x, y) && !comp(y, x)) は区別しません。

最大と最小の両方を取り出したいときは、std::less と std::greater の 2 つを持ち、片方から取り出した要素をもう片方から erase します。

ErasablePriorityQueue<int> max_q;
ErasablePriorityQueue<int, std::greater<int>> min_q;
int x = min_q.top();  // 最小を取り出す
min_q.pop();
max_q.erase(x);

関連

Verified with

Code

#pragma once

/**
 * @file erasable-priority-queue.hpp
 * @brief 削除可能な優先度付きキュー
 */

#include <cassert>
#include <functional>
#include <queue>
#include <vector>

/**
 * @brief 削除可能な優先度付きキュー
 *
 * std::priority_queue と同じく、既定では最大の要素を取り出す
 * 削除する要素を別のヒープに入れておき、先頭に来たときに取り除く。各操作は償却 O(log n)
 * 例:
 * ErasablePriorityQueue<int> pq;
 * pq.push(3); pq.push(5); pq.erase(5); pq.top();  // 3
 *
 * @tparam T 要素の型
 * @tparam Compare 比較関数。std::greater<T> にすると最小の要素を取り出す
 */
template <typename T, typename Compare = std::less<T>>
class ErasablePriorityQueue {
    std::priority_queue<T, std::vector<T>, Compare> que, del;
    Compare comp;

    bool equivalent(const T& x, const T& y) const { return !comp(x, y) && !comp(y, x); }
    // 先頭が削除予定なら取り除く
    void normalize() {
        while (!del.empty() && equivalent(que.top(), del.top())) {
            que.pop();
            del.pop();
        }
    }

   public:
    explicit ErasablePriorityQueue(Compare comp = Compare()) : que(comp), del(comp), comp(comp) {}

    /**
     * @brief 要素数
     */
    long long size() const { return que.size() - del.size(); }
    bool empty() const { return size() == 0; }

    /**
     * @brief x を追加する
     */
    void push(const T& x) {
        que.push(x);
        normalize();
    }
    /**
     * @brief x を 1 つ削除する。x は含まれていること (含まれていない要素を削除すると壊れる)
     * 比較関数で等しい要素は区別しない。空なら assert で落ちる
     */
    void erase(const T& x) {
        assert(!empty());
        del.push(x);
        normalize();
    }
    /**
     * @brief 先頭 (既定では最大) の要素。空なら assert で落ちる
     */
    const T& top() const {
        assert(!empty());
        return que.top();
    }
    /**
     * @brief 先頭の要素を削除する。空なら assert で落ちる
     */
    void pop() {
        assert(!empty());
        que.pop();
        normalize();
    }
};
#line 2 "src/data_structures/erasable-priority-queue.hpp"

/**
 * @file erasable-priority-queue.hpp
 * @brief 削除可能な優先度付きキュー
 */

#include <cassert>
#include <functional>
#include <queue>
#include <vector>

/**
 * @brief 削除可能な優先度付きキュー
 *
 * std::priority_queue と同じく、既定では最大の要素を取り出す
 * 削除する要素を別のヒープに入れておき、先頭に来たときに取り除く。各操作は償却 O(log n)
 * 例:
 * ErasablePriorityQueue<int> pq;
 * pq.push(3); pq.push(5); pq.erase(5); pq.top();  // 3
 *
 * @tparam T 要素の型
 * @tparam Compare 比較関数。std::greater<T> にすると最小の要素を取り出す
 */
template <typename T, typename Compare = std::less<T>>
class ErasablePriorityQueue {
    std::priority_queue<T, std::vector<T>, Compare> que, del;
    Compare comp;

    bool equivalent(const T& x, const T& y) const { return !comp(x, y) && !comp(y, x); }
    // 先頭が削除予定なら取り除く
    void normalize() {
        while (!del.empty() && equivalent(que.top(), del.top())) {
            que.pop();
            del.pop();
        }
    }

   public:
    explicit ErasablePriorityQueue(Compare comp = Compare()) : que(comp), del(comp), comp(comp) {}

    /**
     * @brief 要素数
     */
    long long size() const { return que.size() - del.size(); }
    bool empty() const { return size() == 0; }

    /**
     * @brief x を追加する
     */
    void push(const T& x) {
        que.push(x);
        normalize();
    }
    /**
     * @brief x を 1 つ削除する。x は含まれていること (含まれていない要素を削除すると壊れる)
     * 比較関数で等しい要素は区別しない。空なら assert で落ちる
     */
    void erase(const T& x) {
        assert(!empty());
        del.push(x);
        normalize();
    }
    /**
     * @brief 先頭 (既定では最大) の要素。空なら assert で落ちる
     */
    const T& top() const {
        assert(!empty());
        return que.top();
    }
    /**
     * @brief 先頭の要素を削除する。空なら assert で落ちる
     */
    void pop() {
        assert(!empty());
        que.pop();
        normalize();
    }
};
Back to top page