• 牛客 2020 牛客国庆集训派对 day8 G. Shuffle Cards


    传送门

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const int maxn(1e5 + 10);
    int idx, root, root2;
    
    struct node {
        int val, siz;
        int fa, ch[2];
    } tree[maxn];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, char c)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (c) putchar(c);
    }
    
    inline int new_node(int val)
    {
        ++idx;
        tree[idx].val = val;
        tree[idx].siz = 1;
        return idx;
    }
    
    inline void push_up(int cur)
    {
        tree[cur].siz = tree[tree[cur].ch[0]].siz + tree[tree[cur].ch[1]].siz + 1;
    }
    
    inline bool get_rel(int cur, int fa)
    {
        return tree[fa].ch[1] == cur;
    }
    
    inline void connect(int cur, int fa, bool rel)
    {
        tree[cur].fa = fa;
        tree[fa].ch[rel] = cur;
    }
    
    inline void rotate(int cur)
    {
        int fa = tree[cur].fa;
        int gf = tree[fa].fa;
        bool rel = get_rel(cur, fa);
        connect(tree[cur].ch[rel ^ 1], fa, rel);
        connect(cur, gf, get_rel(fa, gf));
        connect(fa, cur, rel ^ 1);
        push_up(fa);
        push_up(cur);
    }
    
    inline void splaying(int cur, int top, int& rt)
    {
        while (tree[cur].fa not_eq top) {
            int fa = tree[cur].fa;
            int gf = tree[fa].fa;
            if (gf not_eq top) {
                get_rel(cur, fa) ^ get_rel(fa, gf) ? rotate(cur) : rotate(fa);
            }
            rotate(cur);
        }
        if (not top) {
            rt = cur;
        }
    }
    
    inline void insert(int val, int& rt)
    {
        int cur = rt, fa = 0;
        while (cur) {
            fa = cur;
            cur = tree[cur].ch[val > tree[cur].val];
        }
        cur = new_node(val);
        connect(cur, fa, val > tree[fa].val);
        splaying(cur, 0, rt);
    }
    
    inline int get_pos(int rank, int rt)
    {
        int cur = rt;
        while (cur) {
            if (tree[tree[cur].ch[0]].siz >= rank) {
                cur = tree[cur].ch[0];
            } else if (tree[tree[cur].ch[0]].siz + 1 == rank) {
                break;
            } else {
                rank -= tree[tree[cur].ch[0]].siz + 1;
                cur = tree[cur].ch[1];
            }
        }
        return cur;
    }
    
    inline void move(int l, int r, int n)
    {
        if (l == 1) return;
        if (r < n) {
            int x = get_pos(l - 1, root);
            int y = get_pos(r + 1, root);
            splaying(x, 0, root);
            splaying(y, x, root);
            root2 = tree[y].ch[0];
            tree[root2].fa = tree[y].ch[0] = 0;
            splaying(get_pos(r - l + 1, root2), 0, root2);
            splaying(get_pos(1, root), 0, root);
        } else {
            int x = get_pos(l - 1, root);
            splaying(x, 0, root);
            root2 = tree[root].ch[1];
            tree[root2].fa = tree[x].ch[1] = 0;
            splaying(get_pos(r - l + 1, root2), 0, root2);
            splaying(get_pos(l - 1, root), 0, root);
        }
        connect(root, root2, true);
        root = root2;
        push_up(root);
    }
    
    void dfs(int cur, int n)
    {
        if (not cur) return;
        dfs(tree[cur].ch[0], n);
        write(tree[cur].val, ' ');
        dfs(tree[cur].ch[1], n);
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        int n = read(), m = read();
        for (int i = 1; i <= n; ++i) {
            insert(i, root);
        }
        while (m--) {
            int l = read();
            int r = l + read() - 1;
            move(l, r, n);
        }
        dfs(root, n);
        return 0;
    }
    
  • 相关阅读:
    2018.10.05模拟总结
    CentOS 7 分区方案
    Linux讲解之定时任务
    TrueCrypt与CryptSetup双系统全盘加密(图文)
    使用LUKS加密你的磁盘
    使用LUKS加密你的磁盘
    本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法。
    Xrdp
    CentOS7开放端口以及常用的使用命令记录整理
    【Python成长之路】装逼的一行代码:快速共享文件
  • 原文地址:https://www.cnblogs.com/singularity2u/p/14031340.html
Copyright © 2020-2023  润新知