跳到正文

题解

题解 P3380 【【模板】二逼平衡树(树套树)】

w1049约 14 分钟
原文
文章信息与版本

文章信息

发布
2019年8月8日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 14 分钟
题目
P3380

版本

  1. v1当前

写了一个自己觉得挺好看的线段树套Splay, 分享一下

(之前线段树的l==x&&y==r一直写成l==r,调到吐血) (之前写了个内存池,但是发现好像负优化(?))

C++
#include <cstdio>
#include <cctype>
#include <algorithm>
template<class T>
static void read(T &x){x=0;char c=getchar();while(!isdigit(c))c=getchar();while(isdigit(c))x=x*10+(c^'0'),c=getchar();}
using namespace std;
const int N = 50010;
struct Splay{
    struct splayNode{
        int val, size, cnt;
        splayNode *f, *ch[2];
        int dr() { return f->ch[1] == this; }
        void update() { size = ch[0]->size + ch[1]->size + cnt; }
    };
    static splayNode *null;
    splayNode *root;
    Splay() {
        if(!null) {
            null = new splayNode;
            null->ch[0] = null->ch[1] = null;
            null->size = null->val = null->cnt = 0;
        }
        root = null;
    }
    splayNode* newNode(int val, splayNode *f = null) {
        splayNode *node = new splayNode;
        node->val = val;
        node->ch[0] = node->ch[1] = null;
        node->size = node->cnt = 1;
        node->f = f;
        return node;
    }
    void rotate(splayNode *x) {
        int dr = x->dr();
        splayNode *f = x->f, *gf = f->f;
        (gf != null) && (gf->ch[f->dr()] = x), x->f = gf;
        f->ch[dr] = x->ch[dr ^ 1], (f->ch[dr] != null) && (f->ch[dr]->f = f);
        x->ch[dr ^ 1] = f, f->f = x;
        f->update(), x->update();
    }
    void splay(splayNode *x, splayNode *goal = null) {
        for (splayNode *f; (f = x->f) != goal; rotate(x))
            if (f->f != goal) rotate(f->dr() == x->dr() ? f : x);
        if (goal == null) root = x;
    }
    void insert(int val) {
        splayNode *now = root, *f = null;
        while (now != null && now->val != val) f = now, now = now->ch[val > now->val];
        if (now == null) {
            now = newNode(val, f);
            if (f != null) f->ch[val > f->val] = now;
        } else now->cnt++;
        splay(now);
    }
    int kth(int k) {
        splayNode *now = root;
        while(1) {
            if (k <= now->ch[0]->size) now = now->ch[0];
            else if (k > now->ch[0]->size + now->cnt)
                k -= now->ch[0]->size + now->cnt, now = now->ch[1];
            else return now->val;
        }
    }
    int rank(int val) {
    	splayNode *now = root;
    	int res = 0;
        while (now != null) {
        	if (now->val == val) return res + now->ch[0]->size;
        	else if (now->val < val) res += now->ch[0]->size + now->cnt, now = now->ch[1];
        	else now = now->ch[0];
		}
		return res;
    }
    void find(int val) {
        if (root == null) return;
        splayNode *now = root;
        while (now->val != val && now->ch[val > now->val] != null)
            now = now->ch[val > now->val];
        splay(now);
    }
    int getPre(splayNode *now, int val) {
        if (now == null) return -2147483647;
        if (now->val >= val) return getPre(now->ch[0], val);
        else return max(now->val, getPre(now->ch[1], val));
    }
    int getNext(splayNode *now, int val) {
        if (now == null) return 2147483647;
        if (now->val <= val) return getNext(now->ch[1], val);
        else return min(now->val, getNext(now->ch[0], val));
    }
    int getPre(int val) { return getPre(root, val); }
    int getNext(int val) { return getNext(root, val); }
    void remove(int val) {
        if (root == null) return;
        find(val);
        if (root->cnt > 1) --root->cnt, root->update();
        else if(root->ch[0] == null) root = root->ch[1], (root != null) && (root->f = null);
        else {
            splayNode *now = root->ch[0];
            while (now->ch[1] != null) now = now->ch[1];
            splay(now, root);
            now->ch[1] = root->ch[1], (now->ch[1] != null) && (now->ch[1]->f = now);
            root = now, root->f = null, root->update();
        }
    }
    void print() { print(root), puts(""); }
    void print(splayNode *node) {
        if (node == null) return;
        print(node->ch[0]);
        printf("%d ", node->val);
        print(node->ch[1]);
    }
};
Splay::splayNode *Splay::null;
struct SegmentTree{
	Splay *t[N * 4];
    int root, n, maxv;
    int *data;
    SegmentTree(int *a, int n): root(1), n(n), data(a) { build(root, 1, n); }
    #define lson now<<1
    #define rson now<<1|1
    #define mid ((l+r)>>1)
    void build(int now, int l, int r) {
    	t[now] = new Splay;
        for (int i = l; i <= r; i++) t[now]->insert(data[i]);
        if (l == r) { maxv = max(data[l], maxv); return; }
        build(lson, l, mid);
        build(rson, mid + 1, r);
    }
    void modify(int now, int l, int r, int p, int val) {
        t[now]->remove(data[p]), t[now]->insert(val);
        if (l == r) { data[p] = val; maxv = max(val, maxv); return; }
        if (p <= mid) modify(lson, l, mid, p, val);
        else modify(rson, mid + 1, r, p, val);
    }
    int getPre(int now, int l, int r, int x, int y, int val) {
        if (l == x && y == r) return t[now]->getPre(val);
        if (y <= mid) return getPre(lson, l, mid, x, y, val);
        else if (x > mid) return getPre(rson, mid + 1, r, x, y, val);
        else return max(getPre(lson, l, mid, x, mid, val), getPre(rson, mid + 1, r, mid + 1, y, val));
    }
    int getNext(int now, int l, int r, int x, int y, int val) {
        if (l == x && y == r) return t[now]->getNext(val);
        if (y <= mid) return getNext(lson, l, mid, x, y, val);
        else if (x > mid) return getNext(rson, mid + 1, r, x, y, val);
        else return min(getNext(lson, l, mid, x, mid, val), getNext(rson, mid + 1, r, mid + 1, y, val));
    }
    int rank(int now, int l, int r, int x, int y, int val) {
        if (l == x && y == r) return t[now]->rank(val);
        if (y <= mid) return rank(lson, l, mid, x, y, val);
        else if (x > mid) return rank(rson, mid + 1, r, x, y, val);
        else return rank(lson, l, mid, x, mid, val) + rank(rson, mid + 1, r, mid + 1, y, val);
    }
    #undef mid
    int kth(int x, int y, int k) {
        int l = 0, r = maxv, res = 0;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (rank(x, y, mid) <= k) l = mid + 1, res = mid;
            else r = mid - 1;
        }
        return res;
    }
    void modify(int p, int val) { modify(root, 1, n, p, val); }
    int getPre(int l, int r, int val) { return getPre(root, 1, n, l, r, val); }
    int getNext(int l, int r, int val) { return getNext(root, 1, n, l, r, val); }
    int rank(int l, int r, int val) { return rank(root, 1, n, l, r, val) + 1; }
};
int a[N], n, m;
int main() {
    read(n), read(m);
    for (int i = 1; i <= n; i++) read(a[i]);
    SegmentTree Tree(a, n);
    for (int i = 1, opt, a, b, c; i <= m; i++) {
        read(opt), read(a), read(b);
        switch (opt) {
            case 1: read(c), printf("%d\n", Tree.rank(a, b, c)); break;
            case 2: read(c), printf("%d\n", Tree.kth(a, b, c)); break;
            case 3: Tree.modify(a, b); break;
            case 4: read(c), printf("%d\n", Tree.getPre(a, b, c)); break;
            case 5: read(c), printf("%d\n", Tree.getNext(a, b, c)); break;
        }
    }
    return 0;
}
原文