数据结构模板

· · 个人记录

前言

把以后可能会用到的数据结构的高弹性(?)模板放上来。

我是重工业选手,习惯写长板子。如果需要可以自取。

由于本人很菜,不会可持久化,所以以后再填坑吧。

正题

线段树

需要提供的接口:

代码:

template <typename _Tp>
struct SegTree {
    _Tp value = _Tp();
    SegTree *left = nullptr, *right = nullptr;
    void pushup() { value = merge(left->value, right->value); }
    void pushdown() { value.split(left->value, right->value); }
    void update(int il, int ir, const _Tp& iv) {
        pushdown();
        if (il <= value.l && ir <= value.r) {
            value = iv;
            return;
        }
        int mid = (value.l + value.r) >> 1;
        if (value.l <= mid) left->update(il, ir, iv);
        if (value.r > mid) right->update(il, ir, iv);
        pushup();
    }
    // 可以加一些你想要的修改操作
    _Tp query_sum(int il, int ir) {
        if (il <= value.l && value.r <= ir) return value;
        pushdown();
        int mid = (value.l + value.r) >> 1;
        int ans = 0;
        if (value.l <= mid) ans = left->query_sum(il, ir);
        if (mid < value.r) ans = merge(ans, right->query_sum(il, ir));
        return ans;
    }
    void build(const _Tp *a, int il, int ir);
    SegTree(const _Tp &dat = _Tp(), SegTree *lt = nullptr, SegTree *rt = nullptr)
        : value(dat), left(lt), right(rt) {}
    ~SegTree() = default;
};
template <typename _Tp>
void SegTree<_Tp>::build(const _Tp *a, int il, int ir) {
    if (il == ir) {
        value = a[il];
        return;
    }
    int mid = (value.l + value.r) >> 1;
    left = new SegTree<_Tp>(_Tp(il, mid));
    right = new SegTree<_Tp>(_Tp(mid + 1, ir));
    left->build(a, value.l, mid);
    right->build(a, mid + 1, value.r);
    pushup();
}

树状数组

暂时没有高弹性接口。

平衡树(FHQ-Treap && Splay)

以后剩下的几种平衡树也会填坑。

共用代码

需要提供的接口:

inline bool my_is_nullptr(void *ptr) { return (long long)ptr <= 0ll; }
template <typename _Tp, typename _UIntType>
class Node {
public:
    int size, cnt;
    _Tp value;
    _UIntType priority;
    // 可以加上一个 tag 成员变量
    Node *left, *right, *parent;
    inline Node(int s = 0, int c = 0, const _Tp &v = _Tp(), const _UIntType &pro = _UIntType(), /*bool is_reversed = false,*/ Node *l = nullptr, Node *r = nullptr, Node *p = nullptr)
        : size(s), cnt(c), value(v), priority(pro), /*tag(is_reversed),*/ left(l), right(r), parent(p) {} // 这里可以自己改,想做区间操作就得有 tag
    ~Node() = default;
    inline Node<_Tp, _UIntType> &operator=(const Node<_Tp, _UIntType> &) = default;
    inline void pushdown() {} // 这里可以自己改,想做区间操作就得有 tag
    inline void update() {
        size = cnt;
        if (!my_is_nullptr(left)) size += left->size;
        if (!my_is_nullptr(right)) size += right->size;
    }
};

普通平衡树(暂时不完善)

需要提供的接口:

struct my_rand {
    typedef unsigned int result_type;
    static constexpr unsigned int kMul = 0xeb382d69u;
    unsigned int _M_seed;
    inline unsigned int fingerPrint(unsigned int x) { return x *= kMul, x ^= x >> 23, x *= kMul, x ^= x >> 23, x *= kMul, x ^= x >> 23, x * kMul; }
    inline unsigned int operator()() { return _M_seed += fingerPrint(_M_seed); }
    inline my_rand(unsigned int sd = 5489u) : _M_seed(sd) {}
};
template <typename _Tp, typename _KeyOfValue, typename _Comp = std::less<_Tp>, typename _Rand = my_rand>
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 nrotate_treap(const typename _Rand::result_type& seed) : rd(seed) {}
    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, _KeyOfValue, _Comp, _Rand>::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, _KeyOfValue, _Comp, _Rand>::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, _KeyOfValue, _Comp, _Rand>::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, _KeyOfValue, _Comp, _Rand>::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, 1, v, rd(), nullptr, nullptr);
    else {
        root = merge(merge(l, tmp, priority_comp), r, priority_comp);
        return false;
    }
    root = merge(merge(l, tmp, priority_comp), r, priority_comp);
    return true;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void nrotate_treap<_Tp, _KeyOfValue, _Comp, _Rand>::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, 1, v, rd(), nullptr, nullptr);
    else {
        ++tmp->cnt;
        ++tmp->size;
    }
    root = merge(merge(l, tmp, priority_comp), r, priority_comp);
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void nrotate_treap<_Tp, _KeyOfValue, _Comp, _Rand>::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);
    }
    root = merge(merge(l, tmp, priority_comp), r, priority_comp);
    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, _KeyOfValue, _Comp, _Rand>::kth(int index) { return ::kth(root, index); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline int nrotate_treap<_Tp, _KeyOfValue, _Comp, _Rand>::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, _KeyOfValue, _Comp, _Rand>::lower_bound(const _Tp &v) { return this->kth(Rank(v)); }
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline _Tp nrotate_treap<_Tp, _KeyOfValue, _Comp, _Rand>::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, _KeyOfValue, _Comp, _Rand>::equal_range(const _Tp &v) { return std::make_pair(lower_bound(v), upper_bound(v)); }

///////////////////////////////
// ^^^ FHQ-Treap | Splay vvv //

template <typename _Tp>
void _splay_rotate(Node<_Tp> *x) {
    Node<_Tp> *y = x->parent, *z = y->parent;
    bool k = (x == y->right);
    if (k) {
        y->right = x->left;
        if (x->left) x->left->parent = y;
        x->left = y;
    }
    else {
        y->left = x->right;
        if (x->right) x->right->parent = y;
        x->right = y;
    }
    y->parent = x;
    x->parent = z;
    if (z) {
        if (z->right == y) z->right = x;
        else z->left = x;
    }
    y->pushup();
    x->pushup();
}
template <typename _Tp, typename _Comp = std::less<_Tp>, typename _Alloc = std::allocator<_Tp> >
class splay_tree {
    typename _Alloc::template rebind<Node<_Tp> >::other _M_alloc;
    _Comp _M_comp;
    Node<_Tp> *_M_root;
    void _M_splay(Node<_Tp> *x) {
        for (Node<_Tp> *f = x->parent; f = x->parent, f; _splay_rotate(x))
            if (f->parent) _splay_rotate((x == x->parent->right) == (f == f->parent->right) ? f : x);
        _M_root = x;
    }
public:
    splay_tree() : _M_root(nullptr) {}
    void insert_equal(const _Tp& value) {
        if (!_M_root) {
            _M_root = _M_alloc.allocate(1);
            _M_root->value = value;
            _M_root->size = _M_root->cnt = 1;
            _M_root->left = _M_root->right = _M_root->parent = nullptr;
            return;
        }
        Node<_Tp> *cur = _M_root, *cur_fa = _M_root;
        bool flg = false;
        while (cur) {
            flg = _M_comp(cur->value, value);
            if (!flg && !_M_comp(value, cur->value)) {
                cur->cnt++;
                cur->size++;
                cur_fa->size++;
                _M_splay(cur);
                return;
            }
            cur_fa = cur;
            cur = flg ? cur->right : cur->left;
        }
        cur = (flg ? cur_fa->right : cur_fa->left) = _M_alloc.allocate(1);
        cur->size = cur->cnt = 1;
        cur->left = cur->right = nullptr;
        cur->parent = cur_fa;
        cur->value = value;
        _M_splay(cur);
    }
    bool insert_unique(const _Tp& value) {
        if (!_M_root) {
            _M_root = _M_alloc.allocate(1);
            _M_root->value = value;
            _M_root->size = _M_root->cnt = 1;
            _M_root->left = _M_root->right = _M_root->parent = nullptr;
            return true;
        }
        Node<_Tp> *cur = _M_root, *cur_fa = _M_root;
        bool flg = false;
        while (cur) {
            flg = _M_comp(cur->value, value);
            if (!flg && !_M_comp(value, cur->value)) {
                _M_splay(cur);
                return false;
            }
            cur_fa = cur;
            cur = flg ? cur->right : cur->left;
        }
        cur = (flg ? cur_fa->right : cur_fa->left) = _M_alloc.allocate(1);
        cur->size = cur->cnt = 1;
        cur->left = cur->right = nullptr;
        cur->parent = cur_fa;
        cur->value = value;
        _M_splay(cur);
        return true;
    }
    _Tp kth(size_t rk) {
        Node<_Tp> *cur = _M_root, *cur_fa = _M_root;
        while (cur) {
            size_t ls_siz = cur->left ? cur->left->size : 0;
            cur_fa = cur;
            if (rk <= ls_siz) cur = cur->left;
            else if (rk <= ls_siz + cur->cnt) {
                _M_splay(cur);
                return cur->value;
            }
            else {
                rk -= ls_siz + cur->cnt;
                cur = cur->right;
            }
        }
        return cur_fa->value;
    }
    void splay_value(const _Tp& value) {
        Node<_Tp> *cur = _M_root, *cur_fa = _M_root;
        while (cur) {
            bool flg = _M_comp(value, cur->value);
            if (!flg && !_M_comp(cur->value, value)) {
                _M_splay(cur);
                return;
            }
            cur_fa = cur;
            cur = flg ? cur->left : cur->right;
        }
        _M_splay(cur_fa);
    }
    size_t rank(const _Tp& value) {
        size_t res = 0;
        Node<_Tp> *cur = _M_root;
        while (cur) {
            if (_M_comp(value, cur->value)) cur = cur->left;
            else {
                if (cur->left) res += cur->left->size;
                if (_M_comp(cur->value, value)) {
                    res += cur->cnt;
                    cur = cur->right;
                }
                else {
                    _M_splay(cur);
                    return res;
                }
            }
        }
        return res + 1;
    }
    operator const Node<_Tp>*() const { return _M_root; }
    _Tp lower_bound(const _Tp& value) {
        if (!_M_root) return;
        splay_value(value);
        return _M_root->value;
    }
    _Tp upper_bound(const _Tp& value) {
        if (!_M_root) return;
        splay_value(value);
        if (_M_comp(value, _M_root->value)) return;
        Node<_Tp> *cur = _M_root->right;
        if (!cur) return;
        while (cur->left) cur = cur->left;
        _M_splay(cur);
        return cur->value;
    }
    void erase(const _Tp& value) {
        if (!_M_root) return;
        splay_value(value);
        if (_M_root->cnt > 1) {
            _M_root->size--;
            _M_root->cnt--;
            return;
        }
        if (!_M_root->left && !_M_root->right) {
            _M_alloc.deallocate(_M_root, 1);
            _M_root = nullptr;
            return;
        }
        if (!_M_root->left) {
            Node<_Tp> *cur = _M_root;
            _M_root = _M_root->right;
            _M_root->parent = nullptr;
            _M_alloc.deallocate(cur, 1);
            cur = nullptr;
            return;
        }
        if (!_M_root->right) {
            Node<_Tp> *cur = _M_root;
            _M_root = _M_root->left;
            _M_root->parent = nullptr;
            _M_alloc.deallocate(cur, 1);
            cur = nullptr;
            return;
        }
        Node<_Tp> *cur = _M_root;
        splay_pre_to_root(value);
        cur->right->parent = _M_root;
        _M_root->right = cur->right;
        _M_alloc.deallocate(cur, 1);
        cur = nullptr;
        _M_root->pushup();
    }
};

文艺平衡树

需要提供的接口:

代码:(这里使用区间反转的文艺平衡树为例)

template <typename _Tp, typename _Rand>
class segment_nrotate_treap {
    typedef typename _Rand::result_type _UIntType;
    typedef Node<_Tp, _UIntType>* node_type;
    _KeyOfValue _M_key_of_value;
    _Comp _M_comp;
    node_type _M_root;
    _Rand rd;
    std::less<_UIntType> _M_priority_comp;
    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, size_t rk, Node<_Tp, _UIntType> *&l, 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 segment_nrotate_treap() = default;
    inline void reverse(size_t l, size_t r) {
        node_type left = nullptr, mid = nullptr, right = nullptr;
        split_by_rank(_M_root, r, mid, right);
        split_by_rank(mid, l - 1, left, mid);
        if (!my_is_nullptr(mid)) {
            mid->tag = true;
            mid->pushdown();
        }
        _M_root = merge(left, merge(mid, right, _M_priority_comp), _M_priority_comp);
        if (!my_is_nullptr(_M_root)) _M_root->update();
    }
    inline void push_back(const _Tp& value) { _M_root = merge(_M_root, new Node<_Tp, typename _Rand::result_type>(1, 1, value, rd(), false, nullptr, nullptr), _M_priority_comp); }
   inline void push_front(const _Tp& value) { _M_root = merge(new Node<_Tp, typename _Rand::result_type>(1, 1, value, rd(), false, nullptr, nullptr), _M_root, _M_priority_comp); }
    template <typename _Ostream>
    friend _Ostream& operator<<(_Ostream& os, segment_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp> o) { return os << o._M_root; }
};
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
inline void segment_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>
void segment_nrotate_treap<_Tp, _Rand, _KeyOfValue, _Comp>::split_by_rank(Node<_Tp, _UIntType>* cur, size_t rk, Node<_Tp, _UIntType> *&l, Node<_Tp, _UIntType> *&r) {
    if (my_is_nullptr(cur)) {
        l = r = nullptr;
        return;
    }
    cur->pushdown();
    size_t ls_siz = my_is_nullptr(cur->left) ? 0 : cur->left->size;
    if (rk <= ls_siz) {
        r = cur;
        r->pushdown();
        r->update();
        split_by_rank(r->left, rk, l, r->left);
        cur->update();
        cur->pushdown();
        return;
    }
    l = cur;
    l->pushdown();
    l->update();
    split_by_rank(l->right, rk - ls_siz - 1, l->right, r);
    cur->update();
    cur->pushdown();
    return;
}
template <typename _Tp, typename _Rand, typename _KeyOfValue, typename _Comp>
template <typename _Priority_comp>
Node<_Tp, typename _Rand::result_type> *segment_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 (my_is_nullptr(l)) return r;
    if (my_is_nullptr(r)) return l;
    l->pushdown();
    r->pushdown();
    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;
}

ST 表

类型需要满足的接口:

函数指针自己看。

代码:(sparse_table 只能用来做 \color{red}\min\;\max 这两种操作)

template <typename _Tp, size_t _MAXN>
class normal_sparse_table {
    _Tp (*_M_func)(const _Tp&, const _Tp&);
    static constexpr size_t _S_size = std::__lg(_MAXN) + 1;
    _Tp _M_table[_S_size][_MAXN];
    inline void init() { for (size_t j = 1; j < _S_size; j++) for (size_t i = 0; i <= _MAXN - (1 << j); i++) _M_table[j][i] = _M_func(_M_table[j - 1][i], _M_table[j - 1][i + (1 << (j - 1))]); }
public:
    normal_sparse_table() = default;
    template <typename _InputIterator>
    normal_sparse_table(_InputIterator __first, _InputIterator __last, _Tp (*func)(const _Tp&, const _Tp&)) : _M_func(func) {
        std::copy(__first, __last, _M_table[0]);
        init();
    }
    normal_sparse_table<_Tp, _MAXN>& operator=(const normal_sparse_table<_Tp, _MAXN>& o) {
        for (size_t i = 0; i < _S_size; i++) memcpy(_M_table[i], o._M_table[i], _MAXN * sizeof(_Tp));
        _M_func = o._M_func;
        return *this;
    }
    // query [l,r]
    inline _Tp query(size_t l, size_t r) {
        size_t k = std::__lg(r - l + 1);
        return _M_func(_M_table[k][l], _M_table[k][r - (1 << k) + 1]);
    }
};
template <typename _Tp, size_t _MAXN>
class sparse_table {
    _Tp(*_M_func)(const _Tp&, const _Tp&);
    static constexpr size_t _S_blk_siz = std::__lg(_MAXN) + 1;
    static constexpr size_t _S_blk_cnt = _MAXN / _S_blk_siz + 5;
    _Tp _M_pre[_S_blk_cnt * _S_blk_siz], _M_suf[_S_blk_cnt * _S_blk_siz];
    normal_sparse_table<_Tp, _S_blk_cnt> _M_st_table;
    template <typename _InputIterator>
    inline void init(_InputIterator __first, _InputIterator __last) {
        std::fill(std::copy(__first, __last, _M_pre), _M_pre + _S_blk_cnt * _S_blk_siz, _Tp());
        std::fill(std::copy(__first, __last, _M_suf), _M_suf + _S_blk_cnt * _S_blk_siz, _Tp());
        _Tp *tmp_arr = new _Tp[_S_blk_cnt + 1];
        size_t bl = 0, br = std::min(_S_blk_siz - 1, _MAXN);
        for (size_t k = 1; k <= br; k++) _M_pre[k] = _M_func(_M_pre[k - 1], _M_pre[k]);
        for (size_t k = br - 1; ~k; k--) _M_suf[k] = _M_func(_M_suf[k + 1], _M_suf[k]);
        tmp_arr[0] = _M_suf[0];
        bl = _S_blk_siz;
        if (bl >= _MAXN) {
            _M_st_table = normal_sparse_table<_Tp, _S_blk_cnt>(tmp_arr, tmp_arr + 1, _M_func);
            return;
        }
        size_t curpos = 1;
        br = (_S_blk_siz << 1) - 1;
        for (; bl < _MAXN; bl += _S_blk_siz, br += _S_blk_siz) {
            br = std::min(br, _MAXN);
            for (size_t k = bl + 1; k <= br; k++) _M_pre[k] = _M_func(_M_pre[k - 1], _M_pre[k]);
            for (size_t k = br - 1; k >= bl; k--) _M_suf[k] = _M_func(_M_suf[k + 1], _M_suf[k]);
            tmp_arr[curpos++] = _M_suf[bl];
        }
        _M_st_table = normal_sparse_table<_Tp, _S_blk_cnt>(tmp_arr, tmp_arr + curpos, _M_func);
    }
public:
    template <typename _InputIterator>
    sparse_table(_InputIterator __first, _InputIterator __last, _Tp(*func)(const _Tp&, const _Tp&)) : _M_func(func) { init(__first, __last); }
    // query [l,r]
    _Tp query(_Tp* arr, size_t l, size_t r) {
        size_t bl = l / _S_blk_siz, br = r / _S_blk_siz;
        _Tp ret{};
        if (bl == br) {
            for (size_t i = l; i <= r; i++) ret = _M_func(ret, arr[i]);
            return ret;
        }
        if (bl == br - 1) return _M_func(_M_suf[l], _M_pre[r]);
        return _M_func(_M_func(_M_suf[l], _M_pre[r]), _M_st_table.query(bl + 1, br - 1));
    }
};

堆

本数据结构以算法形式呈现。

类型需要满足的接口:

对容器的要求:

代码:

#define LEFT_SON(p) (p << 1 | 1)
#define RIGHT_SON(p) ((p + 1) << 1)
#define PARENT(p) ((p - 1) >> 1)

template <typename _RandomAccessIterator, typename _Diff, typename _Comp>
inline void min_heap_adjust(_RandomAccessIterator __first, _RandomAccessIterator __last,
    _Diff __index, _Comp comp) {
    _Diff siz = __last - __first;
    _Diff tar = LEFT_SON(__index);
    while (tar < siz) {
        _RandomAccessIterator child = __first + tar;
        if (tar < siz - 1) {
            if (comp(*(child + 1), *child)) {
                ++tar;
                ++child;
            }
        }
        if (comp(*child, *(__first + __index))) std::iter_swap(__first + __index, child);
        else break;
        __index = tar;
        tar = LEFT_SON(__index);
    }
}
template <typename _RandomAccessIterator, typename _Comp>
inline void pop_min_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp comp) {
    --__last;
    std::iter_swap(__first, __last);
    min_heap_adjust(__first, __last, 0, comp);
}
template <typename _RandomAccessIterator, typename _Comp>
inline void push_min_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp comp) {
    typedef typename std::iterator_traits< _RandomAccessIterator>::difference_type _Diff;
    _Diff pos = (__last - __first) - 1;
    while (pos && comp(*(__first + pos), *(__first + PARENT(pos)))) {
        std::iter_swap(__first + pos, __first + PARENT(pos));
        pos = PARENT(pos);
    }
}
template <typename _RandomAccessIterator, typename _Comp>
inline void make_min_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp comp) {
    typedef typename std::iterator_traits< _RandomAccessIterator>::difference_type _Diff;
    _Diff siz = __last - __first;
    _Diff parent = PARENT(siz);
    while (true) {
        min_heap_adjust(__first, __last, parent, comp);
        if (!parent) return;
        --parent;
    }
}

template <typename _RandomAccessIterator, typename _Diff, typename _Comp>
inline void max_heap_adjust(_RandomAccessIterator __first, _RandomAccessIterator __last,
    _Diff __index, _Comp comp) {
    _Diff siz = __last - __first;
    _Diff tar = LEFT_SON(__index);
    while (tar < siz) {
        _RandomAccessIterator child = __first + tar;
        if (tar < siz - 1) {
            if (comp(*child, *(child + 1))) {
                ++tar;
                ++child;
            }
        }
        if (comp(*(__first + __index), *child)) std::iter_swap(__first + __index, child);
        else break;
        __index = tar;
        tar = LEFT_SON(__index);
    }
}
template <typename _RandomAccessIterator, typename _Comp>
inline void pop_max_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp comp) {
    --__last;
    std::iter_swap(__first, __last);
    max_heap_adjust(__first, __last, 0, comp);
}
template <typename _RandomAccessIterator, typename _Comp>
inline void push_max_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp comp) {
    typedef typename std::iterator_traits< _RandomAccessIterator>::difference_type _Diff;
    _Diff pos = (__last - __first) - 1;
    while (pos && comp(*(__first + PARENT(pos)), *(__first + pos))) {
        std::iter_swap(__first + pos, __first + PARENT(pos));
        pos = PARENT(pos);
    }
}
template <typename _RandomAccessIterator, typename _Comp>
inline void make_max_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp comp) {
    typedef typename std::iterator_traits< _RandomAccessIterator>::difference_type _Diff;
    _Diff siz = __last - __first;
    _Diff parent = PARENT(siz);
    while (true) {
        max_heap_adjust(__first, __last, parent, comp);
        if (!parent) return;
        --parent;
    }
}
#undef LEFT_SON
#undef RIGHT_SON
#undef PARENT

珂朵莉树

注意,均为左闭右开区间

需要满足的接口:

代码:

#define _M_all_split(__first, __last) iterator itr = _M_split(__last), itl = _M_split(__first)
template <class _Tp, class _Alloc = std::allocator<_Tp> >
class old_driver_tree {
    struct odt_node {
        size_t l, r;
        mutable _Tp v;
        inline odt_node(size_t il, size_t ir, const _Tp& value) : l(il), r(ir), v(value) {}
        inline odt_node(size_t il = 0) : l(il), r(0), v() {}
        inline bool operator<(const odt_node& o) const { return l < o.l; }
    };
    typedef typename std::allocator_traits<_Alloc>::template rebind_alloc<odt_node> _Node_alloc_type;
    typedef std::allocator_traits<_Node_alloc_type> _Node_alloc_traits;
    std::set<odt_node, std::less<odt_node>, _Node_alloc_type> _M_tree;
public:
    typedef const odt_node value_type;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef _Alloc allocator_type;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::iterator iterator;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::const_iterator const_iterator;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::reverse_iterator reverse_iterator;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::const_reverse_iterator const_reverse_iterator;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::pointer pointer;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::const_pointer const_pointer;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::reference reference;
    typedef typename std::set<odt_node, std::less<odt_node>, _Node_alloc_type>::const_reference const_reference;
    inline const_iterator begin() const { return _M_tree.begin(); }
    inline const_iterator end() const { return _M_tree.end(); }
    inline const_iterator cbegin() const { return _M_tree.cbegin(); }
    inline const_iterator cend() const { return _M_tree.cend(); }
    inline const_reverse_iterator rbegin() const { return _M_tree.rbegin(); }
    inline const_reverse_iterator rend() const { return _M_tree.rend(); }
    inline const_reverse_iterator crbegin() const { return _M_tree.crbegin(); }
    inline const_reverse_iterator crend() const { return _M_tree.crend(); }
    inline size_type size() const { return _M_tree.size(); }
    inline bool empty() const { return _M_tree.empty(); }
    inline void clear() { _M_tree.clear(); }
    inline iterator insert(size_t l, size_t r, const _Tp& value) { return _M_tree.insert(odt_node(l, r, value)); }
private:
    inline iterator _M_split(iterator it) {
        iterator lb = _M_tree.lower_bound(it);
        if (lb != _M_tree.end() && it->l == lb->l) return lb;
        --lb;
        size_t il = lb->l, ir = lb->r;
        _Tp value(lb->v);
        _M_tree.erase(lb);
        _M_tree.insert(odt_node(il, it->l - 1, value));
        return _M_tree.insert(odt_node(it->l, ir, value)).first;
    }
    inline iterator _M_split(size_t x) {
        iterator lb = _M_tree.lower_bound(odt_node(x, 0, _Tp()));
        if (lb != _M_tree.end() && x == lb->l) return lb;
        --lb;
        size_t il = lb->l, ir = lb->r;
        _Tp value(lb->v);
        _M_tree.erase(lb);
        _M_tree.insert(odt_node(il, x - 1, value));
        return _M_tree.insert(odt_node(x, ir, value)).first;
    }
public:
    inline void insert(iterator __it) { _M_tree.insert(__it); }
    template <typename... _Up>
    inline void emplace(_Up... args) { _M_tree.emplace(args...); }
    inline void assign(iterator __first, iterator __last, const _Tp& value) {
        _M_all_split(__first, __last);
        _M_tree.erase(itl, itr);
        _M_tree.insert(odt_node(__first->l, __last->r, value));
    }
    inline void operator()(iterator __first, iterator __last, const std::function<_Tp(const _Tp&)>& func) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) itl->v = func(itl->v);
    }
    inline void operator()(iterator __first, iterator __last, _Tp(*func)(const _Tp&)) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) itl->v = func(itl->v);
    }
    inline void operator()(iterator __first, iterator __last, const std::function<void(_Tp&)>& func) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) func(itl->v);
    }
    inline void operator()(iterator __first, iterator __last, void(*func)(_Tp&)) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) func(itl->v);
    }
    inline _Tp operator()(iterator __first, iterator __last, const std::function<_Tp(iterator, iterator)>& func) {
        _M_all_split(__first, __last);
        return func(itl, itr);
    }
    inline _Tp operator()(size_t __first, size_t __last, _Tp(*func)(iterator, iterator)) {
        _M_all_split(__first, __last);
        return func(itl, itr);
    }
    inline _Tp operator()(iterator __first, iterator __last, const std::function<_Tp(const std::pair<iterator, iterator>&)>& func) {
        _M_all_split(__first, __last);
        return func(std::make_pair(itl, itr));
    }
    inline _Tp operator()(iterator __first, iterator __last, _Tp(*func)(const std::pair<iterator, iterator>&)) {
        _M_all_split(__first, __last);
        return func(std::make_pair(itl, itr));
    }
    inline void assign(size_t __first, size_t __last, const _Tp& value) {
        _M_all_split(__first, __last);
        _M_tree.erase(itl, itr);
        _M_tree.insert(odt_node(__first, __last - 1, value));
    }
    inline void operator()(size_t __first, size_t __last, const std::function<_Tp(const _Tp&)>& func) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) itl->v = func(itl->v);
    }
    inline void operator()(size_t __first, size_t __last, _Tp(*func)(const _Tp&)) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) itl->v = func(itl->v);
    }
    inline void operator()(size_t __first, size_t __last, const std::function<void(_Tp&)>& func) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) func(itl->v);
    }
    inline void operator()(size_t __first, size_t __last, void(*func)(_Tp&)) {
        _M_all_split(__first, __last);
        for (; itl != itr; ++itl) func(itl->v);
    }
    inline _Tp operator()(size_t __first, size_t __last, const std::function<_Tp(iterator, iterator)>& func) {
        _M_all_split(__first, __last);
        return func(itl, itr);
    }
    inline _Tp operator()(size_t __first, size_t __last, _Tp(*func)(iterator, iterator)) {
        _M_all_split(__first, __last);
        return func(itl, itr);
    }
    inline _Tp operator()(size_t __first, size_t __last, const std::function<_Tp(const std::pair<iterator, iterator>&)>& func) {
        _M_all_split(__first, __last);
        return func(std::make_pair(itl, itr));
    }
    inline _Tp operator()(size_t __first, size_t __last, _Tp(*func)(const std::pair<iterator, iterator>&)) {
        _M_all_split(__first, __last);
        return func(std::make_pair(itl, itr));
    }
};
#undef _M_all_split

如还有其他数据结构请私信我。