• 洛谷


    https://www.luogu.org/problem/P4567
    事实证明无旋Treap是不是不可能会比Splay快?

    #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 = 2400000 + 5;
    char val[MAXN];
    int ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;
    
    int cur;
    bool rev[MAXN];
    
    void Init() {
        root = 0, tot = 0;
    }
    
    inline void PushUp(int p) {
        siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
    }
    
    inline void PushDown(int p) {
        if(rev[p]) {
            swap(ls(p), rs(p));
            if(ls(p))
                rev[ls(p)] ^= 1;
            if(rs(p))
                rev[rs(p)] ^= 1;
            rev[p] = 0;
        }
    }
    
    void SplitRank(int p, int rk, int &x, int &y) {
        if(!p) {
            x = y = 0;
            return;
        }
        PushDown(p);
        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]) {
            PushDown(x);
            rs(x) = Merge(rs(x), y);
            PushUp(x);
            return x;
        } else {
            PushDown(y);
            ls(y) = Merge(x, ls(y));
            PushUp(y);
            return y;
        }
    }
    
    int NewNode(int v) {
        int p = ++tot;
        ch[p][0] = ch[p][1] = 0;
        val[p] = v, rnd[p] = rand();
        siz[p] = 1;
        rev[p]=false;
        return p;
    }
    
    
    //O(n)建树,返回新树的根
    int st[MAXN], stop;
    char buf[MAXN];
    inline int Build(int n) {
        stop = 0;
        for(int i = 0; i < n; ++i) {
            int tmp = NewNode(buf[i]), last = 0;
            while(stop && rnd[st[stop]] > rnd[tmp]) {
                last = st[stop];
                PushUp(last);
                st[stop--] = 0;
            }
            if(stop)
                rs(st[stop]) = tmp;
            ls(tmp) = last;
            st[++stop] = tmp;
        }
        while(stop)
            PushUp(st[stop--]);
        return st[1];
    }
    
    inline void Move() {
        scanf("%d", &cur);
    }
    
    inline void Insert(int &root) {
        int x = 0, y = 0, z = 0, n;
        SplitRank(root, cur, x, z);
        scanf("%d", &n);
        getchar();
        buf[n] = '';
        for(int i = 0; i < n; ++i){
            buf[i] = getchar();
        }
        y = Build(n);
        root = Merge(Merge(x, y), z);
    }
    
    inline void Delete(int &root) {
        int x = 0, y = 0, z = 0, n;
        SplitRank(root, cur, x, y);
        scanf("%d", &n);
        SplitRank(y, n, y, z);
        //会不会太慢了
        //UnBuild(y);
        //y=Merge(ls(y),rs(y));
        root = Merge(x, z);
    }
    
    void Show(int p) {
        if(!p)
            return;
        PushDown(p);
        Show(ls(p));
        if(val[p]!='
    ')
            putchar(val[p]);
        else
            printf("\n");
        Show(rs(p));
    }
    
    inline void Get(int &root) {
        int x = 0, y = 0, z = 0, n = 0;
        SplitRank(root, cur, x, y);
        SplitRank(y, 1, y, z);
        putchar(val[y]);
        if(val[y]!='
    ')
            putchar('
    ');
        root = Merge(Merge(x, y), z);
    }
    
    inline void Prev() {
        --cur;
    }
    
    inline void Next() {
        ++cur;
    }
    
    void Reverse(int &root) {
        int l = cur+1, r;
        scanf("%d", &r);
        r = l + r - 1;
        //cout<<"l="<<l<<" r="<<r<<endl;
        int x = 0, y = 0, z = 0;
        SplitRank(root, l - 1, x, y);
        SplitRank(y, r + 1 - l, y, z);
        rev[y] ^= 1;
        root = Merge(Merge(x, y), z);
    }
    
    char op[20];
    int main() {
    #ifdef Yinku
        freopen("Yinku.in", "r", stdin);
    #endif // Yinku
        int t;
        scanf("%d", &t);
        Init();
        while(t--) {
            scanf("%s", op);
            switch(op[0]) {
                case 'M':
                    Move();
                    break;
                case 'I':
                    Insert(root);
                    break;
                case 'D':
                    Delete(root);
                    break;
                case 'R':
                    Reverse(root);
                    break;
                case 'G':
                    Get(root);
                    break;
                case 'P':
                    Prev();
                    break;
                case 'N':
                    Next();
                    break;
            }
            /*printf("op=%s
    ",op);
            int x=0,y=0;
            SplitRank(root,cur,x,y);
            Show(x);
            putchar('|');
            Show(y);
            putchar('
    ');
            root=Merge(x,y);*/
        }
        return 0;
    }
    
  • 相关阅读:
    【IT笔试面试题整理】把n个骰子扔在地上,所有骰子朝上一面的点数之和为S概率转
    面试题位操作
    微软面试题 寻找数组中出现的唯一重复的一个数
    【IT笔试面试题整理】给定二叉树先序中序,建立二叉树的递归算法
    【IT笔试面试题整理】 二叉树任意两个节点间最大距离
    面试题堆栈和队列
    LRU cache实现 Java 转
    【IT笔试面试题整理】有序数组生成最小高度二叉树
    Unity3d Asset Store下载的资源在哪?
    Xcode 6 如何将 模拟器(simulator) for iphone/ipad 转变成 simulator for iphone
  • 原文地址:https://www.cnblogs.com/Yinku/p/11330344.html
Copyright © 2020-2023  润新知