• 洛谷 P3224 [HNOI2012]永无乡


    传送门

    AcWing 1063 永无乡

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const int maxn(2e6 + 10);
    int idx, root[maxn], pre[maxn];
    
    struct node {
        int id;
        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, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    inline int find(int cur)
    {
        return pre[cur] == cur ? cur : pre[cur] = find(pre[cur]);
    }
    
    inline int new_node(int val, int id)
    {
        ++idx;
        tree[idx].val = val;
        tree[idx].id = id;
        tree[idx].siz = 1;
        return idx;
    }
    
    inline int get_rel(int cur, int fa)
    {
        return tree[fa].ch[1] == cur;
    }
    
    inline void push_up(int cur)
    {
        tree[cur].siz = tree[tree[cur].ch[0]].siz + tree[tree[cur].ch[1]].siz + 1;
    }
    
    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 id, int& rt)
    {
        int cur = rt, fa = 0;
        while (cur) {
            fa = cur;
            cur = tree[cur].ch[val > tree[cur].val];
        }
        cur = new_node(val, id);
        connect(cur, fa, val > tree[fa].val);
        splaying(cur, 0, rt);
    }
    
    inline int get_id(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 tree[cur].id;
    }
    
    inline void dfs(int u, int& v)
    {
        if (not u) return;
        dfs(tree[u].ch[0], v);
        dfs(tree[u].ch[1], v);
        insert(tree[u].val, tree[u].id, v);
    }
    
    inline void merge(int u, int v)
    {
        u = find(u);
        v = find(v);
        if (u not_eq v) {
            if (tree[root[u]].siz > tree[root[v]].siz) {
                swap(u, v);
            }
            dfs(root[u], root[v]);
            pre[u] = v;
        }
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        int n = read(), m = read();
        for (int i = 1; i <= n; ++i) {
            pre[i] = root[i] = new_node(read(), i);
        }
        while (m--) {
            int u = read(), v = read();
            merge(u, v);
        }
        int q = read();
        while (q--) {
            char op[2];
            scanf("%s", op);
            int x = read(), k = read();
            if (*op == 'Q') {
                int rt = root[find(x)];
                if (tree[rt].siz < k) {
                    write(-1, true);
                } else {
                    write(get_id(k, rt), true);
                }
            } else {
                merge(x, k);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    上下,流动
    面对离去了的亲人,
    计算 star 之间 距离,
    咀嚼,
    python中的内嵌函数
    python中全局变量和局部变量
    python中函数的闭包
    python中函数的收集参数
    python中如何将局部变量扩展为全局变量(global关键字)
    python中的内嵌函数
  • 原文地址:https://www.cnblogs.com/singularity2u/p/14030628.html
Copyright © 2020-2023  润新知