This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: STANDALONE
#include "../../template/template.hpp"
#include "../../structure/heap/partially-retroactive-priority-queue.hpp"
namespace {
enum class OperationType { NOOP, PUSH, POP };
struct Operation {
OperationType type = OperationType::NOOP;
long long value = 0;
};
template <typename Compare>
struct NaivePriorityQueue {
multiset<long long, Compare> values;
long long sum = 0;
};
template <typename Compare>
NaivePriorityQueue<Compare> build_naive(const vector<Operation>& operations,
Compare compare) {
NaivePriorityQueue<Compare> ret{multiset<long long, Compare>(compare), 0};
for (const auto& operation : operations) {
if (operation.type == OperationType::PUSH) {
ret.values.insert(operation.value);
ret.sum += operation.value;
} else if (operation.type == OperationType::POP and
not ret.values.empty()) {
ret.sum -= *ret.values.begin();
ret.values.erase(ret.values.begin());
}
}
return ret;
}
template <typename Compare>
void check(const PartiallyRetroactivePriorityQueue<long long, Compare>& queue,
const vector<Operation>& operations, Compare compare) {
auto expected = build_naive(operations, compare);
assert(queue.empty() == expected.values.empty());
assert(queue.size() == (int)expected.values.size());
assert(queue.sum() == expected.sum);
if (not expected.values.empty()) {
assert(queue.top() == *expected.values.begin());
}
}
template <typename Compare>
void exhaustive_test(Compare compare) {
constexpr int N = 6;
constexpr int STATES = 5;
int patterns = 1;
for (int i = 0; i < N; i++) patterns *= STATES;
for (int mask = 0; mask < patterns; mask++) {
PartiallyRetroactivePriorityQueue<long long, Compare> queue(N, compare);
vector<Operation> operations(N);
int x = mask;
for (int t = 0; t < N; t++) {
int state = x % STATES;
x /= STATES;
if (state == 1) {
operations[t].type = OperationType::POP;
queue.set_pop(t);
} else if (state >= 2) {
operations[t] = {OperationType::PUSH, state - 3};
queue.set_push(t, state - 3);
}
}
check(queue, operations, compare);
}
}
template <typename Compare>
void random_test(Compare compare, uint64_t seed) {
mt19937_64 rng(seed);
for (int n = 1; n <= 30; n++) {
for (int rep = 0; rep < 100; rep++) {
PartiallyRetroactivePriorityQueue<long long, Compare> queue(n, compare);
vector<Operation> operations(n);
check(queue, operations, compare);
for (int q = 0; q < 300; q++) {
int t = (int)(rng() % n);
int type = (int)(rng() % 3);
if (type == 0) {
long long value = (long long)(rng() % 21) - 10;
operations[t] = {OperationType::PUSH, value};
queue.set_push(t, value);
} else if (type == 1) {
operations[t] = {OperationType::POP, 0};
queue.set_pop(t);
} else {
operations[t] = {};
queue.set_noop(t);
}
check(queue, operations, compare);
}
}
}
}
} // namespace
int main() {
{
PartiallyRetroactivePriorityQueue<long long> queue(0);
assert(queue.empty());
assert(queue.size() == 0);
assert(queue.sum() == 0);
}
{
PartiallyRetroactivePriorityQueue<long long> queue(6);
queue.set_pop(0);
queue.set_push(1, 4);
queue.set_pop(2);
queue.set_push(3, 2);
queue.set_push(4, 2);
assert(queue.size() == 2);
assert(queue.top() == 2);
assert(queue.sum() == 4);
queue.set_noop(0);
assert(queue.size() == 2);
assert(queue.sum() == 4);
queue.set_noop(2);
assert(queue.size() == 3);
assert(queue.top() == 2);
assert(queue.sum() == 8);
queue.set_pop(5);
assert(queue.size() == 2);
assert(queue.top() == 2);
assert(queue.sum() == 6);
queue.set_push(3, -5);
assert(queue.size() == 2);
assert(queue.top() == 2);
assert(queue.sum() == 6);
}
exhaustive_test(less<long long>());
exhaustive_test(greater<long long>());
random_test(less<long long>(), 123456789);
random_test(greater<long long>(), 987654321);
}
#line 1 "test/unittest/partially-retroactive-priority-queue.test.cpp"
// competitive-verifier: STANDALONE
#line 1 "template/template.hpp"
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
using namespace std;
using int64 = long long;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (size_t i = 0; i < v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
for (T& in : v) is >> in;
return is;
}
template <typename T1, typename T2>
bool chmax(T1& a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
bool chmin(T1& a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
enable_if_t<is_class_v<T> == 0> fill_v(T& t, const V& v) {
t = v;
}
template <typename T, typename V>
enable_if_t<is_class_v<T> != 0> fill_v(T& t, const V& v) {
for (auto& e : t) fill_v(e, v);
}
template <typename F>
struct FixPoint : F {
explicit FixPoint(F&& f) : F(std::forward<F>(f)) {}
template <typename... Args>
decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <typename F>
decltype(auto) MFP(F&& f) {
return FixPoint<F>{std::forward<F>(f)};
}
#line 4 "test/unittest/partially-retroactive-priority-queue.test.cpp"
#line 2 "structure/heap/partially-retroactive-priority-queue.hpp"
#line 2 "structure/class/range-add-range-min.hpp"
template <typename T>
struct RangeAddRangeMin {
using S = T;
using F = T;
static constexpr S op(const S& a, const S& b) { return min(a, b); }
static constexpr S e() { return numeric_limits<T>::max(); }
static constexpr F mapping(const S& x, const F& f) { return x + f; }
static constexpr F composition(const F& f, const F& g) { return f + g; }
static constexpr F id() { return {0}; }
};
#line 2 "structure/segment-tree/lazy-segment-tree.hpp"
#line 2 "structure/class/acted-monoid.hpp"
template <typename S2, typename Op, typename E, typename F2, typename Mapping,
typename Composition, typename Id>
struct LambdaActedMonoid {
using S = S2;
using F = F2;
S op(const S& a, const S& b) const { return _op(a, b); }
S e() const { return _e(); }
S mapping(const S& x, const F& f) const { return _mapping(x, f); }
F composition(const F& f, const F& g) const { return _composition(f, g); }
F id() const { return _id(); }
LambdaActedMonoid(Op _op, E _e, Mapping _mapping, Composition _composition,
Id _id)
: _op(_op),
_e(_e),
_mapping(_mapping),
_composition(_composition),
_id(_id) {}
private:
Op _op;
E _e;
Mapping _mapping;
Composition _composition;
Id _id;
};
template <typename Op, typename E, typename Mapping, typename Composition,
typename Id>
LambdaActedMonoid(Op _op, E _e, Mapping _mapping, Composition _composition,
Id _id)
-> LambdaActedMonoid<decltype(_e()), Op, E, decltype(_id()), Mapping,
Composition, Id>;
/*
struct ActedMonoid {
using S = ?;
using F = ?;
static constexpr S op(const S& a, const S& b) {}
static constexpr S e() {}
static constexpr S mapping(const S &x, const F &f) {}
static constexpr F composition(const F &f, const F &g) {}
static constexpr F id() {}
};
*/
#line 4 "structure/segment-tree/lazy-segment-tree.hpp"
template <typename ActedMonoid>
struct LazySegmentTree {
using S = typename ActedMonoid::S;
using F = typename ActedMonoid::F;
private:
ActedMonoid m;
int n{}, sz{}, height{};
vector<S> data;
vector<F> lazy;
inline void update(int k) {
data[k] = m.op(data[2 * k + 0], data[2 * k + 1]);
}
inline void all_apply(int k, const F& x) {
data[k] = m.mapping(data[k], x);
if (k < sz) lazy[k] = m.composition(lazy[k], x);
}
inline void propagate(int k) {
if (lazy[k] != m.id()) {
all_apply(2 * k + 0, lazy[k]);
all_apply(2 * k + 1, lazy[k]);
lazy[k] = m.id();
}
}
public:
LazySegmentTree() = default;
explicit LazySegmentTree(ActedMonoid m, int n) : m(m), n(n) {
sz = 1;
height = 0;
while (sz < n) sz <<= 1, height++;
data.assign(2 * sz, m.e());
lazy.assign(2 * sz, m.id());
}
explicit LazySegmentTree(ActedMonoid m, const vector<S>& v)
: LazySegmentTree(m, static_cast<int>(v.size())) {
build(v);
}
void build(const vector<S>& v) {
assert(n == (int)v.size());
for (int k = 0; k < n; k++) data[k + sz] = v[k];
for (int k = sz - 1; k > 0; k--) update(k);
}
void set(int k, const S& x) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = x;
for (int i = 1; i <= height; i++) update(k >> i);
}
S get(int k) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
return data[k];
}
S operator[](int k) { return get(k); }
S prod(int l, int r) {
if (l >= r) return m.e();
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
S L = m.e(), R = m.e();
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) L = m.op(L, data[l++]);
if (r & 1) R = m.op(data[--r], R);
}
return m.op(L, R);
}
S all_prod() const { return data[1]; }
void apply(int k, const F& f) {
k += sz;
for (int i = height; i > 0; i--) propagate(k >> i);
data[k] = m.mapping(data[k], f);
for (int i = 1; i <= height; i++) update(k >> i);
}
void apply(int l, int r, const F& f) {
if (l >= r) return;
l += sz;
r += sz;
for (int i = height; i > 0; i--) {
if (((l >> i) << i) != l) propagate(l >> i);
if (((r >> i) << i) != r) propagate((r - 1) >> i);
}
{
int l2 = l, r2 = r;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
}
l = l2, r = r2;
}
for (int i = 1; i <= height; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
template <typename C>
optional<int> find_first(int l, const C& check) {
if (l >= n) return nullopt;
l += sz;
for (int i = height; i > 0; i--) propagate(l >> i);
S sum = m.e();
do {
while ((l & 1) == 0) l >>= 1;
if (check(m.op(sum, data[l]))) {
while (l < sz) {
propagate(l);
l <<= 1;
auto nxt = m.op(sum, data[l]);
if (not check(nxt)) {
sum = nxt;
l++;
}
}
return l + 1 - sz;
}
sum = m.op(sum, data[l++]);
} while ((l & -l) != l);
return nullopt;
}
template <typename C>
optional<int> find_last(int r, const C& check) {
if (r <= 0) return nullopt;
r += sz;
for (int i = height; i > 0; i--) propagate((r - 1) >> i);
S sum = m.e();
do {
r--;
while (r > 1 and (r & 1)) r >>= 1;
if (check(m.op(data[r], sum))) {
while (r < sz) {
propagate(r);
r = (r << 1) + 1;
auto nxt = m.op(data[r], sum);
if (not check(nxt)) {
sum = nxt;
r--;
}
}
return r - sz;
}
sum = m.op(data[r], sum);
} while ((r & -r) != r);
return nullopt;
}
};
#line 5 "structure/heap/partially-retroactive-priority-queue.hpp"
template <typename T, typename Compare = std::less<T> >
struct PartiallyRetroactivePriorityQueue {
private:
enum class OperationType : uint8_t { NOOP, PUSH, POP };
struct Operation {
OperationType type = OperationType::NOOP;
optional<T> value;
bool alive = false;
};
struct CandidateNode {
int alive_min = -1;
int deleted_max = -1;
};
int n;
int operation_count;
int seg_size;
Compare compare;
vector<Operation> operations;
vector<CandidateNode> candidates;
// A deleted push has weight +1, a surviving push has weight 0, and a pop
// has weight -1. Their prefix sums are nonnegative, and a zero is a bridge.
// Updating one operation is a suffix addition on the prefix-sum array.
LazySegmentTree<RangeAddRangeMin<int> > prefix;
// A batch of n virtual values with lower priority than every real value is
// inserted before the timeline. It makes every pop valid; popping one of
// them is exactly an ignored pop on an empty real queue.
int dummy_alive;
int dummy_deleted = 0;
int present_size = 0;
T present_sum{};
bool key_less(int a, int b) const {
assert(a >= 0 and b >= 0);
if (a == b) return false;
if (a == 0) return false;
if (b == 0) return true;
const T& x = *operations[a - 1].value;
const T& y = *operations[b - 1].value;
if (compare(x, y)) return true;
if (compare(y, x)) return false;
return a < b;
}
int min_id(int a, int b) const {
if (a == -1) return b;
if (b == -1) return a;
return key_less(a, b) ? a : b;
}
int max_id(int a, int b) const {
if (a == -1) return b;
if (b == -1) return a;
return key_less(a, b) ? b : a;
}
void pull_candidate(int k) {
candidates[k].alive_min =
min_id(candidates[2 * k].alive_min, candidates[2 * k + 1].alive_min);
candidates[k].deleted_max = max_id(candidates[2 * k].deleted_max,
candidates[2 * k + 1].deleted_max);
}
void refresh_candidate(int p) {
CandidateNode node;
if (p == 0) {
if (dummy_alive > 0) node.alive_min = 0;
if (dummy_deleted > 0) node.deleted_max = 0;
} else {
const auto& op = operations[p - 1];
if (op.type == OperationType::PUSH) {
if (op.alive) {
node.alive_min = p;
} else {
node.deleted_max = p;
}
}
}
int k = p + seg_size;
candidates[k] = node;
while (k >>= 1) pull_candidate(k);
}
int range_min_alive(int l, int r) const {
int ret = -1;
for (l += seg_size, r += seg_size; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret = min_id(ret, candidates[l++].alive_min);
if (r & 1) ret = min_id(ret, candidates[--r].alive_min);
}
assert(ret != -1);
return ret;
}
int range_max_deleted(int l, int r) const {
int ret = -1;
for (l += seg_size, r += seg_size; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret = max_id(ret, candidates[l++].deleted_max);
if (r & 1) ret = max_id(ret, candidates[--r].deleted_max);
}
assert(ret != -1);
return ret;
}
void add_weight(int p, int x) { prefix.apply(p + 1, operation_count + 1, x); }
int find_first_zero(int l) {
auto ret = prefix.find_first(l, [](int x) { return x == 0; });
assert(ret.has_value());
return *ret - 1;
}
int find_last_zero(int r) {
auto ret = prefix.find_last(r + 1, [](int x) { return x == 0; });
assert(ret.has_value());
return *ret;
}
void promote(int id) {
if (id == 0) {
assert(dummy_deleted > 0);
--dummy_deleted;
++dummy_alive;
add_weight(0, -1);
refresh_candidate(0);
return;
}
auto& op = operations[id - 1];
assert(op.type == OperationType::PUSH and not op.alive);
op.alive = true;
add_weight(id, -1);
++present_size;
present_sum += *op.value;
refresh_candidate(id);
}
void demote(int id) {
if (id == 0) {
assert(dummy_alive > 0);
--dummy_alive;
++dummy_deleted;
add_weight(0, 1);
refresh_candidate(0);
return;
}
auto& op = operations[id - 1];
assert(op.type == OperationType::PUSH and op.alive);
op.alive = false;
add_weight(id, 1);
--present_size;
present_sum -= *op.value;
refresh_candidate(id);
}
void check_time(int t) const { assert(0 <= t and t < n); }
static int checked_size(int size) {
assert(size >= 0);
return size;
}
public:
explicit PartiallyRetroactivePriorityQueue(int size,
Compare comparator = Compare())
: n(checked_size(size)),
operation_count(n + 1),
seg_size(1),
compare(std::move(comparator)),
operations(n),
prefix(RangeAddRangeMin<int>(), vector<int>(n + 2)),
dummy_alive(n) {
while (seg_size < operation_count) seg_size <<= 1;
candidates.assign(2 * seg_size, CandidateNode{});
refresh_candidate(0);
}
/** Replaces operation t by push(value). */
void set_push(int t, T value) {
check_time(t);
set_noop(t);
int p = t + 1;
auto& op = operations[t];
op.type = OperationType::PUSH;
op.value.emplace(std::move(value));
op.alive = false;
add_weight(p, 1);
refresh_candidate(p);
int bridge = find_last_zero(p);
promote(range_max_deleted(bridge, operation_count));
}
/** Replaces operation t by pop. It is ignored when the queue is empty. */
void set_pop(int t) {
check_time(t);
set_noop(t);
int p = t + 1;
int bridge = find_first_zero(p + 1);
demote(range_min_alive(0, bridge));
auto& op = operations[t];
op.type = OperationType::POP;
op.value.reset();
op.alive = false;
add_weight(p, -1);
}
/** Replaces operation t by a no-op. */
void set_noop(int t) {
check_time(t);
int p = t + 1;
auto& op = operations[t];
if (op.type == OperationType::NOOP) return;
if (op.type == OperationType::POP) {
int bridge = find_last_zero(p);
int id = range_max_deleted(bridge, operation_count);
add_weight(p, 1);
promote(id);
} else if (op.alive) {
--present_size;
present_sum -= *op.value;
} else {
int bridge = find_first_zero(p + 1);
int id = range_min_alive(0, bridge);
demote(id);
add_weight(p, -1);
}
op.type = OperationType::NOOP;
op.alive = false;
refresh_candidate(p);
op.value.reset();
}
bool empty() const { return present_size == 0; }
int size() const { return present_size; }
const T& top() const {
assert(not empty());
int id = candidates[1].alive_min;
assert(id > 0);
return *operations[id - 1].value;
}
T sum() const { return present_sum; }
};
#line 6 "test/unittest/partially-retroactive-priority-queue.test.cpp"
namespace {
enum class OperationType { NOOP, PUSH, POP };
struct Operation {
OperationType type = OperationType::NOOP;
long long value = 0;
};
template <typename Compare>
struct NaivePriorityQueue {
multiset<long long, Compare> values;
long long sum = 0;
};
template <typename Compare>
NaivePriorityQueue<Compare> build_naive(const vector<Operation>& operations,
Compare compare) {
NaivePriorityQueue<Compare> ret{multiset<long long, Compare>(compare), 0};
for (const auto& operation : operations) {
if (operation.type == OperationType::PUSH) {
ret.values.insert(operation.value);
ret.sum += operation.value;
} else if (operation.type == OperationType::POP and
not ret.values.empty()) {
ret.sum -= *ret.values.begin();
ret.values.erase(ret.values.begin());
}
}
return ret;
}
template <typename Compare>
void check(const PartiallyRetroactivePriorityQueue<long long, Compare>& queue,
const vector<Operation>& operations, Compare compare) {
auto expected = build_naive(operations, compare);
assert(queue.empty() == expected.values.empty());
assert(queue.size() == (int)expected.values.size());
assert(queue.sum() == expected.sum);
if (not expected.values.empty()) {
assert(queue.top() == *expected.values.begin());
}
}
template <typename Compare>
void exhaustive_test(Compare compare) {
constexpr int N = 6;
constexpr int STATES = 5;
int patterns = 1;
for (int i = 0; i < N; i++) patterns *= STATES;
for (int mask = 0; mask < patterns; mask++) {
PartiallyRetroactivePriorityQueue<long long, Compare> queue(N, compare);
vector<Operation> operations(N);
int x = mask;
for (int t = 0; t < N; t++) {
int state = x % STATES;
x /= STATES;
if (state == 1) {
operations[t].type = OperationType::POP;
queue.set_pop(t);
} else if (state >= 2) {
operations[t] = {OperationType::PUSH, state - 3};
queue.set_push(t, state - 3);
}
}
check(queue, operations, compare);
}
}
template <typename Compare>
void random_test(Compare compare, uint64_t seed) {
mt19937_64 rng(seed);
for (int n = 1; n <= 30; n++) {
for (int rep = 0; rep < 100; rep++) {
PartiallyRetroactivePriorityQueue<long long, Compare> queue(n, compare);
vector<Operation> operations(n);
check(queue, operations, compare);
for (int q = 0; q < 300; q++) {
int t = (int)(rng() % n);
int type = (int)(rng() % 3);
if (type == 0) {
long long value = (long long)(rng() % 21) - 10;
operations[t] = {OperationType::PUSH, value};
queue.set_push(t, value);
} else if (type == 1) {
operations[t] = {OperationType::POP, 0};
queue.set_pop(t);
} else {
operations[t] = {};
queue.set_noop(t);
}
check(queue, operations, compare);
}
}
}
}
} // namespace
int main() {
{
PartiallyRetroactivePriorityQueue<long long> queue(0);
assert(queue.empty());
assert(queue.size() == 0);
assert(queue.sum() == 0);
}
{
PartiallyRetroactivePriorityQueue<long long> queue(6);
queue.set_pop(0);
queue.set_push(1, 4);
queue.set_pop(2);
queue.set_push(3, 2);
queue.set_push(4, 2);
assert(queue.size() == 2);
assert(queue.top() == 2);
assert(queue.sum() == 4);
queue.set_noop(0);
assert(queue.size() == 2);
assert(queue.sum() == 4);
queue.set_noop(2);
assert(queue.size() == 3);
assert(queue.top() == 2);
assert(queue.sum() == 8);
queue.set_pop(5);
assert(queue.size() == 2);
assert(queue.top() == 2);
assert(queue.sum() == 6);
queue.set_push(3, -5);
assert(queue.size() == 2);
assert(queue.top() == 2);
assert(queue.sum() == 6);
}
exhaustive_test(less<long long>());
exhaustive_test(greater<long long>());
random_test(less<long long>(), 123456789);
random_test(greater<long long>(), 987654321);
}