跳到正文

个人记录

数据结构:LCT

Terdy约 59 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 59 分钟

版本

  1. v1当前

LCT 学习笔记

  • 此笔记主要是一个 LCT 板子的存在,没有什么具体的讲解。

一. 前情提要

学习 LCT 的前提是学习 Splay,之后我 Splay 的旋转操作一直背不下来,就摆烂了。之后学到了一个 ch[x][0/1] 存储的旋转板子,这个好背啊,哦之后就背下来了。发现 LCT 就是函数多了一些,理解之后就好记忆了。

二. 大概理解

之前学习 LCT 一直不会的很大原因是没有弄明白 LCT 存的都是些什么,也就辅助树是什么。

其实我们需要知道的除了实边和虚边的剖分后,需要明白一个 Splay 维护的是什么。一个 Splay 维护的是一条链,这条链是从上到下的一条链,Splay 的中序遍历就是从上到下依次遍历这条链上的每个点。那么一个个 Splay 就维护了一条条链,把这些 Splay 拼在一起就成了一个树!

那么这些 Splay 之间是怎么连接的呢?每个 Splay 的根向上连接到这条链在原树中的父亲节点。我们发现每个点有一个父亲,但是可能会有很多的儿子,我们只需要记录 Splay 中的儿子就可以了。

函数中最重要的就是 Access(x) 操作了,需要理解透彻。(我自己是把例子手玩了一遍理解了)

三. 主要函数

1. Wrc(x)

返回是否是右儿子,比较方便后面的代码。

C++
bool Wrc(int x)
{
    return rc[fa[x]] == x;
}

2. IsRoot(x)

判断 xx 是否是其所在 Splay 的根。

C++
bool IsRoot(int x)
{
    if(!fa[x]) return 1;
    return lc[fa[x]] != x && rc[fa[x]] != x;
//若fa=0则x是原树中的根
//若x不是其父亲Splay中的儿子,则它是其所属路径Splay的根
}

3. Rotate(x)

一次旋转操作,看题意是否有 PushUp(x) 操作。

C++
void Rotate(int x)
{
	int y = fa[x] , z = fa[y] , k = Wrc(x);
	if(!IsRoot(y)) ch[z][ch[z][1] == y] = x;//这一行一定要写在前面,这是与 Splay 略有不同的地方。
	ch[y][k] = ch[x][!k] , fa[ch[x][!k]] = y;
	ch[x][!k] = y , fa[y] = x , fa[x] = z;
	PushUp(y) , PushUp(x);
}

4. Splay(x)

将 xx 旋转到其所在子树的根,看题意是否有 PushUp(x) 操作。

C++
void Splay(int x)
{
  	PushDownAll(x);
	while(!IsRoot(x))
	{
		if(!IsRoot(fa[x])) Wrc(x) == Wrc(fa[x]) ? Rotate(fa[x]) : Rotate(x);
		Rotate(x);
	}
	PushUp(x);
}

5. PushUp(x)

看题意是否有这个操作,以及操作的具体内容是什么。

C++
void PushUp(int x)
{
	siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x] + 1;
}

6. PushDown(x)

我们大概率会有翻转子树(就是文艺平衡树)的操作,所以需要下放标记。

C++
void PushDown(int x)
{
    if(rev[x])
    {
        swap(ch[x][0] , ch[x][1]);
        if(ch[x][0]) rev[ch[x][0]] ^= 1;
        if(ch[x][1]) rev[ch[x][1]] ^= 1;
        rev[x] = 0;
    }
}

7. PushDownAll(x)

我们在 Access(x) 操作前需要把 xx 所在 Splay 的 xx 上方的标记全部下放,实现有很多种方式,这里使用了递归。

C++
void PushDownAll(int x)
{
	if(!IsRoot(x)) PushDownAll(fa[x]);
	PushDown(x);
}

8. Access(x)

将 xx 到其所在 Splay 的根的路径“打通”,也就是将这条路径上的虚边全部变成实边,其中一些数组的加减看具体题。

C++
void Access(int x)
{
    for(int y = 0; x; y = x , x = fa[x])
    {
        Splay(x);
        cnt[x] += siz[ch[x][1]];
        rc[x] = y;
        cnt[x] -= siz[ch[x][1]];
        PushUp(x);
    }
}

9. FindRoot(x)

寻找 xx 所在 Splay 的根。

C++
int FindRoot(int x)
{
    Access(x);
    Splay(x);
    while(PushDown(x) , ch[x][0]) x = ch[x][0];
    Splay(x);
    return x;
}

10. MakeRoot(x)

将 xx 变成其所在 Splay 的根。

C++
void MakeRoot(int x)
{
    Access(x);
    Splay(x);
    rev[x] ^= 1;
}

11. Split(x,y)

拎出 x,yx,y 之间的路径,也就是一棵 Splay。

C++
void Split(int x , int y)
{
	MakeRoot(x);
	Access(y);
	Splay(y);
}

12. Link(x,y)

将 x,yx,y 之间连边,数组加减看具体题。

C++
void Link(int x , int y)
{
	Split(x , y);
    fa[x] = y;
    cnt[y] += siz[x];
    PushUp(y);
}

13. Cut(x,y)

将 x,yx,y 之间的边断掉,数组加减看具体题。

C++
void Cut(int x , int y)
{
    Split(x , y);
    ch[y][0] = fa[x] = 0;
}

四. 例题

PS:有些题做的时间比较奇怪,所以会呈现出代码中的一些函数和板子不一样,不过差别不大,板子可以确保正确。

PSS:没有题的具体做法,只是说明了其中 LCT 的作用。

一. 真正维护了树状物的连接。

我们使用 LCT 的目的是维护一棵树的动态连边与断边,很直观的作用。

1.P2147

直接使用 LCT 维护就好了。

代码
C++
#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 5;
int fa[N] , lc[N] , rc[N] , rev[N] , qr , que[N];

bool Wrc(int x)
{
    return rc[fa[x]] == x;
}

bool IsRoot(int x)
{
    if(!fa[x]) return 1;
    return lc[fa[x]] != x && rc[fa[x]] != x;
//若fa=0则x是原树中的根
//若x不是其父亲Splay中的儿子,则它是其所属路径Splay的根
}

void DownDate(int x)//下方标记
{
    if(rev[x])
    {
        swap(lc[x] , rc[x]);
        if(lc[x]) rev[lc[x]] ^= 1;
        if(rc[x]) rev[rc[x]] ^= 1;
        rev[x] = 0;
    }
}

void Rotate(int x)
{
    int y = fa[x] , z = fa[y] , b = lc[y] == x ? rc[x] : lc[x];
    if(z && !IsRoot(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
    fa[x] = z , fa[y] = x , b ? fa[b] = y : 0;
    if(lc[y] == x) rc[x] = y , lc[y] = b;
    else lc[x] = y , rc[y] = b;
    return ;
}

void Splay(int x)
{
    que[qr = 0] = x;
    for(int y = x; !IsRoot(y); y = fa[y]) que[++qr] = fa[y];
    for(int i = qr; i >= 0; i--) DownDate(que[i]);//及时下放标记
//由于打了标记,因此根到当前需要Splay的结点的路径上的标记需要全部下放
    while(!IsRoot(x))
    {
        if(!IsRoot(fa[x]))
        {
            if(Wrc(x) == Wrc(fa[x])) Rotate(fa[x]);
            else Rotate(x);
        }
        Rotate(x);
    }
}

void Access(int x)
{
    for(int y = 0; x; y = x , x = fa[x])
    {
        Splay(x);
        rc[x] = y;
//        if(y) fa[y] = x;
    }
}

int FindRoot(int x)
{
    Access(x);
    Splay(x);
    while(DownDate(x) , lc[x]) x = lc[x];
    Splay(x);
    return x;
}

void MakeRoot(int x)
{
    Access(x);
    Splay(x);
    rev[x] ^= 1;
}

void Link(int x , int y)
{
    MakeRoot(x);
    fa[x] = y;
}

void Cut(int x , int y)
{
    MakeRoot(x);
    Access(y);
    Splay(y);
    lc[y] = 0 , fa[x] = 0;
}

int main()
{
    int n , m;
    scanf("%d%d" , &n , &m);
    for(int i = 1; i <= m; i++)
    {
        char a[10];
        int x , y;
        scanf("%s%d%d" , a , &x , &y);
        if(a[0] == 'Q')
        {
            if(FindRoot(x) == FindRoot(y)) printf("Yes\n");
            else printf("No\n");
        }
        else if(a[0] == 'C')
        {
            Link(x , y);
        }
        else
        {
            Cut(x , y);
        }
    }
    return 0;
}
2. CF1109F

从 11 扫到 nmnm,发现每个位置的最远的没有环的位置是单调的,我们用 LCT 维护是否出现了环。

代码
C++
#include <bits/stdc++.h>

using namespace std;

# define ll long long

const int N = 1e3 + 5 , M = 2e5 + 5;
int n , m , a[N][N];
vector< int > e[M];
ll ans;
int dx[] = {1 , -1 , 0 , 0};
int dy[] = {0 , 0 , 1 , -1};

namespace LCT
{
	int fa[M] , lc[M] , rc[M] , rev[M] , tim[M] , mn[M];
	bool Wrc(int x) {return rc[fa[x]] == x;}
	bool IsRoot(int x) {return !fa[x] || lc[fa[x]] != x && rc[fa[x]] != x;}
	void PushDown(int x)//下方标记
	{
	    if(rev[x])
	    {
	        swap(lc[x] , rc[x]);
	        if(lc[x]) rev[lc[x]] ^= 1;
	        if(rc[x]) rev[rc[x]] ^= 1;
	        rev[x] = 0;
	    }
	}
	void PushDownAll(int x)
	{
		if(!IsRoot(x)) PushDownAll(fa[x]);
		PushDown(x);
	}
	void Rotate(int x)
	{
	    int y = fa[x] , z = fa[y] , b = lc[y] == x ? rc[x] : lc[x];
	    if(!IsRoot(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
	    fa[x] = z , fa[y] = x , b ? fa[b] = y : 0;
	    if(lc[y] == x) rc[x] = y , lc[y] = b;
	    else lc[x] = y , rc[y] = b;
	}
	void Splay(int x)
	{
		PushDownAll(x);
	    while(!IsRoot(x))
	    {
	        if(!IsRoot(fa[x]))
	        {
	            if(Wrc(x) == Wrc(fa[x])) Rotate(fa[x]);
	            else Rotate(x);
	        }
	        Rotate(x);
	    }
	}
	void Access(int x)
	{
	    for(int y = 0; x; y = x , x = fa[x])
	    {
	        Splay(x);
	        rc[x] = y;
	    }
	}
	void MakeRoot(int x)
	{
	    Access(x);
	    Splay(x);
	    rev[x] ^= 1;
	}
	void Split(int x , int y)
	{
		MakeRoot(x);
		Access(y);
		Splay(y);
	}
	void Link(int x , int y)
	{
		Split(x , y);
	    fa[x] = y;
	}
	void Cut(int x , int y)
	{
	    MakeRoot(x);
	    Access(y);
	    Splay(y);
	    lc[y] = 0 , fa[x] = 0;
	}
	int FindRoot(int x)
	{
	    Access(x);
	    Splay(x);
	    while(PushDown(x) , lc[x]) x = lc[x];
	    Splay(x);
	    return x;
	}
}

namespace SGT
{
	int mn[M<<2] , tag[M<<2] , sum[M<<2];
# define lc x << 1
# define rc x << 1 | 1
# define mid (l + r >> 1)
	void PushUp(int x)
	{
		if(mn[lc] < mn[rc]) mn[x] = mn[lc] , sum[x] = sum[lc];
		else if(mn[lc] > mn[rc]) mn[x] = mn[rc] , sum[x] = sum[rc];
		else mn[x] = mn[lc] , sum[x] = sum[lc] + sum[rc];
	}
	void PushDown(int x)
	{
		if(tag[x] != 0)
		{
			tag[lc] += tag[x] , mn[lc] += tag[x];
			tag[rc] += tag[x] , mn[rc] += tag[x];
			tag[x] = 0;
		}
	}
	void Build(int x , int l , int r)
	{
		if(l == r) return mn[x] = 0 , sum[x] = 1 , void();
		Build(lc , l , mid) , Build(rc , mid + 1 , r);
		PushUp(x);
	}
	void Upt(int x , int l , int r , int L , int R , int k)
	{
		// if(x == 1) cout << L << ' ' << R << ' ' << k << endl;
		if(L <= l && r <= R) return tag[x] += k , mn[x] += k , void();
		PushDown(x);
		if(mid >= L) Upt(lc , l , mid , L , R , k);
		if(mid < R) Upt(rc , mid + 1 , r , L , R , k);
		PushUp(x);
	}
	int Query(int x , int l , int r , int L , int R)
	{
		if(L <= l && r <= R) return mn[x] == 1 ? sum[x] : 0;
		PushDown(x);
		int res = 0;
		if(mid >= L) res += Query(lc , l , mid , L , R);
		if(mid < R) res += Query(rc , mid + 1 , r , L , R);
		return res;
	}
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> a[i][j];
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)
	{
		for(int k = 0; k < 4; k++)
		{
			int x = i + dx[k] , y = j + dy[k];
			if(x < 1 || x > n || y < 1 || y > m) continue ;
			e[a[i][j]].push_back(a[x][y]);
		}
	}
	SGT::Build(1 , 1 , n * m);
	for(int i = 1 , j = 0; i <= n * m; i++)
	{
		while(j <= n * m)
		{
			j++;
			if(j > n * m) break ;
			int flag = 0 , cnt = 0;
			for(int u : e[j])
			{
				if(i <= u && u < j)
				{
					if(LCT::FindRoot(j) == LCT::FindRoot(u)) {flag = 1; break ;}
					LCT::Link(u , j);
				}
			}
			for(int u : e[j])
			{
				if(i <= u && u < j)
				{
					if(LCT::FindRoot(j) == LCT::FindRoot(u)) LCT::Cut(u , j);
				}
			}
			if(flag) break ;
			for(int u : e[j])
			{
				if(i <= u && u < j)
				{
					LCT::Link(u , j);
					// cout << i << ' ' << j << ' ' << u << endl;
					cnt++;
				}
			}
			SGT::Upt(1 , 1 , n * m , j , j , j - i + 1);
			SGT::Upt(1 , 1 , n * m , j , n * m , -cnt);
		}
		j--;
		// cout << i << ' ' << j << endl;
		ans += SGT::Query(1 , 1 , n * m , i , j);
		// cout << SGT::Query(1 , 1 , n * m , i , j) << endl;
		for(int u : e[i])
		{
			if(i <= u && u <= j)
			{
				LCT::Cut(i , u);
				// cout << i << ' ' << u << endl;
				SGT::Upt(1 , 1 , n * m , u , n * m , 1);
			}
		}
		SGT::Upt(1 , 1 , n * m , i , j , -1);
	}
	cout << ans;
    return 0;
}
3. P4299

我们需要知道的一个性质是:将两棵树拼起来,新的树的重心一定在原来两棵树的重心所在的路径上。我们可以使用 LCT 来拼接树,之后提取出路径来操作。

代码
C++
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
int n , m , Xor;
int fa[N] , lc[N] , rc[N] , rev[N] , qr , que[N] , siz[N] , cnt[N];
int f[N];

int Find(int x) {return x == f[x] ? x : f[x] = Find(f[x]);}

bool Wrc(int x)
{
    return rc[fa[x]] == x;
}

bool IsRoot(int x)
{
    if(!fa[x]) return 1;
    return lc[fa[x]] != x && rc[fa[x]] != x;
//若fa=0则x是原树中的根
//若x不是其父亲Splay中的儿子,则它是其所属路径Splay的根
}

void PushUp(int x)
{
	siz[x] = siz[lc[x]] + siz[rc[x]] + cnt[x] + 1;
}

void PushDown(int x)//下方标记
{
    if(rev[x])
    {
        swap(lc[x] , rc[x]);
        if(lc[x]) rev[lc[x]] ^= 1;
        if(rc[x]) rev[rc[x]] ^= 1;
        rev[x] = 0;
    }
}

void PushDownAll(int x)
{
	if(!IsRoot(x)) PushDownAll(fa[x]);
	PushDown(x);
}

void Rotate(int x)
{
    int y = fa[x] , z = fa[y] , b = lc[y] == x ? rc[x] : lc[x];
    if(z && !IsRoot(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
    fa[x] = z , fa[y] = x , b ? fa[b] = y : 0;
    if(lc[y] == x) rc[x] = y , lc[y] = b;
    else lc[x] = y , rc[y] = b;
    PushUp(y) , PushUp(x);
    return ;
}

void Splay(int x)
{
	PushDownAll(x);//及时下放标记
//由于打了标记,因此根到当前需要Splay的结点的路径上的标记需要全部下放
    while(!IsRoot(x))
    {
        if(!IsRoot(fa[x]))
        {
            if(Wrc(x) == Wrc(fa[x])) Rotate(fa[x]);
            else Rotate(x);
        }
        Rotate(x);
    }
    PushUp(x);
}

void Access(int x)
{
    for(int y = 0; x; y = x , x = fa[x])
    {
        Splay(x);
        cnt[x] += siz[rc[x]];
        rc[x] = y;
        cnt[x] -= siz[rc[x]];
        PushUp(x);
    }
}

int FindRoot(int x)
{
    Access(x);
    Splay(x);
    while(PushDown(x) , lc[x]) x = lc[x];
    Splay(x);
    return x;
}

void MakeRoot(int x)
{
    Access(x);
    Splay(x);
    rev[x] ^= 1;
}

void Split(int x , int y)
{
	MakeRoot(x);
	Access(y);
	Splay(y);
}

void Link(int x , int y)
{
	Split(x , y);
    fa[x] = y;
    cnt[y] += siz[x];
    PushUp(y);
}

void Cut(int x , int y)
{
    MakeRoot(x);
    Access(y);
    Splay(y);
    lc[y] = 0 , fa[x] = 0;
}

int Upt(int x)
{
	int now = 1e9 , lsum = 0 , rsum = 0 , sum = siz[x] >> 1 , flag = siz[x] & 1;
//	cout << x << ' ' << siz[x] << endl;
	while(x)
	{
		PushDown(x);
		int sl = siz[lc[x]] + lsum , sr = siz[rc[x]] + rsum;
		if(sl <= sum && sr <= sum)
		{
			if(flag) {now = x; break ;}
			now = min(now , x);
		}
		if(sl < sr) lsum += siz[lc[x]] + cnt[x] + 1 , x = rc[x];
		else rsum += siz[rc[x]] + cnt[x] + 1 , x = lc[x];
	}
	Splay(now);
	return now;
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++) siz[i] = 1 , f[i] = i , Xor ^= i;
	while(m--)
	{
		char opt[4];
		int u , v;
		cin >> opt;
		if(opt[0] == 'A')
		{
			cin >> u >> v;
			Link(u , v);
			u = Find(u) , v = Find(v);
			Split(u , v);
			int now = Upt(v);
//			cout << u << ' ' << v << ' ' << now << endl;
			Xor ^= u ^ v ^ now;
			f[u] = f[v] = f[now] = now;
		}
		else if(opt[0] == 'Q')
		{
			cin >> u;
			cout << Find(u) << '\n';
		}
		else
		{
			cout << Xor << '\n';
		}
	}
    return 0;
}
4. P5383

用 LCT 维护一个图的生成树森林。

代码
C++
#include <bits/stdc++.h>

using namespace std;

const int N = 4e5 + 5;
int n , m , q , t;
pair< int , int > e[N];

namespace SGT
{
	int root[N] , tot , lc[N*30] , rc[N*30] , s[N*30];
	int Upt(int x , int l , int r , int pos , int k)
	{
		int y = ++tot;
		lc[y] = lc[x] , rc[y] = rc[x] , s[y] = s[x] + k;
		if(l == r) return y;
		int mid = l + r >> 1;
		if(mid >= pos) lc[y] = Upt(lc[x] , l , mid , pos , k);
		else rc[y] = Upt(rc[x] , mid + 1 , r , pos , k);
		return y;
	}
	int Query(int x , int l , int r , int L , int R)
	{
		if(L <= l && r <= R) return s[x];
		int mid = l + r >> 1 , res = 0;
		if(mid >= L) res += Query(lc[x] , l , mid , L , R);
		if(mid < R) res += Query(rc[x] , mid + 1 , r, L , R);
		return res;
	}
	void Upt(int x , int y , int pos , int k) {root[x] = Upt(root[y] , 1 , m , pos , k);}
	int Query(int l , int r) {return Query(root[r] , 1 , m , l , r);}
}

namespace LCT
{
	int fa[N] , lc[N] , rc[N] , rev[N] , tim[N] , mn[N];
	bool Wrc(int x)
	{
	    return rc[fa[x]] == x;
	}
	bool IsRoot(int x)
	{
	    if(!fa[x]) return 1;
	    return lc[fa[x]] != x && rc[fa[x]] != x;
	//若fa=0则x是原树中的根
	//若x不是其父亲Splay中的儿子,则它是其所属路径Splay的根
	}
	void PushUp(int x)
	{
		mn[x] = tim[x];
		mn[x] = min(mn[x] , min(mn[lc[x]] , mn[rc[x]]));
	}
	void PushDown(int x)//下方标记
	{
	    if(rev[x])
	    {
	        swap(lc[x] , rc[x]);
	        if(lc[x]) rev[lc[x]] ^= 1;
	        if(rc[x]) rev[rc[x]] ^= 1;
	        rev[x] = 0;
	    }
	}
	void PushDownAll(int x)
	{
		// cout << x << ' ' << fa[x] << endl;
		if(!IsRoot(x)) PushDownAll(fa[x]);
		PushDown(x);
	}
	void Rotate(int x)
	{
	    int y = fa[x] , z = fa[y] , b = lc[y] == x ? rc[x] : lc[x];
	    if(!IsRoot(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
	    fa[x] = z , fa[y] = x , b ? fa[b] = y : 0;
	    if(lc[y] == x) rc[x] = y , lc[y] = b;
	    else lc[x] = y , rc[y] = b;
	    PushUp(y) , PushUp(x);
	}
	void Splay(int x)
	{
		PushDownAll(x);//及时下放标记
	//由于打了标记,因此根到当前需要Splay的结点的路径上的标记需要全部下放
	    while(!IsRoot(x))
	    {
	        if(!IsRoot(fa[x]))
	        {
	            if(Wrc(x) == Wrc(fa[x])) Rotate(fa[x]);
	            else Rotate(x);
	        }
	        Rotate(x);
	    }
	    PushUp(x);
	}
	void Access(int x)
	{
	    for(int y = 0; x; y = x , x = fa[x])
	    {
	        Splay(x);
	        rc[x] = y;
	        PushUp(x);
	    }
	}
	void MakeRoot(int x)
	{
	    Access(x);
	    Splay(x);
	    rev[x] ^= 1;
	}
	void Split(int x , int y)
	{
		MakeRoot(x);
		Access(y);
		Splay(y);
	}
	void Link(int x , int y)
	{
		Split(x , y);
	    fa[x] = y;
	    PushUp(y);
	}
	void Cut(int x , int y)
	{
	    MakeRoot(x);
	    Access(y);
	    Splay(y);
	    lc[y] = 0 , fa[x] = 0;
	}
	int FindRoot(int x)
	{
	    Access(x);
	    Splay(x);
	    while(PushDown(x) , lc[x]) x = lc[x];
	    Splay(x);
	    return x;
	}
	void init()
	{
		for(int i = 0; i <= n; i++) tim[i] = mn[i] = 1e9;
		for(int i = 1; i <= m; i++) tim[i + n] = mn[i + n] = i;
	}
	int Ask(int x , int y)
	{
		Split(x , y);
		return mn[y];
	}
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m >> q >> t;
    LCT::init();
	for(int i = 1; i <= m; i++) cin >> e[i].first >> e[i].second;
    for(int i = 1 , u , v; i <= m; i++)
    {
    	u = e[i].first , v = e[i].second;
		SGT::Upt(i , i - 1 , i , 1);
		if(u == v) {SGT::Upt(i , i , i , -1); continue ;}
		if(LCT::FindRoot(u) == LCT::FindRoot(v))
		{
			int mn = LCT::Ask(u , v);
			LCT::Cut(e[mn].first , n + mn) , LCT::Cut(e[mn].second , n + mn);
			SGT::Upt(i , i , mn , -1);
		}
		// cout << u << ' ' << v << ' ' << i + n << endl;
		LCT::Link(u , i + n) , LCT::Link(v , i + n);
	}
	for(int i = 1 , lst = 0 , l , r; i <= q; i++)
	{
		cin >> l >> r;
		if(t) l = (l + t * lst) % m + 1 , r = (r + t * lst) % m + 1;
		if(l > r) swap(l , r);
		cout << (lst = n - SGT::Query(l , r)) << '\n';
	}
    return 0;
}
5. CF1172E

首先需要将题意转化。

我们用 LCT 维护一个个联通块的平方和,十分巧妙。

可以发现我们没有 MakeRoot(x) 操作,为什么呢?

代码
C++
# include <bits/stdc++.h>

using namespace std;

# define ll long long

const int N = 4e5 + 5;
int n , m , c[N] , f[N];
vector< int > e[N] , vec[N][2];
ll delta[N] , tot , ans;
int fa[N] , rev[N] , ch[N][2] , vis[N];
ll siz[N] , cnt[N] , siz2[N];

ll s_2(ll x) {return x * x;}

bool Wrc(int x) {return x == ch[fa[x]][1];}

bool IsRoot(int x) {return !fa[x] || x != ch[fa[x]][0] && x != ch[fa[x]][1];}

void PushUp(int x)
{
	siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x] + 1;
}

void Rotate(int x)
{
	int y = fa[x] , z = fa[y] , k = Wrc(x);
	if(!IsRoot(y)) ch[z][ch[z][1] == y] = x;
	ch[y][k] = ch[x][!k] , fa[ch[x][!k]] = y;
	ch[x][!k] = y , fa[y] = x , fa[x] = z;
	PushUp(y) , PushUp(x);
}

void Splay(int x)
{
	while(!IsRoot(x))
	{
		if(!IsRoot(fa[x])) Wrc(x) == Wrc(fa[x]) ? Rotate(fa[x]) : Rotate(x);
		Rotate(x);
	}
	PushUp(x);
}

void Access(int x)
{
	for(int y = 0; x; y = x , x = fa[x])
	{
		Splay(x);
        cnt[x] += siz[ch[x][1]];
        cnt[x] -= siz[y];
        siz2[x] += s_2(siz[ch[x][1]]);
        siz2[x] -= s_2(siz[y]);
        ch[x][1] = y;
        PushUp(x);
	}
}

int FindRoot(int x)
{
	Access(x);
	Splay(x);
	while(ch[x][0]) x = ch[x][0];
	Splay(x);
	return x;
}

void Link(int x)
{
    int y = f[x];
    Splay(x);
    tot -= siz2[x] + s_2(siz[ch[x][1]]);
    int z = FindRoot(y);
    Splay(z);
    tot -= s_2(siz[ch[z][1]]);
    fa[x] = y;
    Splay(y);
    cnt[y] += siz[x];
    siz2[y] += s_2(siz[x]);
    PushUp(y);
    Access(x);
    Splay(z);
    tot += s_2(siz[ch[z][1]]);
}

void Cut(int x)
{
    Access(x);
    tot += siz2[x];
    int z = FindRoot(x);
    Splay(z);
    tot -= s_2(siz[ch[z][1]]);
    Splay(x);
    ch[x][0] = fa[ch[x][0]] = 0;
    PushUp(x);
    Splay(z);
    tot += s_2(siz[ch[z][1]]);
}

void dfs(int u)
{
	for(int i = 0; i < e[u].size(); i++)
	{
		int v = e[u][i];
		if(v == f[u]) continue ;
		f[v] = u;
		dfs(v);
	}
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		cin >> c[i];
		vec[c[i]][0].push_back(i);
		vec[c[i]][1].push_back(0);
	}
	for(int i = 1 , u , v; i < n; i++) cin >> u >> v , e[u].push_back(v) , e[v].push_back(u);
	for(int i = 1 , u , col; i <= m; i++)
	{
		cin >> u >> col;
		vec[c[u]][0].push_back(u);
		vec[c[u]][1].push_back(i);
		c[u] = col;
		vec[col][0].push_back(u);
		vec[col][1].push_back(i);
	}
	dfs(1);
    f[1] = n + 1;
    for(int i = 1; i <= n + 1; i++) siz[i] = 1;
	for(int i = 1; i <= n; i++) Link(i);
	for(int i = 1; i <= n; i++)
	{
		if(!vec[i][0].size()) {delta[0] += s_2(n); continue ;}
		ll lst;
		if(vec[i][1][0]) delta[0] += s_2(n) , lst = s_2(n);
		else lst = 0;
		for(int j = 0; j < vec[i][0].size(); j++)
		{
			int u = vec[i][0][j];
			if(vis[u] ^= 1) Cut(u);
			else Link(u);
			if(j == vec[i][0].size() - 1 || vec[i][1][j] != vec[i][1][j + 1])
			{
				delta[vec[i][1][j]] += tot - lst;
				lst = tot;
			}
		}
		for(int j = vec[i][0].size() - 1; ~j; j--)
		{
			int u = vec[i][0][j];
			if(vis[u] ^= 1) Cut(u);
			else Link(u);
		}
	}
	ans = 1ll * n * n * n;
	for(int i = 0; i <= m; i++)
	{
		ans -= delta[i];
		cout << ans << '\n';
	}
	return 0;
}

二. 利用 Access(x) 操作完成一些修改操作形如“点到根”的操作。

我们发现我们并没有用到 Link(x,y) 和 Cut(x,y) 操作,那我们还用 LCT 干什么呢?

我们主要需要使用我们的 Access(x) 操作,它能够很好地进行“点到根”的操作。可以很直观地感受到。

1. P3703

发现答案就是每个点到根之中虚边的数量,直接用 LCT 维护。

代码
C++
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
int n , m;
vector< int > e[N];
int dep[N] , fa[N][20] , lg[N] , st[N] , ed[N] , arc[N] , Tcnt;

namespace SGT
{
	int mx[N<<2] , tag[N<<2];
# define lc x << 1
# define rc x << 1 | 1
# define mid (l + r >> 1)
	void PushUp(int x) {mx[x] = max(mx[lc] , mx[rc]);}
	void PushDown(int x)
	{
		if(tag[x] != 0)
		{
			tag[lc] += tag[x] , mx[lc] += tag[x];
			tag[rc] += tag[x] , mx[rc] += tag[x];
			tag[x] = 0;
		}
	}
	void Build(int x , int l , int r)
	{
		if(l == r) return mx[x] = dep[arc[l]] , void();
		Build(lc , l , mid) , Build(rc , mid + 1 , r);
		PushUp(x);
	}
	void Upt(int x , int l , int r , int L , int R , int k)
	{
		if(L <= l && r <= R) return tag[x] += k , mx[x] += k , void();
		PushDown(x);
		if(mid >= L) Upt(lc , l , mid , L , R , k);
		if(mid < R) Upt(rc , mid + 1 , r , L , R , k);
		PushUp(x);
	}
	int Query(int x , int l , int r , int L , int R)
	{
		if(L <= l && r <= R) return mx[x];
		int res = 0;
		PushDown(x);
		if(mid >= L) res = max(res , Query(lc , l , mid , L , R));
		if(mid < R) res = max(res , Query(rc , mid + 1 , r , L , R));
		return res;
	}
# undef lc
# undef rc
# undef mid
}

namespace LCT
{
	int fa[N] , lc[N] , rc[N] , rev[N];
	bool Wrc(int x)
	{
	    return rc[fa[x]] == x;
	}
	bool IsRoot(int x)
	{
	    if(!fa[x]) return 1;
	    return lc[fa[x]] != x && rc[fa[x]] != x;
	//若fa=0则x是原树中的根
	//若x不是其父亲Splay中的儿子,则它是其所属路径Splay的根
	}
	void PushDown(int x)//下方标记
	{
	    if(rev[x])
	    {
	        swap(lc[x] , rc[x]);
	        if(lc[x]) rev[lc[x]] ^= 1;
	        if(rc[x]) rev[rc[x]] ^= 1;
	        rev[x] = 0;
	    }
	}
	void PushDownAll(int x)
	{
		if(!IsRoot(x)) PushDownAll(fa[x]);
		PushDown(x);
	}
	void Rotate(int x)
	{
	    int y = fa[x] , z = fa[y] , b = lc[y] == x ? rc[x] : lc[x];
	    if(!IsRoot(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
	    fa[x] = z , fa[y] = x , b ? fa[b] = y : 0;
	    if(lc[y] == x) rc[x] = y , lc[y] = b;
	    else lc[x] = y , rc[y] = b;
	}
	void Splay(int x)
	{
		PushDownAll(x);//及时下放标记
	//由于打了标记,因此根到当前需要Splay的结点的路径上的标记需要全部下放
	    while(!IsRoot(x))
	    {
	        if(!IsRoot(fa[x]))
	        {
	            if(Wrc(x) == Wrc(fa[x])) Rotate(fa[x]);
	            else Rotate(x);
	        }
	        Rotate(x);
	    }
	}
	int Find(int x)
	{
		while(lc[x]) x = lc[x];
		Splay(x);
		return x;
	}
	void Access(int x)
	{
	    for(int y = 0; x; y = x , x = fa[x])
	    {
	        Splay(x);
	        int z = rc[x];
	        rc[x] = y;
	        if(z) z = Find(z) , SGT::Upt(1 , 1 , n , st[z] , ed[z] , 1);
	        if(y) z = Find(rc[x]) , SGT::Upt(1 , 1 , n , st[z] , ed[z] , -1);
			Splay(x);
	    }
	}
}

void dfs(int u , int fat)
{
	st[u] = ++Tcnt , arc[Tcnt] = u;
	LCT::fa[u] = fa[u][0] = fat , dep[u] = dep[fat] + 1;
	for(int i = 1; i <= lg[dep[u]]; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
	for(int i = 0; i < e[u].size(); i++)
	{
		int v = e[u][i];
		if(v ^ fat) dfs(v , u);
	}
	ed[u] = Tcnt;
}

int LCA(int u , int v)
{
	if(dep[u] < dep[v]) swap(u , v);
	while(dep[u] > dep[v]) u = fa[u][lg[dep[u] - dep[v]]];
	if(u == v) return u;
	for(int i = lg[dep[u]]; ~i; i--) if(fa[u][i] ^ fa[v][i]) u = fa[u][i] , v = fa[v][i];
	return fa[u][0];
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for(int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
    for(int i = 1 , u , v; i < n; i++) cin >> u >> v , e[u].push_back(v) , e[v].push_back(u);
    dfs(1 , 0);
    SGT::Build(1 , 1 , n);
    for(int i = 1 , opt , x , y; i <= m; i++)
    {
    	cin >> opt >> x;
    	if(opt == 1) LCT::Access(x);
    	else if(opt == 2)
    	{
    		cin >> y;
    		int lca = LCA(x , y);
    		cout << SGT::Query(1 , 1 , n , st[x] , st[x]) + SGT::Query(1 , 1 , n , st[y] , st[y]) - 2 * SGT::Query(1 , 1 , n , st[lca] , st[lca]) + 1 << '\n';
		}
    	else cout << SGT::Query(1 , 1 , n , st[x] , ed[x]) << '\n';
	}
    return 0;
}
2. CF1137F

每次将一个点到根的路径提取出来染上新的颜色,并改变根,使用 LCT 很简单地维护。

代码
C++
#include <bits/stdc++.h>

using namespace std;

# define ll long long

const int N = 2e5 + 5;
int n , q;
vector< int > e[N];

struct BIT
{
	int s[N<<1];
	int Lowbit(int x) {return x & -x;}
	void Upt(int x , int d) {for(; x <= n + q; x += Lowbit(x)) s[x] += d;}
	int Query(int x) {int res = 0; for(; x; x -= Lowbit(x)) res += s[x]; return res;}
} bit;

int fa[N] , lc[N] , rc[N] , rev[N] , siz[N] , col[N] , ncol;

bool Wrc(int x) {return rc[fa[x]] == x;}

bool IsRoot(int x) {return !fa[x] || lc[fa[x]] != x && rc[fa[x]] != x;}

void PushUp(int x) {siz[x] = siz[lc[x]] + siz[rc[x]] + 1;}

void PushDown(int x)//下方标记
{
	if(rev[x])
	{
		swap(lc[x] , rc[x]);
		if(lc[x]) rev[lc[x]] ^= 1;
		if(rc[x]) rev[rc[x]] ^= 1;
		rev[x] = 0;
	}
	if(lc[x]) col[lc[x]] = col[x];
	if(rc[x]) col[rc[x]] = col[x];
}

void PushDownAll(int x)
{
	if(!IsRoot(x)) PushDownAll(fa[x]);
	PushDown(x);
}

void Rotate(int x)
{
	int y = fa[x] , z = fa[y] , b = lc[y] == x ? rc[x] : lc[x];
	if(!IsRoot(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
	fa[x] = z , fa[y] = x , b ? fa[b] = y : 0;
	if(lc[y] == x) rc[x] = y , lc[y] = b;
	else lc[x] = y , rc[y] = b;
	PushUp(y) , PushUp(x);
}

void Splay(int x)
{
	PushDownAll(x);
	while(!IsRoot(x))
	{
		if(!IsRoot(fa[x]))
		{
			if(Wrc(x) == Wrc(fa[x])) Rotate(fa[x]);
			else Rotate(x);
		}
		Rotate(x);
	}
	PushUp(x);
}

void Access(int x)
{
	for(int y = 0; x; y = x , x = fa[x])
	{
		Splay(x);
		bit.Upt(col[x] , -(siz[lc[x]] + 1));
		bit.Upt(ncol , siz[lc[x]] + 1);
		rc[x] = y;
		PushUp(x);
	}
}

void MakeRoot(int x)
{
	Access(x);
	Splay(x);
	rev[x] ^= 1;
	col[x] = ncol;
}

int Query(int x)
{
	Splay(x);
	return bit.Query(col[x]) - siz[lc[x]];
}

void dfs(int u)
{
	col[u] = u , siz[u] = 1;
	for(int v : e[u]) if(v ^ fa[u])
	{
		fa[v] = u;
		dfs(v);
		if(col[v] > col[u])
		{
			col[u] = col[v];
			siz[u] = siz[v] + 1;
			rc[u] = v;
		}
	}
	bit.Upt(col[u] , 1);
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> q;
	for(int i = 1 , u , v; i < n; i++) cin >> u >> v , e[u].push_back(v) , e[v].push_back(u);
	dfs(n);
	char opt[30];
	for(int i = 1 , u , v , lst = n; i <= q; i++)
	{
		cin >> opt >> u;
		if(opt[0] == 'u') ncol = i + n , MakeRoot(u);
		else if(opt[0] == 'w') cout << Query(u) << '\n';
		else cin >> v , cout << (Query(u) < Query(v) ? u : v) << '\n';
	}
    return 0;
}
3. P4338

第一步转换比较困难,反正后面就是用 Access(x) 来进行一次点到根的修改。

代码
C++
# include <bits/stdc++.h>

using namespace std;

# define ll long long

const int N = 4e5 + 5;
int n , m;
vector< int > e[N];

int fa[N] , rev[N] , ch[N][2] , top[N];
ll siz[N] , cnt[N] , ans , a[N];

bool Wrc(int x) {return x == ch[fa[x]][1];}

bool IsRoot(int x) {return !fa[x] || x != ch[fa[x]][0] && x != ch[fa[x]][1];}

void PushDown(int x)
{
	if(rev[x])
	{
		swap(ch[x][0] , ch[x][1]);
		if(ch[x][0]) rev[ch[x][0]] ^= 1;
		if(ch[x][1]) rev[ch[x][1]] ^= 1;
		rev[x] = 0;
	}
}

void PushDownAll(int x)
{
	if(!IsRoot(x)) PushDownAll(fa[x]);
	PushDown(x);
}

void PushUp(int x)
{
	siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + cnt[x] + a[x];
}

void Rotate(int x)
{
	int y = fa[x] , z = fa[y] , k = Wrc(x);
	if(!IsRoot(y)) ch[z][ch[z][1] == y] = x;
	ch[y][k] = ch[x][!k] , fa[ch[x][!k]] = y;
	ch[x][!k] = y , fa[y] = x , fa[x] = z;
	PushUp(y) , PushUp(x);
}

void Splay(int x)
{
	PushDownAll(x);
	while(!IsRoot(x))
	{
		if(!IsRoot(fa[x])) Wrc(x) == Wrc(fa[x]) ? Rotate(fa[x]) : Rotate(x);
		Rotate(x);
	}
	PushUp(x);
}

void Access(int x , int w)
{
	for(int y = 0; x; y = x , x = fa[x])
	{
		Splay(x);
		ll S = siz[x] - siz[ch[x][0]];
		ans -= top[x] < 2 ? (S - (top[x] ? a[x] : siz[ch[x][1]])) * 2 : S - 1;
		S += w , siz[x] += w , (y ? cnt[x] += w : a[x] += w);
		if(siz[y] * 2 > S) cnt[x] += siz[ch[x][1]] , cnt[x] -= siz[y] , ch[x][1] = y;
		if(siz[ch[x][1]] * 2 > S) top[x] = 0 , ans += (S - siz[ch[x][1]]) * 2;
		else
		{
			if(ch[x][1]) cnt[x] += siz[ch[x][1]] , ch[x][1] = 0;
			if(a[x] * 2 > S) top[x] = 1 , ans += (S - a[x]) * 2;
			else top[x] = 2 , ans += S - 1 , ch[x][1] = 0;
		}
		PushUp(x);
	}
}

void dfs(int u)
{
	ll mx = a[u] , p = u;
	for(int i = 0; i < e[u].size(); i++)
	{
		int v = e[u][i];
		if(v == fa[u]) continue ;
		fa[v] = u;
		dfs(v);
		cnt[u] += siz[v];
		if(siz[v] > mx) mx = siz[v] , p = v;
	}
	siz[u] = cnt[u] + a[u];
	if(mx * 2 > siz[u])
	{
		ans += 2 * (siz[u] - mx);
		if(p == u) top[u] = 1;
		else ch[u][1] = p , cnt[u] -= siz[p];
	}
	else top[u] = 2 , ans += siz[u] - 1;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1 , u , v; i < n; i++) cin >> u >> v , e[u].push_back(v) , e[v].push_back(u);
	dfs(1);
	cout << ans << '\n';
	for(int i = 1 , u , w; i <= m; i++)
	{
		cin >> u >> w;
		Access(u , w);
		cout << ans << '\n';
	}
	return 0;
}

三. LCT 还可以维护基环树。

1. P9201

不会做,所以没有功德无量。

原文