競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.yosupo.jp/problem/double_ended_priority_queue"
#include <functional>
#include <iostream>
#include "../../../src/data_structures/erasable-priority-queue.hpp"
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n, q;
std::cin >> n >> q;
// 最大値用と最小値用の 2 つを持ち、片方から取り出したらもう片方からも削除する
ErasablePriorityQueue<int> max_q;
ErasablePriorityQueue<int, std::greater<int>> min_q;
for (int i = 0; i < n; i++) {
int x;
std::cin >> x;
max_q.push(x);
min_q.push(x);
}
while (q--) {
int t;
std::cin >> t;
if (t == 0) {
int x;
std::cin >> x;
max_q.push(x);
min_q.push(x);
} else if (t == 1) {
int x = min_q.top();
min_q.pop();
max_q.erase(x);
std::cout << x << '\n';
} else {
int x = max_q.top();
max_q.pop();
min_q.erase(x);
std::cout << x << '\n';
}
}
}#line 1 "test/yosupo/data_structure/double-ended-priority-queue.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/double_ended_priority_queue"
#include <functional>
#include <iostream>
#line 2 "src/data_structures/erasable-priority-queue.hpp"
/**
* @file erasable-priority-queue.hpp
* @brief 削除可能な優先度付きキュー
*/
#include <cassert>
#line 10 "src/data_structures/erasable-priority-queue.hpp"
#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 7 "test/yosupo/data_structure/double-ended-priority-queue.test.cpp"
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n, q;
std::cin >> n >> q;
// 最大値用と最小値用の 2 つを持ち、片方から取り出したらもう片方からも削除する
ErasablePriorityQueue<int> max_q;
ErasablePriorityQueue<int, std::greater<int>> min_q;
for (int i = 0; i < n; i++) {
int x;
std::cin >> x;
max_q.push(x);
min_q.push(x);
}
while (q--) {
int t;
std::cin >> t;
if (t == 0) {
int x;
std::cin >> x;
max_q.push(x);
min_q.push(x);
} else if (t == 1) {
int x = min_q.top();
min_q.pop();
max_q.erase(x);
std::cout << x << '\n';
} else {
int x = max_q.top();
max_q.pop();
min_q.erase(x);
std::cout << x << '\n';
}
}
}