zoukankan      html  css  js  c++  java
  • bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282

    题面:

    3282: Tree

    Time Limit: 30 Sec  Memory Limit: 512 MB
    Submit: 2845  Solved: 1424
    [Submit][Status][Discuss]

    Description

    给定N个点以及每个点的权值,要你处理接下来的M个操作。
    操作有4种。操作从0到3编号。点从1到N编号。
    0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。
    保证x到y是联通的。
    1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。
    2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。
    3:后接两个整数(x,y),代表将点X上的权值变成Y。

    Input

    第1行两个整数,分别为N和M,代表点数和操作数。
    第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。
    第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。
    1<=N,M<=300000

     

     

     

    Output

    对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

    Sample Input

    3 3
    1
    2
    3
    1 1 2
    0 1 2
    0 1 1

    Sample Output

    3
    1
     
     
    思路:
    模板题,练下手
     
    实现代码:
    #include<bits/stdc++.h>
    using namespace std;
    const int M = 3e5+10;
    const int inf = 0x3f3f3f3f;
    int n,m,sz,rt,c[M][2],fa[M],v[M],sum[M],st[M],top;
    bool rev[M];
    
    inline void up(int x){
        int l = c[x][0],r = c[x][1];
        sum[x] = sum[l] ^ sum[r] ^ v[x];
    }
    
    inline void pushrev(int x){
        int t = c[x][0];
        c[x][0] = c[x][1]; c[x][1] = t;
        rev[x] ^= 1;
    }
    
    inline void pushdown(int x){
        if(rev[x]){
            int l = c[x][0],r = c[x][1];
            if(l) pushrev(l);
            if(r) pushrev(r);
            rev[x] = 0;
        }
    } 
    
    inline bool nroot(int x){  //判断一个点是否为一个splay的根
        return c[fa[x]][0]==x||c[fa[x]][1] == x;
    }
    
    inline void rotate(int x){
        int y = fa[x],z = fa[y],k = c[y][1] == x;
        int w = c[x][!k];
        if(nroot(y)) c[z][c[z][1]==y]=x;
        c[x][!k] = y; c[y][k] = w;
        if(w) fa[w] = y; fa[y] = x; fa[x] = z;
        up(y);
    }
    
    inline void splay(int x){
        int y = x,z = 0;
        st[++z] = y;
        while(nroot(y)) st[++z] = y = fa[y];
        while(z) pushdown(st[z--]);
        while(nroot(x)){
            y = fa[x];z = fa[y];
            if(nroot(y))
                rotate((c[y][0]==x)^(c[z][0]==y)?x:y);
            rotate(x);
        }
        up(x);
    }
    
    //打通根节点到指定节点的实链,使得一条中序遍历从根开始以指定点结束的splay出现
    inline void access(int x){
        for(int y = 0;x;y = x,x = fa[x])
            splay(x),c[x][1]=y,up(x);
    }
    
    inline void makeroot(int x){  //换根,让指定点成为原树的根
        access(x); splay(x); pushrev(x);
    }
    
    inline int findroot(int x){  //寻找x所在原树的树根
        access(x); splay(x);
        while(c[x][0]) pushdown(x),x = c[x][0];
        splay(x);
        return x;
    }
    
    inline void split(int x,int y){  //拉出x-y的路径成为一个splay
        makeroot(x); access(y); splay(y);
    }
    
    inline void cut(int x,int y){   //断开边
        makeroot(x);
        if(findroot(y) == x&&fa[y] == x&&!c[y][0]){
            fa[y] = c[x][1] = 0;
            up(x);
        }
    }
    
    inline void link(int x,int y){   //连接边
        makeroot(x);
        if(findroot(y)!=x) fa[x] = y;
    }
    
    int main()
    {
        int n,m,x,y,op;
       scanf("%d%d",&n,&m);
       for(int i = 1;i <= n;i ++) scanf("%d",&v[i]);
       while(m--){
            scanf("%d%d%d",&op,&x,&y);
            if(op==0) split(x,y),printf("%d
    ",sum[y]);
            else if(op == 1) link(x,y);
            else if(op == 2) cut(x,y);
            else if(op == 3) splay(x),v[x] = y;
       }
       return 0;
    }
  • 相关阅读:
    flask路由(2)
    flask(1)
    了解websocket协议
    理解python的queue&deque
    oracle sqlplus中使用spool生成美化的html网页
    Oracle自动生成html格式awr的报告
    oracle data guard备库备份恢复
    【翻译自mos文章】DBA_JOBS 和 DBA_JOBS_RUNNING 不同的结果的解释
    ORA-23421: job number 225 is not a job in the job queue
    ORA-23421: job number 225 is not a job in the job queue
  • 原文地址:https://www.cnblogs.com/kls123/p/10785829.html
Copyright © 2011-2022 走看看