• 真·LCT模板


    在看了大佬的博客后写下(抄下)的……

    大佬的博客:https://www.cnblogs.com/olinr/p/9455411.html

    #include<bits/stdc++.h>
    #define maxx 400005
    using namespace std;
    int c[maxx][2],val[maxx],fa[maxx],dis[maxx];
    bool rev[maxx];
    struct node{
    	int st[maxx];
    	int tp;
    	bool empty(){return tp==0;}
    	void pop(){st[tp--]=0;}
    	void push(int x){st[++tp]=x;}
    	int top(){return st[tp];}
    }s;
    bool pdroot(int x){return (c[fa[x]][0]==x||c[fa[x]][1]==x);}
    void put_up(int x){dis[x]=dis[c[x][0]]^dis[c[x][1]]^val[x];}
    void zychange(int x)
    {
    	rev[x]^=1;
    	swap(c[x][0],c[x][1]);
    }
    void put_down(int x)
    {
    	if(rev[x])
    	{
    		if(c[x][0])zychange(c[x][0]);
    		if(c[x][1])zychange(c[x][1]);
    		rev[x]=0;
    	}
    }
    void rot(int x)
    {
    	int y=fa[x],z=fa[y];
    	int l=(c[y][1]==x),r=l^1;
    	if(pdroot(y))c[z][c[z][1]==y]=x;
    	fa[x]=z;
    	fa[y]=x;
    	fa[c[x][r]]=y;
    	c[y][l]=c[x][r];c[x][r]=y;//注意 这个语句顺序不能换!!(卡了我半小时……) 
    	put_up(y);
        put_up(x);
    }
    void splay(int x)
    {	
    	int y=x;
    	s.push(y);
    	while(pdroot(y))
    	{
    		y=fa[y];
    		s.push(y);
    	}
    	while(!s.empty())
    	{
    		put_down(s.top());
    		s.pop();
    	}
    	while(pdroot(x))
    	{
    		int y=fa[x],z=fa[y];
    		int l=(c[y][1]==x),l1=(c[z][1]==y);
    		if(pdroot(y))
    		{
    			if(l^l1)rot(x);
    			else rot(y);
    		}
    		rot(x);
    	}
    	put_up(x);
    }
    void access(int x)
    {
    	for(int y=0;x;x=fa[y=x])
    	{
    		splay(x);
    		c[x][1]=y;
    		put_up(x);
    	}
    }
    void makeroot(int x)
    {
    	access(x);
    	splay(x);
    	zychange(x);
    }
    void split(int x,int y)
    {	
    	makeroot(x);
    	access(y);
    	splay(y);
    }
    int findroot(int x)
    {
    	access(x);
    	splay(x);
    	while(c[x][0])
    	{
    		put_down(x);
    		x=c[x][0];
    	}
    	return x;
    }
    void link(int x,int y)
    {
    	makeroot(x);
    	if(findroot(y)!=x)fa[x]=y;
    }
    void cut(int x,int y)
    {
    	makeroot(x);
    	if(findroot(y)==x&&fa[x]==y&&!c[x][1])
    	{
    		fa[x]=c[y][0]=0;
    		put_up(y);
    	}
    }
    int main()
    {
    	int n,m;
    	int x,y,ch;
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++)scanf("%d",&val[i]);
    	while(m--)
    	{
    		scanf("%d%d%d",&ch,&x,&y);
    		if(ch==0)
            {
                split(y,x);
                printf("%d",dis[x]);
                putchar('
    ');
                continue;
            }
            if(ch==1)
            {
                link(x,y);
                continue;
            }
            if(ch==2)
            {
                cut(y,x);
                continue;
            }
            if(ch==3)
            {
                splay(x);
                val[x]=y;
                continue;
            }
    	}
    }
    

      

    一蓑烟雨任平生
  • 相关阅读:
    商城----项目宏观(1)
    Java动态代理-JDK自带实现
    git提交出现remote rejected master -> XX changes closed
    openstack认证实践
    转一篇Git代码回滚技巧
    线段树解决leetcode307. Range Sum Query
    The Skyline Problem leetcode 详解
    编程容易犯错的几个地方
    c++中小项堆声明和使用
    C++中int转为char 以及int 转为string和string 转int和字符串的split
  • 原文地址:https://www.cnblogs.com/wyb-----520/p/10123776.html
Copyright © 2020-2023  润新知