• 洛谷 P3391 【模板】文艺平衡树


    传送门

    AcWing 2437 Splay

    Version 1: fhq Treap

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const int maxn(1e5 + 10);
    int idx, root;
    
    struct node {
        int l, r;
        int key, val;
        int siz;
        bool rev;
    } 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 key)
    {
        ++idx;
        tree[idx].key = key;
        tree[idx].val = rand();
        tree[idx].siz = 1;
        return idx;
    }
    
    inline void push_up(int cur)
    {
        tree[cur].siz = tree[tree[cur].l].siz + tree[tree[cur].r].siz + 1;
    }
    
    inline void push_down(int cur)
    {
        if (tree[cur].rev) {
            swap(tree[cur].l, tree[cur].r);
            tree[tree[cur].l].rev ^= 1;
            tree[tree[cur].r].rev ^= 1;
            tree[cur].rev = false;
        }
    }
    
    inline void split(int cur, int siz, int& x, int& y)
    {
        if (not cur) {
            x = y = 0;
        } else {
            push_down(cur);
            if (siz > tree[tree[cur].l].siz) {
                x = cur;
                split(tree[cur].r, siz - tree[tree[cur].l].siz - 1, tree[cur].r, y);
            } else {
                y = cur;
                split(tree[cur].l, siz, x, tree[cur].l);
            }
            push_up(cur);
        }
    }
    
    inline int merge(int x, int y)
    {
        if (not x or not y) {
            return x + y;
        }
        if (tree[x].val < tree[y].val) {
            push_down(x);
            tree[x].r = merge(tree[x].r, y);
            push_up(x);
            return x;
        }
        push_down(y);
        tree[y].l = merge(x, tree[y].l);
        push_up(y);
        return y;
    }
    
    inline void reverse(int l, int r)
    {
        int x, y, z;
        split(root, l - 1, x, y);
        split(y, r - l + 1, y, z);
        tree[y].rev ^= 1;
        root = merge(merge(x, y), z);
    }
    
    void dfs(int cur)
    {
        if (not cur) return;
        push_down(cur);
        dfs(tree[cur].l);
        write(tree[cur].key, ' ');
        dfs(tree[cur].r);
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        int n = read(), m = read();
        for (int i = 1; i <= n; ++i) {
            root = merge(root, new_node(i));
        }
        while (m--) {
            int l = read(), r = read();
            reverse(l, r);
        }
        dfs(root);
        return 0;
    }
    

    Version 2: Splay

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const int maxn(1e5 + 10);
    int idx, root;
    
    struct node {
        int val, siz;
        int fa, ch[2];
        bool rev;
    } 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 void push_down(int cur)
    {
        if (tree[cur].rev) {
            swap(tree[cur].ch[0], tree[cur].ch[1]);
            tree[tree[cur].ch[0]].rev ^= 1;
            tree[tree[cur].ch[1]].rev ^= 1;
            tree[cur].rev = false;
        }
    }
    
    inline int get_rel(int cur, int fa)
    {
        return tree[fa].ch[1] == cur;
    }
    
    inline void connect(int cur, int fa, bool rel)
    {
        tree[fa].ch[rel] = cur;
        tree[cur].fa = fa;
    }
    
    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)
    {
        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) {
            root = cur;
        }
    }
    
    inline void insert(int val)
    {
        int cur = root, 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);
    }
    
    inline int get_id(int pos)
    {
        int cur = root;
        while (cur) {
            push_down(cur);
            if (tree[tree[cur].ch[0]].siz >= pos) {
                cur = tree[cur].ch[0];
            } else if (tree[tree[cur].ch[0]].siz + 1 == pos) {
                return cur;
            } else {
                pos -= tree[tree[cur].ch[0]].siz + 1;
                cur = tree[cur].ch[1];
            }
        }
        return cur;
    }
    
    inline void reverse(int l, int r)
    {
        int x = get_id(l - 1);
        int y = get_id(r + 1);
        splaying(x, 0);
        splaying(y, x);
        tree[tree[y].ch[0]].rev ^= 1;
    }
    
    void dfs(int cur, int n)
    {
        if (not cur) return;
        push_down(cur);
        dfs(tree[cur].ch[0], n);
        if (tree[cur].val >= 1 and tree[cur].val <= 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 = 0; i <= n + 1; ++i) {
            insert(i);
        }
        while (m--) {
            int l = read() + 1, r = read() + 1;
            reverse(l, r);
        }
        dfs(root, n);
        return 0;
    }
    
  • 相关阅读:
    摘:复习STL
    C#:在其它dll中调用此文件中的资源
    摘录:CSharp调用C动态链接库详解
    C/C++基本数据类型
    mysqlreport指南
    mysqlreport工具
    收藏一个简洁的PHP可逆加密函数
    解决MySQL查询数据不一致诟病
    Ubuntu下搭建C/C++开发环境
    mysql:主键和索引的区别
  • 原文地址:https://www.cnblogs.com/singularity2u/p/14012690.html
Copyright © 2020-2023  润新知