• 模板


    空间消耗非常玄学,有多大开多大就完事了。其实是因为单次操作可能会有数次Merge和Split操作,按照下面的版本的话Merge和Split都进行复制,所以一次操作可能复制了4个版本。
    四个函数式查询,然后Merge的时候拷贝对应的xy子树,Split的时候拷贝p树。事实上,Merge和Split总是成对出现,只需要在其中喜欢的一个进行可持久化(复制节点)就可以了,比较推荐在Split的时候复制节点。这样单次操作大概复制2个版本。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define ls(p) ch[p][0]
    #define rs(p) ch[p][1]
    
    const int MAXN = 30000000 + 5;
    int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root[MAXN];
    
    void Init() {
        tot = 0;
        memset(root,0,sizeof(root));
    }
    
    int NewNode(int v) {
        ++tot;
        ch[tot][0] = ch[tot][1] = 0;
        val[tot] = v, rnd[tot] = rand();
        siz[tot] = 1;
        return tot;
    }
    
    int CopyNode(int p) {
        ++tot;
        ch[tot][0] = ch[p][0];
        ch[tot][1] = ch[p][1];
        val[tot] = val[p];
        rnd[tot] = rnd[p];
        siz[tot] = siz[p];
        return tot;
    }
    
    void PushUp(int p) {
        siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
    }
    
    void SplitValue(int p, int v, int &x, int &y) {
        if(!p) {
            x = y = 0;
            return;
        }
        if(v < val[p]) {
            y = CopyNode(p);
            SplitValue(ls(y), v, x, ls(y));
            PushUp(y);
        } else {
            x = CopyNode(p);
            SplitValue(rs(x), v, rs(x), y);
            PushUp(x);
        }
    }
    
    /*void SplitRank(int p, int rk, int &x, int &y) {
        if(!p) {
            x = y = 0;
            return;
        }
        if(rk <= siz[ls(p)]) {
            y = p;
            SplitRank(ls(p), rk, x, ls(p));
            PushUp(y);
        } else {
            x = p;
            SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
            PushUp(x);
        }
    }*/
    
    int Merge(int x, int y) {
        if(!x || !y)
            return x | y;
        if(rnd[x] < rnd[y]) {
            int p = CopyNode(x);
            rs(p) = Merge(rs(p), y);
            PushUp(p);
            return p;
        } else {
            int p = CopyNode(y);
            ls(p) = Merge(x, ls(p));
            PushUp(p);
            return p;
        }
    }
    
    void Insert(int &root, int v) {
        int x = 0, y = 0;
        SplitValue(root, v, x, y);
        root = Merge(Merge(x, NewNode(v)), y);
    }
    
    void Remove(int &root, int v) {
        int x = 0, y = 0, z = 0;
        SplitValue(root, v, x, z);
        SplitValue(x, v - 1, x, y);
        y = Merge(ls(y), rs(y));
        root = Merge(Merge(x, y), z);
    }
    
    int GetRank2(int p, int v) {
        int rk = 1;
        while(p) {
            if(v < val[p])
                p = ls(p);
            else if(v == val[p])
                p = ls(p);
            else {
                rk += siz[ls(p)] + 1;
                p = rs(p);
            }
        }
        return rk;
    }
    
    int GetValue2(int p, int rk) {
        while(p) {
            if(rk <= siz[ls(p)])
                p = ls(p);
            else if(rk == siz[ls(p)] + 1)
                return val[p];
            else {
                rk -= siz[ls(p)] + 1;
                p = rs(p);
            }
        }
    }
    
    int GetPrev2(int p, int v) {
        int prev;
        while(p) {
            if(v <= val[p])
                p = ls(p);
            else {
                prev = val[p];
                p = rs(p);
            }
        }
        return prev;
    }
    
    int GetNext2(int p, int v) {
        int next;
        while(p) {
            if(v < val[p]) {
                next = val[p];
                p = ls(p);
            } else
                p = rs(p);
        }
        return next;
    }
    
    int main() {
    #ifdef Yinku
        freopen("Yinku.in", "r", stdin);
    #endif // Yinku
        int n;
        scanf("%d", &n);
        Init();
        for(int i = 1; i <= n; ++i) {
            int v,op, x;
            scanf("%d%d%d",&v, &op, &x);
            root[i]=root[v];
            switch(op) {
                case 1:
                    Insert(root[i], x);
                    break;
                case 2:
                    Remove(root[i], x);
                    break;
                case 3:
                    printf("%d
    ", GetRank2(root[i], x));
                    break;
                case 4:
                    printf("%d
    ", GetValue2(root[i], x));
                    break;
                case 5:
                    printf("%d
    ", GetPrev2(root[i], x));
                    break;
                case 6:
                    printf("%d
    ", GetNext2(root[i], x));
                    break;
            }
        }
        return 0;
    }
    
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define ls(p) ch[p][0]
    #define rs(p) ch[p][1]
    
    const int MAXN = 20000000 + 5;
    int val[MAXN], ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root[MAXN];
    
    void Init() {
        tot = 0;
        memset(root,0,sizeof(root));
    }
    
    int NewNode(int v) {
        ++tot;
        ch[tot][0] = ch[tot][1] = 0;
        val[tot] = v, rnd[tot] = rand();
        siz[tot] = 1;
        return tot;
    }
    
    int CopyNode(int p) {
        ++tot;
        ch[tot][0] = ch[p][0];
        ch[tot][1] = ch[p][1];
        val[tot] = val[p];
        rnd[tot] = rnd[p];
        siz[tot] = siz[p];
        return tot;
    }
    
    void PushUp(int p) {
        siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
    }
    
    void SplitValue(int p, int v, int &x, int &y) {
        if(!p) {
            x = y = 0;
            return;
        }
        if(v < val[p]) {
            y = CopyNode(p);
            SplitValue(ls(y), v, x, ls(y));
            PushUp(y);
        } else {
            x = CopyNode(p);
            SplitValue(rs(x), v, rs(x), y);
            PushUp(x);
        }
    }
    
    /*void SplitRank(int p, int rk, int &x, int &y) {
        if(!p) {
            x = y = 0;
            return;
        }
        if(rk <= siz[ls(p)]) {
            y = p;
            SplitRank(ls(p), rk, x, ls(p));
            PushUp(y);
        } else {
            x = p;
            SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
            PushUp(x);
        }
    }*/
    
    int Merge(int x, int y) {
        if(!x || !y)
            return x | y;
        if(rnd[x] < rnd[y]) {
            //int p = CopyNode(x);
            int p=x;
            rs(p) = Merge(rs(p), y);
            PushUp(p);
            return p;
        } else {
            int p=y;
            //int p = CopyNode(y);
            ls(p) = Merge(x, ls(p));
            PushUp(p);
            return p;
        }
    }
    
    void Insert(int &root, int v) {
        int x = 0, y = 0;
        SplitValue(root, v, x, y);
        root = Merge(Merge(x, NewNode(v)), y);
    }
    
    void Remove(int &root, int v) {
        int x = 0, y = 0, z = 0;
        SplitValue(root, v, x, z);
        SplitValue(x, v - 1, x, y);
        y = Merge(ls(y), rs(y));
        root = Merge(Merge(x, y), z);
    }
    
    int GetRank2(int p, int v) {
        int rk = 1;
        while(p) {
            if(v < val[p])
                p = ls(p);
            else if(v == val[p])
                p = ls(p);
            else {
                rk += siz[ls(p)] + 1;
                p = rs(p);
            }
        }
        return rk;
    }
    
    int GetValue2(int p, int rk) {
        while(p) {
            if(rk <= siz[ls(p)])
                p = ls(p);
            else if(rk == siz[ls(p)] + 1)
                return val[p];
            else {
                rk -= siz[ls(p)] + 1;
                p = rs(p);
            }
        }
    }
    
    int GetPrev2(int p, int v) {
        int prev;
        while(p) {
            if(v <= val[p])
                p = ls(p);
            else {
                prev = val[p];
                p = rs(p);
            }
        }
        return prev;
    }
    
    int GetNext2(int p, int v) {
        int next;
        while(p) {
            if(v < val[p]) {
                next = val[p];
                p = ls(p);
            } else
                p = rs(p);
        }
        return next;
    }
    
    int main() {
    #ifdef Yinku
        freopen("Yinku.in", "r", stdin);
    #endif // Yinku
        int n;
        scanf("%d", &n);
        Init();
        for(int i = 1; i <= n; ++i) {
            int v,op, x;
            scanf("%d%d%d",&v, &op, &x);
            root[i]=root[v];
            switch(op) {
                case 1:
                    Insert(root[i], x);
                    break;
                case 2:
                    Remove(root[i], x);
                    break;
                case 3:
                    printf("%d
    ", GetRank2(root[i], x));
                    break;
                case 4:
                    printf("%d
    ", GetValue2(root[i], x));
                    break;
                case 5:
                    printf("%d
    ", GetPrev2(root[i], x));
                    break;
                case 6:
                    printf("%d
    ", GetNext2(root[i], x));
                    break;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    window 7系统环境同时安装window xp系统,形成双系统
    工作感悟
    数据湖框架选型很纠结?一文了解Apache Hudi核心优势
    mysql数据库设计-规则
    maven中多个子模块的构建顺序
    EXTJS3.0 表单元素TextField datefield 设置只读并改背景颜色为灰色
    MySQL5.7的账号回收权限
    哲学王子-复旦博导王德峰教授:阅读与哲学思考
    abseil 的 cmake 方式编译
    [javascript] ie下audio不支持一些媒体类型
  • 原文地址:https://www.cnblogs.com/Yinku/p/11330360.html
Copyright © 2020-2023  润新知