跳到正文

题解

你说得对,但是LCT是一款

Katyusha_01约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年5月14日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 7 分钟
获赞
16
题目
P5327

版本

  1. v1当前

无数篇题解提到了一个 O(mlog⁡3n)O(m\log^3 n) 的做法:将每一条链使用重链剖分划分成 O(log⁡n)O(\log n) 段区间,然后扫描线。此时如果使用 树剖+线段树 维护的话复杂度高达 O(mlog⁡3n)O(m\log^3 n) 据称是无法通过的。但是如果使用 LCT 复杂度就可以直接降至 O(mlog⁡2n)O(m\log^2 n),可以通过。

代码如下,常数略大,没有卡常。

C++
#include<bits/stdc++.h>
using namespace std;
typedef struct{
	int fa,s[2];
	bool rev;
	int mn,mncnt,x;
	int tag;
}Tree;
Tree t[100011];
int cnt;
void pushup(int p)
{
	t[p].mn = min({t[t[p].s[0]].mn,t[p].x,t[t[p].s[1]].mn});
	t[p].mncnt = (t[t[p].s[0]].mn == t[p].mn) * t[t[p].s[0]].mncnt + (t[p].x == t[p].mn) + (t[t[p].s[1]].mn == t[p].mn) * t[t[p].s[1]].mncnt;
}
void Rev(int p)
{
	swap(t[p].s[0],t[p].s[1]),t[p].rev ^= 1;
}
void add(int p,int x)
{
	t[p].x += x,t[p].mn += x,t[p].tag += x;
}
void pushdown(int p)
{
	if(t[p].rev) Rev(t[p].s[0]),Rev(t[p].s[1]),t[p].rev ^= 1;
	add(t[p].s[0],t[p].tag),add(t[p].s[1],t[p].tag),t[p].tag = 0; 
}
bool isroot(int x)
{
	return t[t[x].fa].s[0] != x && t[t[x].fa].s[1] != x;
}
void rotate(int x)
{
	int y = t[x].fa,z = t[y].fa;
	if(!isroot(y))
		t[z].s[t[z].s[1] == y] = x;
	t[x].fa = z;
	int op = (x == t[y].s[1]);
	t[t[y].s[op] = t[x].s[op ^ 1]].fa = y;
	t[t[x].s[op ^ 1] = y].fa = x;
	pushup(y),pushup(x);
}
int st[100011];
void splay(int x)
{
	int top = 0,r = x;
	st[++top] = r;
	while(!isroot(r)) st[++top] = r = t[r].fa;
	while(top) pushdown(st[top--]);
	while(!isroot(x))
	{
		int y = t[x].fa,z = t[y].fa;
		if(!isroot(y))
		{
			if((t[y].s[1] == x) ^ (t[z].s[1] == y)) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
}
void access(int x)
{
	int r = x;
	for(int y = 0;x;y = x,x = t[x].fa)
	{
		splay(x); 
		t[x].s[1] = y;
		pushup(x);
	}
	splay(r);
}
void makeroot(int x)
{
	access(x),Rev(x);
}
void split(int x,int y)
{
	makeroot(x),access(y);
}
void mdf(int x,int y,int w)
{
	split(x,y);
	cnt -= (t[y].mn == 0) * t[y].mncnt;
	add(y,w);
	cnt += (t[y].mn == 0) * t[y].mncnt;
}
int n,m;
vector<int> v[100011];
int fa[100011],dep[100011],siz[100011],bgs[100011],upt[100011],dfn[100011],idx;
void DFS1(int p,int f)
{
	dep[p] = dep[fa[p] = f] + (siz[p] = 1);
	for(auto i:v[p]) if(i != f)
	{
		DFS1(i,p);
		siz[p] += siz[i];
		if(siz[i] > siz[bgs[p]])
			bgs[p] = i;
	}
}
void DFS2(int p,int ut)
{
	upt[p] = ut,dfn[p] = ++idx;
	if(!bgs[p]) return;
	DFS2(bgs[p],ut);
	for(auto i:v[p]) if(!dfn[i])
		DFS2(i,i);
}
vector<pair<int,int> > pick(int x,int y)
{
	vector<pair<int,int> > res;
	while(upt[x] != upt[y])
	{
		if(dep[upt[x]] > dep[upt[y]])
			res.push_back({dfn[upt[x]],dfn[x]}),x = fa[upt[x]];
		else
			res.push_back({dfn[upt[y]],dfn[y]}),y = fa[upt[y]];
	}
	if(dfn[x] > dfn[y]) swap(x,y);
	res.push_back({dfn[x],dfn[y]});
	return res;
}
vector<pair<pair<int,int>,int> > dl[100011];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i < n;i++)
	{
		int x,y;
		cin >> x >> y;
		v[x].push_back(y),v[y].push_back(x);
	}
	DFS1(1,0);
	DFS2(1,1);
	for(int i = 1;i <= n;i++)
		t[i].fa = fa[i],t[i].mncnt = 1;
	cnt = n;
	for(int i = 1;i <= m;i++)
	{
		int x,y;
		cin >> x >> y;
		vector<pair<int,int> > ar = pick(x,y);
		for(auto o:ar)
			dl[o.first].push_back({{x,y},1}),dl[o.second + 1].push_back({{x,y},-1});
	}
	long long ans = 0;
	for(int i = 1;i <= n;i++)
	{
		for(auto x:dl[i])
			mdf(x.first.first,x.first.second,x.second);
		ans += max((n - cnt) - 1,0);
	}
	cout << ans / 2;
	return 0;
}
原文
存档评论3 条抓取于