数据结构 1
C++template <class _KTp, class _Tp, class _KeyOfValue, class _Comp = std::less<_KTp>, class _Alloc = std::allocator<_Tp>>
class _Splay
{
struct _Node
{
_Tp _M_value;
unsigned long long _M_size;
unsigned long long _M_cnt;
_Node *_M_left, *_M_right, *_M_father;
inline _Node(unsigned long long s = 0ull, const _Tp &v = _Tp(), unsigned long long c = 0ull, _Node *l = nullptr, _Node *r = nullptr, _Node *f = nullptr)
: _M_value(v), _M_size(s), _M_cnt(c), _M_left(l), _M_right(r), _M_father(f) {}
inline _Node &operator=(const _Node &n)
{
_M_value = n._M_value;
_M_size = n._M_size;
_M_cnt = n._M_cnt;
_M_left = n._M_left;
_M_right = n._M_right;
_M_father = n._M_father;
return *this;
}
inline ~_Node()
{
_M_value = _Tp();
_M_size = 0;
_M_cnt = 0;
_M_left = _M_right = _M_father = nullptr;
}
inline bool operator==(const _Node &n) const
{
return _M_value == n._M_value && _M_size == n._M_size && _M_cnt == n._M_cnt && _M_left == n._M_left && _M_right == n._M_right && _M_father == n._M_father;
}
inline void _M_pushup()
{
unsigned long long ls = 0, rs = 0;
if (_M_left)
ls = _M_left->_M_size;
if (_M_right)
rs = _M_right->_M_size;
_M_size = ls + rs + _M_cnt;
}
inline bool _M_get()
{
return this == _M_father->_M_right;
}
inline void _M_rotate()
{
_Node *y = _M_father, *z = y->_M_father;
if (this == _M_father->_M_right)
{
y->_M_right = _M_left;
if (_M_left)
{
_M_left->_M_father = y;
}
_M_left = y;
y->_M_father = this;
_M_father = z;
if (z)
{
if (y == z->_M_right)
{
z->_M_right = this;
}
else
{
z->_M_left = this;
}
}
}
else
{
y->_M_left = _M_right;
if (_M_right)
{
_M_right->_M_father = y;
}
_M_right = y;
y->_M_father = this;
_M_father = z;
if (z)
{
if (y == z->_M_right)
{
z->_M_right = this;
}
else
{
z->_M_left = this;
}
}
}
y->_M_pushup();
_M_pushup();
}
inline void _M_splay()
{
for (_Node *f = _M_father; f = _M_father, f; _M_rotate())
{
if (f->_M_father)
{
if (_M_get() == f->_M_get())
{
f->_M_rotate();
}
else
{
_M_rotate();
}
}
}
}
};
_Node *_M_root;
private:
class _M_iterator_base
{
protected:
static inline void _M_inc(_Node *&_M_cur) noexcept(false)
{
while (_M_cur == _M_cur->_M_father->_M_right)
{
_M_cur = _M_cur->_M_father;
}
_M_cur = _M_cur->_M_father;
while (_M_cur->_M_left)
{
_M_cur = _M_cur->_M_left;
}
}
static inline void _M_dec(_Node *&_M_cur) noexcept(false)
{
while (_M_cur == _M_cur->_M_father->_M_left)
{
_M_cur = _M_cur->_M_father;
}
_M_cur = _M_cur->_M_father;
while (_M_cur->_M_left)
{
_M_cur = _M_cur->_M_left;
}
}
};
public:
/*
*
* @brief The Splay Tree's iterator type.
*
*/
class iterator : protected _M_iterator_base
{
public:
typedef _Tp value_type;
typedef _Tp &reference;
typedef _Tp *pointer;
typedef const _Tp &const_reference;
typedef const _Tp *const_pointer;
typedef std::ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
private:
_Node *_M_cur;
public:
reference operator*() const
{
return this->_M_cur->_M_value;
}
pointer operator->() const
{
return &(operator*());
}
iterator &operator++()
{
_M_inc(_M_cur);
return *this;
}
iterator operator++(int)
{
iterator tmp = *this;
_M_inc(_M_cur);
return tmp;
}
iterator &operator--()
{
_M_dec(_M_cur);
return *this;
}
iterator operator--(int)
{
iterator tmp = *this;
_M_dec(_M_cur);
return *this;
}
iterator(const iterator &it) : _M_cur(it._M_cur) {}
iterator(_Node *ptr) : _M_cur(ptr) {}
iterator() {}
};
/*
*
* @brief The Splay Tree's const_iterator type.
*
*/
class const_iterator : protected _M_iterator_base
{
public:
typedef _Tp value_type;
typedef const _Tp &reference;
typedef const _Tp *pointer;
typedef const _Tp &const_reference;
typedef const _Tp *const_pointer;
typedef std::ptrdiff_t difference_type;
typedef std::bidirectional_iterator_tag iterator_category;
private:
_Node *_M_cur;
public:
reference operator*() const
{
return this->_M_cur->_M_value;
}
pointer operator->() const
{
return &(operator*());
}
const_iterator &operator++()
{
_M_inc(_M_cur);
return *this;
}
const_iterator operator++(int)
{
iterator tmp = *this;
_M_inc(_M_cur);
return tmp;
}
const_iterator &operator--()
{
_M_dec(_M_cur);
return *this;
}
const_iterator operator--(int)
{
iterator tmp = *this;
_M_dec(_M_cur);
return *this;
}
const_iterator(const const_iterator &it) : _M_cur(it._M_cur) {}
const_iterator(_Node *ptr) : _M_cur(ptr) {}
const_iterator() {}
};
private:
typedef typename std::allocator_traits<_Alloc>::template rebind_alloc<_Node> _M_node_alloc_type;
typedef std::allocator_traits<_M_node_alloc_type> _M_node_alloc_traits;
_M_node_alloc_type _M_alloc;
inline _Node *_M_get_node()
{
return _M_node_alloc_traits::allocate(_M_alloc, 1);
}
inline void _M_put_node(_Node *n)
{
_M_node_alloc_traits::deallocate(_M_alloc, n, 1);
}
private:
inline void _M_init()
{
_M_root = _M_get_node();
_M_root->_M_size = 0;
_M_root->_M_cnt = 0;
_M_root->_M_value = _Tp();
}
private:
_Comp _M_key_comp;
_KeyOfValue _M_key_of_value;
public:
// default ctor
_Splay(const _Comp &comp = _Comp(), const _KeyOfValue &key_of_value = _KeyOfValue()) : _M_key_comp(comp), _M_key_of_value(key_of_value)
{
_M_init();
}
public:
typedef _KTp key_type;
typedef _Tp value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public:
/*
*
* @brief Insert a value to the Splay tree.
* This algorithm continues insert as long as there is one or more equal elements in the current tree.
*
* @param __v The value to insert.
*
*/
inline iterator _M_insert_equal(const _Tp &__v)
{
if (_M_root->_M_size == 0)
{
_M_root->_M_value = __v;
_M_root->_M_size = 1;
_M_root->_M_cnt = 1;
_M_root->_M_left = _M_root->_M_right = _M_root->_M_father = nullptr;
return (iterator)_M_root;
}
_Node *cur = _M_root, *f = nullptr;
while (cur)
{
if (cur->_M_value == __v)
{
++cur->_M_cnt;
++cur->_M_size;
if (f) f->_M_pushup();
cur->_M_splay();
_M_root = cur;
return (iterator)cur;
}
f = cur;
if (_M_key_comp(_M_key_of_value(cur->_M_value), _M_key_of_value(__v)))
{
cur = cur->_M_left;
}
else
{
cur = cur->_M_right;
}
}
cur = _M_get_node();
cur->_M_value = __v;
cur->_M_size = 1;
cur->_M_cnt = 1;
cur->_M_left = cur->_M_right = nullptr;
cur->_M_father = f;
if (_M_key_comp(_M_key_of_value(f->_M_value), _M_key_of_value(__v)))
{
f->_M_left = cur;
}
else
{
f->_M_right = cur;
}
cur->_M_splay();
_M_root = cur;
return (iterator)cur;
}
/*
*
* @brief Insert a value to the Splay tree.
* This algorithm returns as long as there is one or more equal elements in the current tree.
*
* @param __v The value to insert.
*
*/
inline std::pair<iterator, bool> _M_insert_unique(const _Tp &__v)
{
if (_M_root->_M_size == 0)
{
_M_root->_M_value = __v;
_M_root->_M_size = 1;
_M_root->_M_cnt = 1;
_M_root->_M_left = _M_root->_M_right = _M_root->_M_father = nullptr;
return std::make_pair((iterator)_M_root, true);
}
_Node *cur = _M_root, *f = nullptr;
while (cur)
{
if (cur->_M_value == __v)
{
++cur->_M_cnt;
++cur->_M_size;
if (f) f->_M_pushup();
cur->_M_splay();
_M_root = cur;
return std::make_pair((iterator)cur, false);
}
f = cur;
if (_M_key_comp(_M_key_of_value(cur->_M_value), _M_key_of_value(__v)))
{
cur = cur->_M_left;
}
else
{
cur = cur->_M_right;
}
}
cur = _M_get_node();
cur->_M_value = __v;
cur->_M_size = 1;
cur->_M_cnt = 1;
cur->_M_left = cur->_M_right = nullptr;
cur->_M_father = f;
if (_M_key_comp(_M_key_of_value(f->_M_value), _M_key_of_value(__v)))
{
f->_M_left = cur;
}
else
{
f->_M_right = cur;
}
cur->_M_splay();
_M_root = cur;
return std::make_pair((iterator)cur, true);
}
static std::size_t _M_height(_Node* cur) {
if (!cur) return 0;
return std::max(_M_height(cur->_M_left), _M_height(cur->_M_right)) + 1;
}
inline std::size_t height() {
return _M_height(_M_root);
}
};
C++inline bool my_is_nullptr(void *ptr) { return (long long)ptr >= 0x100000000ll || (long long)ptr <= 0ll; }
template <typename _Tp, typename _UIntType>
class Node {
public:
int size;
_Tp value;
int cnt;
_UIntType priority;
Node *left, *right;
inline Node(int s = 0, const _Tp &v = _Tp(), int c = 0, const _UIntType &pro = _UIntType(), Node *l = nullptr, Node *r = nullptr)
: size(s), value(v), cnt(c), priority(pro), left(l), right(r) {}
~Node() = default;
inline Node<_Tp, _UIntType> &operator=(const Node<_Tp, _UIntType> &) = default;
inline void update() {
size = cnt;
if (!my_is_nullptr(left)) size += left->size;
if (!my_is_nullptr(right)) size += right->size;
}
};
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp = std::less<_Tp> >
class rotate_treap {
typedef typename _Rand::result_type _UIntType;
Node<_Tp, _UIntType> *root;
_KeyOfValue _key_of_value;
_Comp _comp;
_Rand rd;
public:
inline void insert_unique(const _Tp& v);
inline void insert_equal(const _Tp& v);
inline void erase(const _Tp& v);
inline _Tp lower_bound(const _Tp& v) const;
inline _Tp upper_bound(const _Tp& v) const;
inline std::size_t Rank(const _Tp& v) const;
inline const _Tp kth(std::size_t index) const;
inline std::pair<_Tp, _Tp> equal_range(const _Tp& v) const;
};
template <typename _Tp, typename _UIntType, typename _KeyOfValue, typename _Comp = std::less<_Tp>, typename _Rand>
void __insert_unique(Node<_Tp, _UIntType> *& cur, const _Tp& v, _KeyOfValue key_of_value, _Comp comp, _Rand& rd) {
if (my_is_nullptr(cur)) {
cur = new Node<_Tp, _UIntType>(1, v, 1, rd(), nullptr, nullptr);
return;
}
if (!comp(key_of_value(cur->value), key_of_value(v)) && !comp(key_of_value(v), key_of_value(cur->value))) { // key_of_value(cur->value) == key_of_value(v)
return;
}
if (comp(key_of_value(v), key_of_value(cur->value))) {
__insert_unique(cur->left, v, key_of_value, comp, rd);
if (cur->left->priority < cur->priority) zig(cur);
}
else {
__insert_unique(cur->right, v, key_of_value, comp, rd);
if (cur->right->priority < cur->priority) zag(cur);
}
cur->update();
return;
}
template <typename _Tp, typename _UIntType, typename _KeyOfValue, typename _Comp = std::less<_Tp>, typename _Rand>
void __insert_equal(Node<_Tp, _UIntType> *& cur, const _Tp& v, _KeyOfValue key_of_value, _Comp comp, _Rand& rd) {
if (my_is_nullptr(cur)) {
cur = new Node<_Tp, _UIntType>(1, v, 1, rd(), nullptr, nullptr);
return;
}
if (!comp(key_of_value(cur->value), key_of_value(v)) && !comp(key_of_value(v), key_of_value(cur->value))) { // key_of_value(cur->value) == key_of_value(v)
++cur->cnt;
++cur->size;
return;
}
if (comp(key_of_value(v), key_of_value(cur->value))) {
__insert_unique(cur->left, v, key_of_value, comp, rd);
if (cur->left->priority < cur->priority) zig(cur);
}
else {
__insert_unique(cur->right, v, key_of_value, comp, rd);
if (cur->right->priority < cur->priority) zag(cur);
}
cur->update();
return;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void rotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::insert_unique(const _Tp& v) { __insert_unique(root, v, _key_of_value, _comp, rd); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void rotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::insert_equal(const _Tp& v) { __insert_equal(root, v, _key_of_value, _comp, rd); }
template <typename _Tp, typename _UIntType, typename _KeyOfValue, typename _Comp>
void __erase(Node<_Tp, _UIntType> *& cur, const _Tp& v, _KeyOfValue key_of_value, _Comp comp) {
if (my_is_nullptr(cur)) return;
if (comp(key_of_value(v), key_of_value(cur->value))) {
__erase(cur->left, v, key_of_value, comp);
cur->update();
}
else if (comp(key_of_value(cur->value), key_of_value(v))) {
__erase(cur->right, v, key_of_value, comp);
cur->update();
}
else { // key_of_value(cur->value) == key_of_value(v)
if (cur->cnt > 1) {
--cur->cnt;
--cur->size;
return;
}
short state = 0;
state |= (!my_is_nullptr(cur->left));
state |= (!my_is_nullptr(cur->right) << 1);
Node<_Tp, _UIntType> *tmp = cur;
switch (state) {
case 0: {
delete cur;
cur = nullptr;
break;
}
case 1: {
cur = tmp->left;
delete tmp;
break;
}
case 2: {
cur = tmp->right;
delete tmp;
break;
}
default: {
if (cur->left->priority < cur->right->priority) {
zig(cur);
__erase(cur->right, v, key_of_value, comp);
}
else {
zag(cur);
__erase(cur->left, v, key_of_value, comp);
}
cur->update();
}
}
}
return;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void rotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::erase(const _Tp& v) { __erase(root, v, _key_of_value, _comp); }
template <typename _Tp, typename _UIntType>
const _Tp __how_much(Node<_Tp, _UIntType> * cur, size_t index) {
if (my_is_nullptr(cur)) return 0x3f3f3f3f;
size_t less_siz = my_is_nullptr(cur->left) ? 0 : cur->left->size;
if (index <= less_siz) return __how_much(cur->left, index);
if (index <= less_siz + cur->cnt) return cur->value;
return __how_much(cur->right, index - less_siz - cur->cnt);
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline const _Tp rotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::kth(size_t index) const { return __how_much(root, index); }
template <typename _Tp, typename _UIntType, typename _KeyOfValue, typename _Comp>
size_t __rank(Node<_Tp, _UIntType> * cur, const _Tp& v, _KeyOfValue key_of_value, _Comp comp) {
size_t less_siz = my_is_nullptr(cur->left) ? 0 : cur->left->size;
if (comp(key_of_value(v), key_of_value(cur->value))) {
if (!my_is_nullptr(cur->left)) return __rank(cur->left, v, key_of_value, comp);
return 1;
}
if (comp(key_of_value(cur->value), key_of_value(v))) {
if (!my_is_nullptr(cur->right)) return less_siz + cur->cnt + __rank(cur->right, v, key_of_value, comp);
return cur->size + 1;
}
return less_siz + 1;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
size_t rotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::Rank(const _Tp& v) const { return __rank(root, v, _key_of_value, _comp); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline _Tp rotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::lower_bound(const _Tp& v) const { return this->kth(Rank(v)); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline _Tp rotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::upper_bound(const _Tp& v) const { return this->kth(Rank(v) + 1); }
C++inline bool my_is_nullptr(void *ptr) { return (long long)ptr >= 0x100000000ll || (long long)ptr <= 0ll; }
template <typename _Tp, typename _UIntType>
class Node {
public:
int size;
_Tp value;
int cnt;
_UIntType priority;
Node *left, *right;
inline Node(int s = 0, const _Tp &v = _Tp(), int c = 0, const _UIntType &pro = _UIntType(), Node *l = nullptr, Node *r = nullptr)
: size(s), value(v), cnt(c), priority(pro), left(l), right(r) {}
~Node() = default;
inline Node<_Tp, _UIntType> &operator=(const Node<_Tp, _UIntType> &) = default;
inline void update() {
size = cnt;
if (!my_is_nullptr(left)) size += left->size;
if (!my_is_nullptr(right)) size += right->size;
}
};
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp = std::less<_Tp>>
class nrotate_treap {
typedef typename _Rand::result_type _UIntType;
Node<_Tp, _UIntType> *root;
_KeyOfValue _key_of_value;
std::less<_UIntType> priority_comp;
_Comp comp;
_Rand rd;
static inline void split(Node<_Tp, _UIntType> *cur, _Tp v, Node<_Tp, _UIntType> *&l, Node<_Tp, _UIntType> *&r, _KeyOfValue key_of_value, _Comp comp);
static inline void split_by_rank(Node<_Tp, _UIntType>* cur, int rk, Node<_Tp, _UIntType> *&l, Node<_Tp, _UIntType> *&mid, Node<_Tp, _UIntType> *&r);
template <typename _Priority_comp>
static inline Node<_Tp, _UIntType> *merge(Node<_Tp, _UIntType> *l, Node<_Tp, _UIntType> *r, _Priority_comp comp);
public:
inline nrotate_treap() = default;
inline ~nrotate_treap() = default;
inline bool insert_unique(const _Tp &v);
inline void insert_equal(const _Tp &v);
inline void erase(const _Tp &v);
inline _Tp lower_bound(const _Tp &v);
inline _Tp upper_bound(const _Tp &v);
inline int Rank(const _Tp &v);
inline _Tp kth(int index);
inline std::pair<_Tp, _Tp> equal_range(const _Tp &v);
inline int size() {
root->update();
return root->size;
}
};
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::split(Node<_Tp, typename _Rand::result_type> *cur, _Tp v, Node<_Tp, typename _Rand::result_type> *&l, Node<_Tp, typename _Rand::result_type> *&r, _KeyOfValue key_of_value, _Comp comp) {
if (my_is_nullptr(cur)) {
l = r = nullptr;
return;
}
if (comp(key_of_value(v), key_of_value(cur->value))) {
split(cur->left, v, l, cur->left, key_of_value, comp);
cur->update();
r = cur;
}
else {
split(cur->right, v, cur->right, r, key_of_value, comp);
cur->update();
l = cur;
}
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::split_by_rank(Node<_Tp, _UIntType>* cur, int rk, Node<_Tp, _UIntType> *&l, Node<_Tp, _UIntType> *&mid, Node<_Tp, _UIntType> *&r) {
if (my_is_nullptr(cur)) {
l = mid = r = nullptr;
return;
}
int ls_siz = my_is_nullptr(cur->left) ? 0 : cur->left->size;
if (rk <= ls_siz) {
split_by_rank(cur->left, rk, l, mid, r);
cur->left = r;
cur->update();
r = cur;
return;
}
if (rk <= ls_siz + cur->cnt) {
l = cur->left;
r = cur->right;
cur->left = cur->right = nullptr;
mid = cur;
return;
}
split_by_rank(cur->right, rk - ls_siz - cur->cnt, l, mid, r);
cur->right = l;
cur->update();
l = cur;
return;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
template <typename _Priority_comp>
inline Node<_Tp, typename _Rand::result_type> *nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::merge(Node<_Tp, typename _Rand::result_type> *l, Node<_Tp, typename _Rand::result_type> *r, _Priority_comp comp) {
if (!l) return r;
if (!r) return l;
if (comp(l->priority, r->priority)) {
l->right = merge(l->right, r, comp);
l->update();
return l;
}
r->left = merge(l, r->left, comp);
r->update();
return r;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline bool nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::insert_unique(const _Tp &v) {
Node<_Tp, typename _Rand::result_type> *l, *tmp, *r;
split(root, v, l, r, _key_of_value, comp);
split(l, v - 1, l, tmp, _key_of_value, comp);
if (my_is_nullptr(tmp)) tmp = new Node<_Tp, typename _Rand::result_type>(1, v, 1, rd(), nullptr, nullptr);
else {
root = merge(merge(l, tmp, priority_comp), r, priority_comp);
if (!my_is_nullptr(root)) root->update();
return false;
}
root = merge(merge(l, tmp, priority_comp), r, priority_comp);
if (!my_is_nullptr(root)) root->update();
return true;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::insert_equal(const _Tp &v) {
Node<_Tp, typename _Rand::result_type> *l, *tmp, *r;
split(root, v, l, r, _key_of_value, comp);
split(l, v - 1, l, tmp, _key_of_value, comp);
if (my_is_nullptr(tmp)) tmp = new Node<_Tp, typename _Rand::result_type>(1, v, 1, rd(), nullptr, nullptr);
else {
++tmp->cnt;
++tmp->size;
}
root = merge(merge(l, tmp, priority_comp), r, priority_comp);
if (!my_is_nullptr(root)) root->update();
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::erase(const _Tp &v) {
Node<_Tp, typename _Rand::result_type> *l, *tmp, *r;
split(root, v, l, r, _key_of_value, comp);
split(l, v - 1, l, tmp, _key_of_value, comp);
if (my_is_nullptr(tmp)) {
root = merge(l, r, priority_comp);
return;
}
if (tmp->cnt > 1) {
--tmp->cnt;
--tmp->size;
}
else {
tmp = merge(tmp->left, tmp->right, priority_comp);
if (!my_is_nullptr(tmp)) tmp->update();
}
root = merge(merge(l, tmp, priority_comp), r, priority_comp);
if (!my_is_nullptr(root)) root->update();
return;
}
template <typename _Tp, typename _UIntType>
_Tp kth(Node<_Tp, _UIntType>* cur, int index) {
if (my_is_nullptr(cur)) return _Tp();
int left_siz = (my_is_nullptr(cur->left) ? 0 : cur->left->size);
if (index <= left_siz) return kth(cur->left, index);
if (index <= left_siz + cur->cnt) return cur->value;
return kth(cur->right, index - left_siz - cur->cnt);
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline _Tp nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::kth(int index) { return ::kth(root, index); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline int nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::Rank(const _Tp &v) {
Node<_Tp, typename _Rand::result_type> *x, *y;
split(root, v - 1, x, y, _key_of_value, comp);
int res = (my_is_nullptr(x) ? 0 : x->size) + 1;
root = merge(x, y, priority_comp);
return res;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline _Tp nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::lower_bound(const _Tp &v) { return this->kth(Rank(v)); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline _Tp nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::upper_bound(const _Tp &v) { return this->kth(Rank(v) + 1); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline std::pair<_Tp, _Tp> nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::equal_range(const _Tp &v) { return std::make_pair(lower_bound(v), upper_bound(v)); }
C++bool my_is_nullptr(void *ptr) { return (long long)ptr >= 0x1000000000ll || (long long)ptr <= 0ll; }
template <typename _Tp, typename _UIntType>
class Node {
public:
int size, cnt;
_Tp value;
_UIntType priority;
Node *left, *right;
inline Node(int s = 0, const _Tp &v = _Tp(), int c = 0, const _UIntType &pro = _UIntType(), Node *l = nullptr, Node *r = nullptr) : size(s), cnt(c), value(v), priority(pro), left(l), right(r) {}
inline ~Node() = default;
inline Node<_Tp, _UIntType> &operator=(const Node<_Tp, _UIntType> &o) {
size = o.size;
cnt = o.cnt;
value = o.value;
priority = o.priority;
left = o.left;
right = o.right;
return *this;
}
inline void update() {
size = cnt;
if (!my_is_nullptr(left)) size += left->size;
if (!my_is_nullptr(right)) size += right->size;
}
};
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp = std::less<_Tp>, size_t _MAXN = 1048576>
class persistent_nrotate_treap {
typedef typename _Rand::result_type _UIntType;
typedef Node<_Tp, _UIntType> node_type;
node_type *root[_MAXN];
_KeyOfValue _key_of_value;
std::less<_UIntType> priority_comp;
_Comp comp;
_Rand rd;
static void split(node_type *cur, _Tp v, node_type *&l, node_type *&r, _KeyOfValue key_of_value, _Comp comp);
template <typename _Priority_comp>
static node_type *merge(node_type *l, node_type *r, _Priority_comp comp);
public:
inline persistent_nrotate_treap() = default;
inline ~persistent_nrotate_treap() = default;
inline bool insert_unique(size_t version, const _Tp &v);
inline void insert_equal(size_t version, const _Tp &v);
inline void erase(size_t version, const _Tp &v);
inline _Tp lower_bound(size_t version, const _Tp &v);
inline _Tp upper_bound(size_t version, const _Tp &v);
inline int Rank(size_t version, const _Tp &v);
inline _Tp kth(size_t version, int index);
inline std::pair<_Tp, _Tp> equal_range(size_t version, const _Tp &v);
inline int size(size_t version) {
if (my_is_nullptr(root[version])) return 0;
root[version]->update();
return root[version]->size;
}
inline void copy(size_t from, size_t to) { root[to] = root[from]; }
};
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
void persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::
split(Node<_Tp, typename _Rand::result_type> *cur, _Tp v,
Node<_Tp, typename _Rand::result_type> *&l,
Node<_Tp, typename _Rand::result_type> *&r,
_KeyOfValue key_of_value, _Comp comp) {
if (my_is_nullptr(cur)) {
l = r = nullptr;
return;
}
if (comp(key_of_value(v), key_of_value(cur->value))) {
r = new Node<_Tp, typename _Rand::result_type>(0, 0, 0, 0, nullptr, nullptr);
*r = *cur;
split(r->left, v, l, r->left, key_of_value, comp);
r->update();
}
else {
l = new Node<_Tp, typename _Rand::result_type>(0, 0, 0, 0, nullptr, nullptr);
*l = *cur;
split(l->right, v, l->right, r, key_of_value, comp);
l->update();
}
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
template <typename _Priority_comp>
Node<_Tp, typename _Rand::result_type> *persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::
merge(Node<_Tp, typename _Rand::result_type> *l,
Node<_Tp, typename _Rand::result_type> *r,
_Priority_comp comp) {
if (!l) return r;
if (!r) return l;
if (comp(l->priority, r->priority)) {
Node<_Tp, typename _Rand::result_type>* rt = new Node<_Tp, typename _Rand::result_type>(0, 0, 0, 0, nullptr, nullptr);
*rt = *l;
rt->right = merge(rt->right, r, comp);
rt->update();
return rt;
}
Node<_Tp, typename _Rand::result_type>* rt = new Node<_Tp, typename _Rand::result_type>(0, 0, 0, 0, nullptr, nullptr);
*rt = *r;
rt->left = merge(l, rt->left, comp);
rt->update();
return rt;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline bool persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::
insert_unique(size_t version, const _Tp &v) {
if (my_is_nullptr(root[version])) {
root[version] = new Node<_Tp, typename _Rand::result_type>(1, v, 1, rd(), nullptr, nullptr);
return true;
}
Node<_Tp, typename _Rand::result_type> *l = nullptr, *tmp = nullptr, *r = nullptr;
split(root[version], v, l, r, _key_of_value, comp);
split(l, v - 1, l, tmp, _key_of_value, comp);
if (my_is_nullptr(tmp)) tmp = new Node<_Tp, typename _Rand::result_type>(1, v, 1, rd(), nullptr, nullptr);
else {
root[version] = merge(merge(l, tmp, priority_comp), r, priority_comp);
if (!my_is_nullptr(root)) root->update();
return false;
}
root[version] = merge(merge(l, tmp, priority_comp), r, priority_comp);
if (!my_is_nullptr(root)) root->update();
return true;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline void persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::
insert_equal(size_t version, const _Tp &v) {
if (my_is_nullptr(root[version])) {
root[version] = new Node<_Tp, typename _Rand::result_type>(1, v, 1, rd(), nullptr, nullptr);
return;
}
Node<_Tp, typename _Rand::result_type> *l = nullptr, *tmp = nullptr, *r = nullptr;
split(root[version], v, l, r, _key_of_value, comp);
split(l, v - 1, l, tmp, _key_of_value, comp);
if (my_is_nullptr(tmp)) tmp = new Node<_Tp, typename _Rand::result_type>(1, v, 1, rd(), nullptr, nullptr);
else {
++tmp->cnt;
++tmp->size;
}
root[version] = merge(merge(l, tmp, priority_comp), r, priority_comp);
if (!my_is_nullptr(root[version])) root[version]->update();
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline void persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::
erase(size_t version, const _Tp &v) {
Node<_Tp, typename _Rand::result_type> *l = nullptr, *tmp = nullptr, *r = nullptr;
split(root[version], v, l, r, _key_of_value, comp);
std::pair<Node<_Tp, typename _Rand::result_type>*, Node<_Tp, typename _Rand::result_type>*> temp(l, r);
split(l, v - 1, l, tmp, _key_of_value, comp);
if (my_is_nullptr(tmp)) {
root[version] = merge(l, r, priority_comp);
return;
}
if (tmp->cnt > 1) {
--tmp->cnt;
--tmp->size;
root[version] = merge(merge(l, tmp, priority_comp), r, priority_comp);
}
else {
tmp = merge(tmp->left, tmp->right, priority_comp);
root[version] = merge(merge(l, tmp, priority_comp), r, priority_comp);
}
if (!my_is_nullptr(root[version])) root[version]->update();
return;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline int persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::
Rank(size_t version, const _Tp &v) {
Node<_Tp, typename _Rand::result_type> *x = nullptr, *y = nullptr;
split(root[version], v - 1, x, y, _key_of_value, comp);
int res = (my_is_nullptr(x) ? 0 : x->size) + 1;
root[version] = merge(x, y, priority_comp);
return res;
}
template <typename _Tp, typename _UIntType>
_Tp kth(Node<_Tp, _UIntType>* cur, int index) {
if (index <= 0) return std::numeric_limits<_Tp>::min();
if (my_is_nullptr(cur)) return _Tp();
cur->update();
if (index > cur->size) return std::numeric_limits<_Tp>::max();
int left_siz = (my_is_nullptr(cur->left) ? 0 : cur->left->size);
if (index <= left_siz) return kth(cur->left, index);
if (index <= left_siz + cur->cnt) return cur->value;
return kth(cur->right, index - left_siz - cur->cnt);
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline _Tp persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::kth(size_t version, int index) { return ::kth(root[version], index); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline _Tp persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::lower_bound(size_t version, const _Tp &v) { return kth(version, Rank(version, v)); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline _Tp persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::upper_bound(size_t version, const _Tp &v) { return kth(version, Rank(version, v) + 1); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp, size_t _MAXN>
inline std::pair<_Tp, _Tp> persistent_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp, _MAXN>::equal_range(size_t version, const _Tp &v) { return std::make_pair(lower_bound(version, v), upper_bound(version, v)); }
C++#include <iterator>
#include <memory>
template <class _Tp>
class __my_list_node {
public:
typedef _Tp value_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
public:
_Tp data;
__my_list_node<_Tp> *prev, *next;
__my_list_node(_Tp x) : data(x), prev(nullptr), next(nullptr) {}
__my_list_node() {}
__my_list_node(_Tp x, __my_list_node<_Tp>* p, __my_list_node<_Tp>* n) : data(x), prev(p), next(n) {}
};
template <class _Tp>
class __my_list_iterator {
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef _Tp value_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
typedef __my_list_node<_Tp>* link_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public:
link_type node;
bool operator==(const __my_list_iterator<_Tp>& o) const { return node == o.node; }
bool operator!=(const __my_list_iterator<_Tp>& o) const { return node != o.node; }
reference operator*() const { return (*node).data; }
pointer operator->() const { return &(operator*()); }
__my_list_iterator<_Tp>& operator++() {
node = (link_type)((*node).next);
return *this;
}
__my_list_iterator<_Tp>& operator--() {
node = (link_type)((*node).prev);
return *this;
}
operator link_type() { return node; }
__my_list_iterator(const link_type& x) : node(x) {}
__my_list_iterator() : node(nullptr) {}
operator bool() {return node != nullptr; }
};
template <class _Tp, class _Alloc = std::allocator<_Tp>>
class my_list {
public:
typedef _Tp value_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
typedef __my_list_iterator<_Tp> iterator;
typedef const __my_list_iterator<_Tp> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
protected:
__my_list_node<_Tp>* _M_node;
typedef typename std::allocator_traits<_Alloc>::rebind_alloc<_Tp> _Tp_alloc_type;
typedef typename std::allocator_traits<_Alloc>::rebind_alloc<__my_list_node<_Tp>> _Node_alloc_type;
typedef std::allocator_traits<_Tp_alloc_type> _Tp_alloc_traits;
typedef std::allocator_traits<_Node_alloc_type> _Node_alloc_traits;
_Node_alloc_type _M_alloc;
protected:
__my_list_node<_Tp>* _M_get_node() {return _Node_alloc_traits::allocate(_M_alloc, 1); }
void _M_put_node(__my_list_node<_Tp>* p) {_Node_alloc_traits::deallocate(_M_alloc, p, 1); }
public:
void empty_initialize() {
_M_node = _M_get_node();
_M_node->prev = _M_node;
_M_node->next = _M_node;
}
my_list() {empty_initialize(); }
iterator begin() { return (__my_list_node<_Tp>*)((*_M_node).next); }
iterator end() { return _M_node; }
const_iterator begin() const { return (__my_list_node<_Tp>*)((*_M_node).next); }
const_iterator end() const { return _M_node; }
#if __cplusplus >= 201103L
const_iterator cbegin() const { return (__my_list_node<_Tp>*)((*_M_node).next); }
const_iterator cend() const { return _M_node; }
#endif
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
#endif
size_type size() { return std::distance(begin(), end()); }
bool empty() const { return _M_node->next == _M_node; }
reference front() { return *begin(); }
reference back() { return *(--end()); }
iterator insert(const_iterator _Pos, const _Tp& x) {
__my_list_node<_Tp>* tmp = _M_get_node();
tmp->data = x;
tmp->next = _Pos.node;
tmp->prev = _Pos.node->prev;
((__my_list_node<_Tp>*)(_Pos.node->prev))->next = tmp;
_Pos.node->prev = tmp;
return tmp;
}
void push_back(const _Tp& x) { insert(end(), x); }
void push_front(const _Tp& x) { insert(begin(), x); }
iterator erase(const_iterator _Pos) {
__my_list_node<_Tp>* next_node = (__my_list_node<_Tp>*)(_Pos.node->next);
__my_list_node<_Tp>* prev_node = (__my_list_node<_Tp>*)(_Pos.node->prev);
prev_node->next = next_node;
next_node->prev = prev_node;
_M_put_node(_Pos.node);
return iterator(next_node);
}
void pop_front() { erase(begin()); }
void pop_back() { erase(end()); }
void clear() {
__my_list_node<_Tp>* cur = (__my_list_node<_Tp>*)begin();
while (cur != _M_node) {
__my_list_node<_Tp>* tmp = cur;
cur = cur->next;
_M_put_node(tmp);
}
}
void remove(const _Tp& x) {
iterator first = begin(), last = end();
while (first != last) {
iterator next = first;
++next;
if (*first == x) erase(first);
first = next;
}
}
template <class _Comp>
void unique(_Comp cmp) {
iterator first = begin(), last = end();
if (first == last) return;
iterator next = first;
while (++next != last) {
if (cmp(*first, *next)) erase(next);
else first = next;
next = first;
}
}
void unique() { unique(std::equal_to<_Tp>()); }
protected:
void transfer(const_iterator _Pos, iterator first, iterator last) {
if (_Pos != last) {
(*((__my_list_node<_Tp>*)((*last.node).prev))).next = _Pos.node;
(*((__my_list_node<_Tp>*)((*first.node).prev))).next = last.node;
(*((__my_list_node<_Tp>*)((*_Pos.node).prev))).next = first.node;
__my_list_node<_Tp>* tmp = (__my_list_node<_Tp>*)((*_Pos.node).prev);
(*_Pos.node).prev = (*last.node).prev;
(*last.node).prev = (*first.node).prev;
(*first.node).prev = tmp;
}
}
public:
void splice(const_iterator _Pos, my_list<_Tp>& x) { if (!x.empty()) transfer(_Pos, x.begin(), x.end()); }
void splice(const_iterator _Pos, my_list<_Tp>&, const_iterator i) {
iterator j = i;
++j;
if (_Pos == i || _Pos == j) return;
transfer(_Pos, i, j);
}
void splice(const_iterator _Pos, my_list<_Tp>&, const_iterator first, const_iterator last) {
if (first != last) transfer(_Pos, first, last);
}
template <class _Comp>
void merge(my_list<_Tp>& x, _Comp cmp) {
iterator first1 = begin(), last1 = end(), first2 = x.begin(), last2 = x.end();
while (first1 != last1 && first2 != last2) {
if (cmp(*first2, *first1)) {
iterator next = first2;
transfer(first1, first2, ++next);
first2 = next;
}
else ++first1;
}
if (first2 != last2) transfer(last1, first2, last2);
}
void merge(my_list<_Tp>& x) { merge(x, std::less<_Tp>()); }
void reverse() {
if (_M_node->next == _M_node || (__my_list_node<_Tp>*)(_M_node->next)->next == _M_node) return;
iterator first = begin();
++first;
while (first != end()) {
iterator old = first;
++first;
transfer(begin(), old, first);
}
}
void swap(my_list<_Tp>& x) { std::swap(*this, x); }
template <typename _Comp>
void sort(_Comp cmp) {
if (_M_node->next == _M_node || (__my_list_node<_Tp>*)(_M_node->next)->next == _M_node) return;
my_list<_Tp, _Alloc> carry, count[64];
int fill = 0;
while (!empty()) {
carry.splice(carry.begin(), *this, begin());
int i = 0;
while (i < fill && !count[i].empty()) {
count[i].merge(carry, cmp);
carry.swap(count[i++]);
}
carry.swap(count[i]);
if (i == fill) ++fill;
}
for (int i = 1; i < fill; i++) count[i].merge(count[i - 1], cmp);
swap(count[fill - 1]);
}
void sort() { sort(std::less<_Tp>()); }
my_list(const my_list<_Tp, _Alloc>& l) : _M_node(l._M_node), _M_alloc(l._M_alloc) {}
my_list<_Tp, _Alloc>& operator=(const my_list<_Tp, _Alloc>& l) {
_M_node = l._M_node;
_M_alloc = l._M_alloc;
return *this;
}
protected:
template <class _ForwardIterator>
void _M_insert_all(_ForwardIterator bg, _ForwardIterator ed, std::forward_iterator_tag, _Tp) { for (; bg != ed; ++bg) push_back(*bg); }
public:
template <class _ForwardIterator>
my_list(_ForwardIterator bg, _ForwardIterator ed) { _M_insert_all(bg, ed, typename std::iterator_traits<_Tp>::iterator_category, typename std::iterator_traits<_Tp>::value_type); }
};