zoukankan      html  css  js  c++  java
  • 真·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;
            }
    	}
    }
    

      

    一蓑烟雨任平生
  • 相关阅读:
    升级windows 11小工具
    windows 10更新升级方法
    您需要了解的有关 Oracle 数据库修补的所有信息
    Step by Step Apply Rolling PSU Patch In Oracle Database 12c RAC Environment
    Upgrade Oracle Database Manually from 12.2.0.1 to 19c
    如何应用版本更新 12.2.0.1.210420(补丁 32507738 – 2021 年 4 月 RU)
    xtrabackup 安装、备份和恢复
    Centos_Lvm expand capacity without restarting CentOS
    Centos_Lvm_Create pv vg lv and mount
    通过全备+relaylog同步恢复被drop的库或表
  • 原文地址:https://www.cnblogs.com/wyb-----520/p/10123776.html
Copyright © 2011-2022 走看看