This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "ds/w_ary_tree.hpp"
#pragma once
#include "bit/clz.hpp"
#include "bit/ctz.hpp"
template <class T0, make_unsigned_t<T0> U, class = void>
class w_ary_tree {
using T = make_unsigned_t<T0>;
public:
bool insert(T x) {
if (!ptr) ptr = make_unique<w_ary_tree<T0, V>[]>((U - 1) / V + 1);
mask |= uint64_t(1) << (x / V);
return ptr[x / V].insert(x % V);
}
bool erase(T x) {
if (!ptr) return false;
bool ret = ptr[x / V].erase(x % V);
if (ptr[x / V].mask == 0) {
if constexpr (V > 64) ptr[x / V].ptr.reset();
mask &= ~(uint64_t(1) << (x / V));
}
return ret;
}
bool contains(T x) const { return ptr && ptr[x / V].contains(x % V); }
T min() const {
if (!mask) return -1;
T i = ctz(mask);
return (i * V) | ptr[i].min();
}
T max() const {
if (!mask) return -1;
T i = 63 - clz(mask);
return (i * V) | ptr[i].max();
}
// max s s.t. s < x
T pred(T x) const {
if (!ptr) return -1;
T u = ptr[x / V].pred(x % V);
if (u != -1) return (x & -V) | u;
uint64_t mask2 = mask & ~(~uint64_t(0) << (x / V));
if (mask2 == 0) return -1;
int i = 63 - clz(mask2);
return (i * V) | ptr[i].max();
}
// min s s.t. s >= x
T succ(T x) const {
if (!ptr) return -1;
T u = ptr[x / V].succ(x % V);
if (u != -1) return (x & -V) | u;
uint64_t mask2 = mask & ~(~uint64_t(0) >> (63 - x / V));
if (mask2 == 0) return -1;
int i = ctz(mask2);
return (i * V) | ptr[i].min();
}
private:
static constexpr T calc_v(T u) {
T res = 1;
while (u > 64) u /= 64, res *= 64;
return res;
}
static constexpr T V = calc_v(U);
uint64_t mask = 0;
unique_ptr<w_ary_tree<T0, V>[]> ptr;
template <class T2, make_unsigned_t<T2>, class>
friend class w_ary_tree;
};
template <class T0, make_unsigned_t<T0> U>
class w_ary_tree<T0, U, enable_if_t<U <= 64>> {
using T = make_unsigned_t<T0>;
public:
bool insert(T x) {
bool ret = ~mask >> x & 1;
mask |= uint64_t(1) << x;
return ret;
}
bool erase(T x) {
bool ret = mask >> x & 1;
mask &= ~(uint64_t(1) << x);
return ret;
}
bool contains(T x) const { return mask >> x & 1; }
T min() const { return mask ? ctz(mask) : -1; }
T max() const { return mask ? 63 - clz(mask) : -1; }
// max s s.t. s < x
T pred(T x) const {
uint64_t mask2 = mask & ~(~uint64_t(0) << x);
return mask2 == 0 ? -1 : 63 - clz(mask2);
}
T succ(T x) const {
uint64_t mask2 = mask & (~uint64_t(0) << x);
return mask2 == 0 ? -1 : ctz(mask2);
}
private:
uint64_t mask = 0;
template <class T2, make_unsigned_t<T2>, class>
friend class w_ary_tree;
};
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "bit/clz.hpp"
#pragma GCC target("lzcnt")
template <class T>
int clz(T x) {
if (!x) return sizeof(T) * 8;
if constexpr (sizeof(T) <= sizeof(unsigned)) {
return __builtin_clz((unsigned)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
return __builtin_clzll((unsigned long long)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
}
}
#line 3 "bit/ctz.hpp"
#pragma GCC target("bmi")
template <class T>
int ctz(T x) {
if (!x) return sizeof(T) * 8;
if constexpr (sizeof(T) <= sizeof(unsigned)) {
return __builtin_ctz((unsigned)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
return __builtin_ctzll((unsigned long long)x);
} else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
unsigned long long y = x;
return y ? ctz(y)
: sizeof(y) * 8 + ctz((unsigned long long)(x >> sizeof(y) * 8));
}
}
#line 4 "ds/w_ary_tree.hpp"
template <class T0, make_unsigned_t<T0> U, class = void>
class w_ary_tree {
using T = make_unsigned_t<T0>;
public:
bool insert(T x) {
if (!ptr) ptr = make_unique<w_ary_tree<T0, V>[]>((U - 1) / V + 1);
mask |= uint64_t(1) << (x / V);
return ptr[x / V].insert(x % V);
}
bool erase(T x) {
if (!ptr) return false;
bool ret = ptr[x / V].erase(x % V);
if (ptr[x / V].mask == 0) {
if constexpr (V > 64) ptr[x / V].ptr.reset();
mask &= ~(uint64_t(1) << (x / V));
}
return ret;
}
bool contains(T x) const { return ptr && ptr[x / V].contains(x % V); }
T min() const {
if (!mask) return -1;
T i = ctz(mask);
return (i * V) | ptr[i].min();
}
T max() const {
if (!mask) return -1;
T i = 63 - clz(mask);
return (i * V) | ptr[i].max();
}
// max s s.t. s < x
T pred(T x) const {
if (!ptr) return -1;
T u = ptr[x / V].pred(x % V);
if (u != -1) return (x & -V) | u;
uint64_t mask2 = mask & ~(~uint64_t(0) << (x / V));
if (mask2 == 0) return -1;
int i = 63 - clz(mask2);
return (i * V) | ptr[i].max();
}
// min s s.t. s >= x
T succ(T x) const {
if (!ptr) return -1;
T u = ptr[x / V].succ(x % V);
if (u != -1) return (x & -V) | u;
uint64_t mask2 = mask & ~(~uint64_t(0) >> (63 - x / V));
if (mask2 == 0) return -1;
int i = ctz(mask2);
return (i * V) | ptr[i].min();
}
private:
static constexpr T calc_v(T u) {
T res = 1;
while (u > 64) u /= 64, res *= 64;
return res;
}
static constexpr T V = calc_v(U);
uint64_t mask = 0;
unique_ptr<w_ary_tree<T0, V>[]> ptr;
template <class T2, make_unsigned_t<T2>, class>
friend class w_ary_tree;
};
template <class T0, make_unsigned_t<T0> U>
class w_ary_tree<T0, U, enable_if_t<U <= 64>> {
using T = make_unsigned_t<T0>;
public:
bool insert(T x) {
bool ret = ~mask >> x & 1;
mask |= uint64_t(1) << x;
return ret;
}
bool erase(T x) {
bool ret = mask >> x & 1;
mask &= ~(uint64_t(1) << x);
return ret;
}
bool contains(T x) const { return mask >> x & 1; }
T min() const { return mask ? ctz(mask) : -1; }
T max() const { return mask ? 63 - clz(mask) : -1; }
// max s s.t. s < x
T pred(T x) const {
uint64_t mask2 = mask & ~(~uint64_t(0) << x);
return mask2 == 0 ? -1 : 63 - clz(mask2);
}
T succ(T x) const {
uint64_t mask2 = mask & (~uint64_t(0) << x);
return mask2 == 0 ? -1 : ctz(mask2);
}
private:
uint64_t mask = 0;
template <class T2, make_unsigned_t<T2>, class>
friend class w_ary_tree;
};