LCT标准模板---P3690

//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f7f7f7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define PiL pair <ll , ll>
#define PLL pair <ll , ll>
#define Pii pair <int , int>
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}

using namespace std;
const int N = 300005;
int n , m , val[N];
struct Link_Cut_Tree
{
    int top, ch[N][2], fa[N], ans[N], lazy[N], stk[N];
    inline void push_up(int x) { ans[x] = (ans[ch[x][0]] ^ ans[ch[x][1]] ^ val[x]); }
    inline void push_down(int x) {
        int l = ch[x][0], r = ch[x][1];
        if (lazy[x]) {
            lazy[l] ^= 1, lazy[r] ^= 1, lazy[x] ^= 1;
            swap(ch[x][0], ch[x][1]);
        }
    }
    //true代表是当前splay的根
    inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
    //splay单旋 (画一画旋转图就知道怎么写了)
    void rotate(int x) {
        int y = fa[x], z = fa[y], l, r;
        if (ch[y][0] == x) l = 0 ; else l = 1;
        r = (l ^ 1);
        //普通的Splay是不用的
        if (!isroot(y)) {
            if (ch[z][0] == y) ch[z][0] = x;
            else ch[z][1] = x;
        }
        fa[x] = z, fa[y] = x, fa[ch[x][r]] = y;
        ch[y][l] = ch[x][r], ch[x][r] = y;
        push_up(y), push_up(x);
    }
    //splay双旋
    void splay(int x)
    {
        top = 1 , stk[top] = x;
        for (int i = x ; !isroot(i) ; i = fa[i]) stk[++top] = fa[i];
        for (int i = top ; i ; i --) push_down(stk[i]);
        //三种旋转方式
        while (!isroot(x))
        {
            int y = fa[x] , z = fa[y];
            if (!isroot(y))
            {
                if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x); // 1 三点不在一条线上
                else rotate(y); // 2 三点在一条线上
            }
            rotate(x); // 3 如果x的父亲是根直接旋上去
        }
    }
    //在辅助树中把x节点和根节点拉成一条实链,并且x深度为最大
    void access(int x){for(int t=0;x;t=x,x=fa[x])splay(x),ch[x][1]=t,push_up(x);}
    //原树中,将x节点作为原树的根节点,所以需要改变这条实链,其他节点不用管,因为大小相对不变
    void makeroot(int x){access(x);splay(x);lazy[x]^=1;}
    //返回辅助树中x所在平衡树根节点
    int find(int x){access(x);splay(x);while(ch[x][0])x=ch[x][0];return x;}
    //将x设置为根节点,然后和y拉在一条实链上,然后splay令y为当前平衡树根节点
    void split(int x,int y){makeroot(x);access(y);splay(y);}
    //需要满足三个条件:  1.x与y之间没有其他边  2.x没有右儿子  3.x与y联通
    void cut(int x,int y){split(x,y);if(ch[y][0]==x && ch[x][1] == 0)ch[y][0]=0,fa[x]=0;}
    //首先将x作为原树根节点,然后令y为x的父亲就链接好了
    void link(int x,int y){makeroot(x);fa[x]=y;}
}T;
int main()
{
    read (n , m);
    for (int i = 1 ; i <= n ; i ++)
        read (val[i]), T.ans[i] = val[i];
    while (m --)
    {
        int op , x , y;
        read (op ,x , y);
        if (op == 0)
        {
            T.split(x, y);
            write (T.ans[y]), LF;
        }
        else if(op == 1)
        {
            int fx = T.find(x) , fy = T.find(y);
            if(fx != fy)
                T.link(x, y);
        }
        else if(op == 2)
        {
            int fx = T.find(x) , fy = T.find(y);
            if(fx == fy)
                T.cut(x, y);
        }
        else
        {
            T.access(x) , T.splay(x) , val[x] = y, T.push_up(x);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值