更好的 set?

· · 科技·工程

背景

在最近的几次模拟赛中本蒟蒻频繁遇到要使用平衡树维护信息的题目,然而每一次比赛单独打平衡树实在是太烦了,于是就想能不能把自己写的平衡树封装成一个 set 一般的东西。

简介

(代码在最后。)
使用 FHQ-treap + 指针 维护,故性能略输于 STL 自带的 set 与 map,但 luogu 模板题实测速度接近甚至略快于正常常数的 FHQ-treap,同时因使用指针,空间较为优秀,始终为 O(n)。
缺点:因为为了卡常弥补指针的大常数,导致大量使用传引用使得代码过于屎山带来了一些神秘 bug,类型传入自身类型如 treap<treap<int>> 时要套一层结构体变为 treap<node>(node 内部包含 treap<int>)不然视编译环境可能 CE(不过 OI 竞赛实际上一般不用套自己吧?)。
其他地方就放心用了。
使用 treap< 你的类型,随机数函数(可以没有) > 名称 即可定义。注意下标从 0 开始。

功能

支持传入随机数函数,需要为 unsigned 类型,传入自定义类型时可以重载小于号后正常使用。
注:函数后带 const 标记的在 const 类型下可用,否则不行。没有错误处理,越界或内存问题会直接 RE。最后一个比较函数是用来防可能存在的 CE 的,一般没用。
1.inline void insert(const t& in) 插入值且自动升序排序,O(\log n)。
2.inline void pop(const int& l, const int& len = 1) 从 l 的位置开始连续删除 len 个值(不传入为 1),O(\log n)。
3.inline const t& get(const int& x) const 获取第 x 个值,O(\log n)。
4.inline void rmall(const t& x) 删除所有 x,O(\log n)。
5.inline void erase(const t& x, const int& len = 1) 从第一个大于等于 x 的值开始连续删除 len 个值(不传入为 1),O(\log n)。
6.inline const int find(const t& x) const 找到第一个 x 的位置(若没有返回总元素数量),O(\log n)。
7.inline const int lower_bound(const t& x) const 找到第一个大于等于 x 的位置,O(\log n)。
8.inline const int upper_bound(const t& x) const 找到第一个大于 x 的位置,O(\log n)。
9.inline const int count(const t& x) const 返回 x 的数量,O(\log n),比 find 略慢。
10.inline const bool isin(const t& x) const 返回 x 是否存在,O(\log n),比 count 与 find 略快。
11.inline const int size() const 返回总元素数量,O(1)。
12.inline void clear() 清空,O(n)。
13.inline void from(const treap<t, rn>& x) 清空自身同时从 x 深拷贝,O(n + m)(m 为 x 总元素数)。
14.inline void swap(const treap<t, rn>& x) 交换自身与 x 的所有值,O(1)。
15.inline treap<t, rn> copy() 返回自身的深拷贝版本,O(n)。
16.inline const t& operator[](const int& x) const 通过中括号访问第 x 位的值,同 get。
17.inline treap& operator=(const treap<t, rn>& x) 通过等于号深拷贝 x,同 from。

代码

namespace ftrees {
    //the newest2.0
    namespace treapu {
#define it unsigned
        it las = 0;
        inline const it rnd() {
            return las = ((las * 13) ^ 998244353);
        }
#undef it
    }
    template<typename t, const unsigned rn() = treapu::rnd>
    class treap {
#define it unsigned
#define gll(x) (x->l==nullptr?0:x->l->len)
#define grl(x) (x->r==nullptr?0:x->r->len)
#define gsl(x) (x==nullptr?0:x->len)
    private:
        struct node {
            t s;
            it len, k;
            node* l, * r;
        };
        mutable node* rt;
        inline void pu(node*& x) const {
            x->len = gll(x) + grl(x) + 1;
        }
        inline node* cr(const t& in) const {
            node* out = new node;
            out->s = in;
            out->len = 1;
            out->k = rn();
            out->l = out->r = nullptr;
            return out;
        }
        inline void fll(node* i, const t& x, node*& l, node*& r) const {
            if (i == nullptr) {
                l = r = nullptr;
                return;
            }
            if (i->s < x) {
                l = i;
                fll(l->r, x, l->r, r);
                pu(l);
            }
            else {
                r = i;
                fll(r->l, x, l, r->l);
                pu(r);
            }
        }
        inline void flr(node* i, const t& x, node*& l, node*& r) const {
            if (i == nullptr) {
                l = r = nullptr;
                return;
            }
            if (x < i->s /* i->s > x */) {
                r = i;
                flr(r->l, x, l, r->l);
                pu(r);

            }
            else {
                l = i;
                flr(l->r, x, l->r, r);
                pu(l);
            }
        }
        inline void fls(node* i, const it& x, node*& l, node*& r) const {
            if (i == nullptr) {
                l = r = nullptr;
                return;
            }
            if (gll(i) < x) {
                l = i;
                fls(l->r, x - gll(i) - 1, l->r, r);
                pu(l);
            }
            else {
                r = i;
                fls(r->l, x, l, r->l);
                pu(r);
            }
        }
        inline node* hb(node* l, node* r) const {
            if (l == nullptr)return r;
            if (r == nullptr)return l;
            if (l->k > r->k) {
                l->r = hb(l->r, r);
                pu(l);
                return l;
            }
            else {
                r->l = hb(l, r->l);
                pu(r);
                return r;
            }
        }
        inline node* ge(const it& w, node* x) const {
            if (x == nullptr)return nullptr;
            if (w == gll(x) + 1)return x;
            if (w <= gll(x))return ge(w, x->l);
            else return ge(w - gll(x) - 1, x->r);
        }
        inline void cl(node*& x) const {
            if (x == nullptr)return;
            cl(x->l), cl(x->r);
            delete x;
            x = nullptr;
        }
        inline const bool ch(const t& in, node* x) const {
            if (x == nullptr)return 0;
            if (x->s < in)return ch(in, x->l);
            if (in < x->s)return ch(in, x->r);
            return 1;
        }
        inline const it ub(const t& in, node* x) const {
            if (x == nullptr)return 0;
            if (in < x->s) return ub(in, x->l);
            else return gll(x) + 1 + ub(in, x->r);
        }
        inline const it lb(const t& in, node* x) const {
            if (x == nullptr)return 0;
            if (x->s < in) return gll(x) + 1 + lb(in, x->r);
            else return lb(in, x->l);
        }
        //inline void print(node* x) {
        //  if (x == nullptr)return;
        //  print(x->l);
        //  cout << x->s << ' ';
        //  print(x->r);
        //}
        inline node* cp(node* x) const {
            if (x == nullptr)return nullptr;
            node* out = new node;
            out->k = x->k;
            out->len = x->len;
            out->s = x->s;
            out->l = cp(x->l);
            out->r = cp(x->r);
            return out;
        }
    public:
        treap() {
            rt = nullptr;
        }
        treap(const treap<t, rn>& x) {
            rt = cp(x.rt);
        }
        ~treap() {
            cl(rt);
        }
        inline void insert(const t& in) {
            node* a = nullptr, * b = nullptr;
            fll(rt, in, a, b);
            rt = hb(hb(a, cr(in)), b);
        }
        inline void pop(const it& l, const it& len = 1) {
            node* a = nullptr, * b = nullptr, * mid = nullptr;
            fls(rt, l, a, b);
            fls(b, len, mid, b);
            rt = hb(a, b);
            cl(mid);
        }
        inline const t& get(const it& x) const {
            return ge(x + 1, rt)->s;
        }
        inline void rmall(const t& x) {
            node* a = nullptr, * b = nullptr, * mid = nullptr;
            fll(rt, x, a, b);
            flr(b, x, mid, b);
            rt = hb(a, b);
            cl(mid);
        }
        inline void erase(const t& x, const it& len = 1) {
            node* a = nullptr, * b = nullptr, * mid = nullptr;
            fll(rt, x, a, b);
            fls(b, len, mid, b);
            rt = hb(a, b);
            cl(mid);
        }
        inline const it find(const t& x) const {
            const it w = lb(x, rt) + 1;
            if (w <= gsl(rt) && ge(w, rt)->s == x)return w - 1;
            return gsl(rt);
        }
        inline const it lower_bound(const t& x) const {
            return lb(x, rt);
        }
        inline const it upper_bound(const t& x) const {
            return ub(x, rt);
        }
        inline const it count(const t& x) const {
            node* a = nullptr, * b = nullptr, * mid = nullptr;
            fll(rt, x, a, b);
            flr(b, x, mid, b);
            const it out = gsl(mid);
            rt = hb(hb(a, mid), b);
            return out;
        }
        inline const bool isin(const t& x) const {
            return ch(x, rt);
        }
        inline const it size() const {
            return gsl(rt);
        }
        inline void clear() {
            cl(rt);
        }
        inline void from(const treap<t, rn>& x) {
            cl(rt);
            rt = cp(x.rt);
        }
        inline void swap(treap<t, rn>& x) {
            const node* ls = rt;
            rt = x.rt;
            x.rt = ls;
        }
        inline treap<t, rn> copy() {
            treap<t, rn> out;
            out.rt = cp(rt);
            return out;
        }
        inline const t& operator[](const it& x) const {
            return get(x);
        }
        inline treap& operator=(const treap<t, rn>& x) {
            from(x);
            return *this;
        }
        inline bool operator<(const treap<t, rn>& x) const {
            return size() < x.size();
        }

#undef it
#undef gll(x)
#undef grl(x)
#undef gsl(x)
    };
}
using namespace ftrees;

其他

本来是写了一个可持久化版本,可以实现非常抽象的 O(1) 复制与对数级空间(参考 rope),结果发现全是浅拷贝内存处理跟屎一样,除了只能过板子题还常数贼大一点用没有,白花了两小时,哭死。