zoukankan      html  css  js  c++  java
  • [CROATIAN2009] OTOCI

    [题目链接]

             https://www.lydsy.com/JudgeOnline/problem.php?id=1180

    [算法]

             动态树维护森林连通性

             时间复杂度 : O(NlogN ^ 2)

    [代码]

             

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 2e5 + 10;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    
    int n;
    int val[MAXN];
    
    struct Link_Cut_Tree
    {
            struct Node
            {
                    int father , son[2] , value , cnt;
                    bool tag;
            }    a[MAXN];
            inline void init()
            {
                    for (int i = 1; i <= n; i++)
                    {
                            a[i].value = a[i].cnt = val[i];
                            a[i].tag = false;
                            a[i].father = a[i].son[0] = a[i].son[1] = 0;
                    }
            }
            inline void update(int x)
            {
                    a[x].cnt = a[a[x].son[0]].cnt + a[a[x].son[1]].cnt + a[x].value;
            }
            inline void pushdown(int x)
            {
                    if (a[x].tag)
                    {
                            swap(a[x].son[0] , a[x].son[1]);
                            a[a[x].son[0]].tag ^= 1;
                            a[a[x].son[1]].tag ^= 1;
                            a[x].tag = false;
                    }
            }
            inline bool get(int x)
            {
                    pushdown(a[x].father);
                    return a[a[x].father].son[1] == x;
            }
            inline bool nroot(int x)
            {
                    return a[a[x].father].son[0] == x | a[a[x].father].son[1] == x;
            }
            inline void rotate(int x)
            {
                    int f = a[x].father , g = a[f].father;
                    int tmpx = get(x) , tmpf = get(f);
                    int w = a[x].son[tmpx ^ 1];
                    if (nroot(f)) a[g].son[tmpf] = x;
                    a[x].son[tmpx ^ 1] = f;
                    a[f].son[tmpx] = w;
                    if (w) a[w].father = f;
                    a[f].father = x;
                    a[x].father = g;
                    update(f);    
            }
            inline void splay(int x)
            {
                    int y = x , z = 0;
                    static int st[MAXN];
                    st[++z] = y;
                    while (nroot(y)) y = st[++z] = a[y].father;
                    while (z) pushdown(st[z--]);
                    while (nroot(x))
                    {
                            int y = a[x].father , z = a[y].father;
                            if (nroot(y))
                                    rotate((a[z].son[0] == y) ^ (a[y].son[0] == x) ? x : y);
                            rotate(x);
                    }
                    update(x);
            }
            inline void access(int x)
            {
                    for (int y = 0; x; x = a[y = x].father)
                    {
                            splay(x);
                            a[x].son[1] = y;
                            update(x);
                    }
            }
            inline void make_root(int x)
            {
                    access(x);            
                    splay(x);
                    a[x].tag ^= 1;
                    pushdown(x);
            }
            inline int find_root(int x)
            {
                    access(x);
                    splay(x);
                    while (a[x].son[0])
                    {
                            pushdown(x);
                            x = a[x].son[0];
                    }
                    return x;
            }
            inline void link(int x , int y)
            {
                    make_root(x);
                    if (find_root(y) != x) a[x].father = y;
            }
            inline void split(int x , int y)
            {
                    make_root(x);
                    access(y);
                    splay(y);
            }
            inline int query(int x , int y)
            {
                    split(x , y);
                    return a[y].cnt;
            }
            inline bool connected(int x , int y)
            {    
                    return (find_root(x) == find_root(y));
            }
            inline void modify(int u , int x)
            {
                    splay(u);
                    a[u].value = x;
                    update(u);
            }
    } LCT;
    
    template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
    template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
    template <typename T> inline void read(T &x)
    {
        T f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
    }
    int main()
    {
            
            scanf("%d" , &n);
            for (int i = 1; i <= n; i++) scanf("%d" , &val[i]);
            LCT.init();
            int q;
            scanf("%d" , &q);
            while (q--)
            {
                    char type[10];
                    scanf("%s" , type);
                    if (type[0] == 'b')
                    {
                            int u , v;
                            scanf("%d%d" , &u , &v);
                            if (LCT.connected(u , v)) 
                            {
                                    printf("no
    ");
                            } else 
                            {
                                    printf("yes
    ");
                                    LCT.link(u , v);
                            }
                    } else if (type[0] == 'p')
                    {
                            int u , x;
                            scanf("%d%d" , &u , &x);
                            LCT.modify(u , x);
                    } else
                    {
                            int u , v;
                            scanf("%d%d" , &u , &v);
                            if (!LCT.connected(u , v)) printf("impossible
    ");
                            else printf("%d
    " , LCT.query(u , v));
                    }
            }
            
            return 0;
        
    }
  • 相关阅读:
    python版本切换及添加路径
    python下载及安装步骤
    day01计算机基础-python解释器-pycharm介绍
    查看pycharm有效期
    对于Makefile的基本使用
    Shell命令整理
    一起来免费听无损高品质音乐吧!
    记录 解决ubuntu16.04 ‘E: 无法获得锁 /var/lib/dpkg/lock-frontend
    Ubuntu16手动安装OpenStack——keystone篇
    python3中的RE(正则表达式)
  • 原文地址:https://www.cnblogs.com/evenbao/p/10152813.html
Copyright © 2011-2022 走看看