树上邻域数点(set,dsu on tree,换根)

树上邻域数点(count)

题目

3s,512MB \texttt{3s,512MB} 3s,512MB

题目背景

Y Y Y 不喜欢 t o p t r e e toptree toptree

题目描述

有一个 n n n 个点的树 T ( r ) = ( V , E ) T(r)=(V,E) T(r)=(V,E),其中 V V V T T T 的点集, E E E T T T 的边集,点的编号依次为 1 , 2 , 3 , 3 , … … , n 1,2,3,3,……,n 1,2,3,3……,n,根节点为 r r r ,记 $ x $ 节点的深度 D ( x ) D(x) D(x) 表示节点 x x x r r r 的唯一简单路径的边的条数。

对于一个非空点集 S ⊆ V S\subseteq V SV ,我们可以对 S S S 进行 任意次数 如下操作,称为拓展

  • 选择 x ∈ S , y ∈ S x\in S,y\in S xS,yS ,然后把 x x x y y y 的唯一简单路径上的所有点加入 S S S

如果对于某一个由拓展得到的 S ′ S' S,其无法再通过拓展得到别的点集,我们就称 S ′ S' S 是最初的点集 S S S完备邻域,记作 N ( S ) = S ′ N(S)=S' N(S)=S ,可证明 N ( S ) N(S) N(S) 是存在且唯一的。

R ( S ) R(S) R(S) 代表所有 x ∈ N ( S ) x\in N(S) xN(S) x x x 中, D ( x ) D(x) D(x) 最小的 x x x 的编号。

现在给定一个排列 p 1 , p 2 … … , p n p_1,p_2……,p_n p1,p2……,pn,请你对于 r = 1 → n r=1\to n r=1n,求出 :

∑ 1 ≤ l ≤ r ≤ n R ( { p l , p l + 1 … … p r − 1 , p r } ) \sum_{1\leq l\leq r\le n}R(\{p_l,p_{l+1}……p_{r-1},p_r\}) 1lrnR({pl,pl+1……pr1,pr})

输入格式

第一行一个正整数 n n n

接下来 n − 1 n-1 n1 行,每行两个整数 x , y x,y x,y,代表一条树上的边 ( x , y ) ∈ E (x,y)\in E (x,y)E

接下来一个长度为 n n n 的排列代表 p n p_n pn

输出格式

一共 n n n 个数,第 i i i 个数代表 r = i r=i r=i 时的答案。

输入样例
5
1 3
5 1
4 3
4 2
1 3 4 5 2
输出样例
27
46
45
54
55
数据规模
  • 15 % 15\% 15% 1 ≤ n ≤ 500 1\leq n\leq 500 1n500
  • 15 % 15\% 15% 1 ≤ n ≤ 2000 1\leq n\leq 2000 1n2000
  • 20 % 20\% 20% 1 ≤ n ≤ 9000 1\leq n\leq 9000 1n9000
  • 20 % 20\% 20% x i = i , y i = i + 1 x_i=i,y_i=i+1 xi=i,yi=i+1
  • 20 % 20\% 20% 1 ≤ n ≤ 1 × 1 0 5 1\leq n\leq 1\times 10^5 1n1×105
  • 100 % 100\% 100% 1 ≤ n ≤ 3 × 1 0 5 1\leq n\leq 3\times 10^5 1n3×105

树上邻域数点(count)

分析

题目让求的其实就是以每个点为根时,所有排列的区间的 l c a lca lca 之和。

15pts

考虑直接以每一个节点为根,暴力统计 n 2 n^2 n2 个区间的 l c a lca lca 之和。

时间复杂度 O ( n 3 l o g   n ) O(n^3log\,n) O(n3logn) ,可以获得 15   p t s 15\,pts 15pts 的高分。

//15pts
for(int i=1;i<=n;i++)
{
    dfs(i,0);
    Dfs(i,0,i);//树剖求lca
    ans=0;
    for(int j=1;j<=n;j++)
    {
        int l=p[j];
        ans+=l;
        for(int k=j+1;k<=n;k++)
        {
            l=lca(l,p[k]);
            ans+=l;
        }
    }
    printf("%d\n",ans);
}
30pts

枚举区间的做法是 n a i v e naive naive 的,考虑如何批量计算区间贡献。

套路的,我们考虑一个点会贡献给哪些区间,但是一段区间的 l c a lca lca 不一定是这个区间里的数,很麻烦,怎么办?

我们考虑先对原序列邻项求一个 l c a lca lca ,显然答案是序列中各数之和加上新序列的各区间的 l c a lca lca 之和,这样处理完后我们可以发现,一个区间的 l c a lca lca 实际上就是这个区间中 d e p dep dep 最小的数(因为邻项 l c a lca lca 实际上组成了一条链)。这个可以用单调栈来简单计算。

时间复杂度 O ( n 2 l o g   n ) O(n^2log\,n) O(n2logn) ,可以获得 30   p t s 30\,pts 30pts

//30pts
for(int i=1;i<=n;i++)
{
    dfs(i,0);
    Dfs(i,0,i);
    ans=(1+n)*n/2;
    for(int j=1;j<n;j++)
    a[j]=lca(p[j],p[j+1]);
    while(!q.empty())
    q.pop();
    for(int j=1;j<n;j++)//单调栈
    {
        while(!q.empty()&&dep[a[q.top()]]>dep[a[j]])
        q.pop();
        if(q.empty())
        l[j]=1;
        else
        l[j]=q.top()+1;
        q.push(j);
    }
    while(!q.empty())
    q.pop();
    for(int j=n-1;j;j--)
    {
        while(!q.empty()&&dep[a[q.top()]]>=dep[a[j]])
        q.pop();
        if(q.empty())
        r[j]=n-1;
        else
        r[j]=q.top()-1;
        q.push(j);
    }
    for(int j=1;j<n;j++)
    ans+=1LL*(j-l[j]+1)*(r[j]-j+1)*a[j];
    printf("%lld\n",ans);
}
50pts

上述算法的复杂度瓶颈在于换根时求邻项 l c a lca lca ,直接硬算肯定不行的,我们需要一些技巧。

我们枚举根的时候不要直接 f o r for for 循环去枚举,考虑从 1 1 1 开始 d f s dfs dfs ,这样的好处是,假设现在的根是 x x x ,如果我们已经知道了两点以 f a x fa_x fax 为根时的 l c a lca lca ,那么容易发现 l c a lca lca 要么变成 x x x ,要么不变,而 l c a lca lca 变为 x x x 当且仅当 x x x 在两点之间的路径上时。所以我们可以先预处理出来所有邻项之间的路径和初始 l c a lca lca O ( 1 ) O(1) O(1) 转移即可。

也可以利用一个结论直接来求:如果 l c a 1 ( x , y ) lca_1(x,y) lca1(x,y) 表示 1 1 1 为根的时候 x , y x,y x,y l c a lca lca ,那么以 z z z 为根时 l c a lca lca 就是: l c a 1 ( x , y ) ⨁ l c a 1 ( x , z ) ⨁ l c a 1 ( y , z ) lca_1(x,y)\bigoplus lca_1(x,z)\bigoplus lca_1(y,z) lca1(x,y)lca1(x,z)lca1(y,z) ,需要用到 O ( 1 )   l c a O(1)\,lca O(1)lca

时间复杂度 O ( n 2 ) O(n^2) O(n2) ,可以得到 50 p t s 50pts 50pts

请注意常数因子对程序的影响。

//50pts
void solve(int x,int fa)
{
	dfs(x,0);//求dep数组
	ans=(1+n)*n/2;
	for(int i=1;i<n;i++)
	if(v[x][i])
	a[i]=x;
	while(!q.empty())
	q.pop();
	for(int i=1;i<n;i++)
	{
		while(!q.empty()&&dep[a[q.top()]]>dep[a[i]])
		q.pop();
		if(q.empty())
		l[i]=1;
		else
		l[i]=q.top()+1;
		q.push(i);
	}
	while(!q.empty())
	q.pop();
	for(int i=n-1;i;i--)
	{
		while(!q.empty()&&dep[a[q.top()]]>=dep[a[i]])
		q.pop();
		if(q.empty())
		r[i]=n-1;
		else
		r[i]=q.top()-1;
		q.push(i);
	}
	for(int i=1;i<n;i++)
	ans+=1LL*(i-l[i]+1)*(r[i]-i+1)*a[i];
	b[x]=ans;
	int jl[9010];//存当前层lca
	for(int i=1;i<n;i++)
	jl[i]=a[i];
	for(int i=head[x];~i;i=edge[i].nex)
	{
		if(edge[i].to==fa)
		continue;
		for(int j=1;j<n;j++)
		a[j]=jl[j];
		solve(edge[i].to,x);
	}
}

int main()
{
    
	dfs(1,0);
	for(int i=1;i<n;i++)//预处理路径
	{
		int x=p[i],y=p[i+1];
		v[x][i]=1;
		v[y][i]=1;
		while(x!=y)
		{
			if(dep[x]<dep[y])
			swap(x,y);
			x=F[x];
			v[x][i]=1;
		}
		d[i]=x;
	}
	solve(1,0);
	for(int i=1;i<=n;i++)
	printf("%lld\n",b[i]);
	return 0;
}
70pts

如果树是一条链该怎么搞,我们发现每次换根的时候有很多区间的 l c a lca lca 其实并没有发生改变,那么对他们进行计算实际上就是在浪费复杂度。

考虑有哪些区间的 l c a lca lca 没有改变,显然是区间内所有数换根前后都全部在根的左边或右边,这启发我们去维护树的左右子树。

开一个 s e t set set ,维护左右子树里排列的连续段,在换根时动态维护左右子树的答案,没有统计的区间就是跨过根的区间,直接计算即可。

时间复杂度 O ( n   l o g   n ) O(n\,log\,n) O(nlogn) ,配合前面的算法可获得 70   p t s 70\,pts 70pts

//70pts
#include<bits/stdc++.h>
using namespace std;
int n,p[300010],id[300010],l[300010],r[300010];
long long ans,num1,num2;//ans表示答案,num1/num2表示左/右子树的区间个数
struct node
{
	int l,r;
	bool operator <(const node &y)const
	{
		return l<y.l;
	}
};
set<node> q1,q2;
stack<int> st;
set<node>::iterator split(int x,set<node> &q)//分裂区间,可以同珂朵莉树的写法
{
	set<node>::iterator it=q.lower_bound((node){x,0});
	if(it!=q.end()&&it->l==x)
	return it;
	it--;
	int L=it->l,R=it->r;
	if(R<x)
	return q.end();
	q.erase(it);
	q.insert((node){L,x-1});
	return q.insert((node){x,R}).first;
}
int main()
{
	freopen("cin.in","r",stdin);
	freopen("out.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&p[i]);
		id[p[i]]=i;
	}
	q2.insert((node){1,n});
	if(id[1]!=n)
	split(id[1]+1,q2);
	q2.erase(split(id[1],q2));
	num2=1LL*n*(n+1)/2;
	for(int i=1;i<=n;i++)
	{
		while(!st.empty()&&p[i]<p[st.top()])
		st.pop();
		if(st.empty())
		l[i]=1;
		else
		l[i]=st.top()+1;
		st.push(i);
	}
	while(!st.empty())
	st.pop();
	for(int i=n;i;i--)
	{
		while(!st.empty()&&p[i]<p[st.top()])
		st.pop();
		if(st.empty())
		r[i]=n;
		else
		r[i]=st.top()-1;
		st.push(i);
	}
	long long jl;
	for(int i=1;i<=n;i++)
	{
		ans+=1LL*(i-l[i]+1)*(r[i]-i+1)*p[i];
		if(p[i]==1)
		{
			jl=1LL*(i-l[i]+1)*(r[i]-i+1);
			num2-=1LL*(i-l[i]+1)*(r[i]-i+1);
		}
	}
	printf("%lld\n",ans);
	ans-=jl;
	for(int i=1;i<n;i++)
	{
        //左子树插入节点,合并区间
		set<node>::iterator it=q1.lower_bound((node){id[i],0});
		if(it!=q1.begin())
		it--;
		if(it!=q1.end()&&it->r+1==id[i])
		{
			int L=it->l,len=it->r-it->l+1;
			num1-=1LL*(len+1)*len/2;
			q1.erase(it);
			it=q1.insert((node){L,id[i]}).first;
			num1+=1LL*(len+2)*(len+1)/2;
		}
		else
		{
			num1++;
			q1.insert((node){id[i],id[i]});
		}
		set<node>::iterator it2=q1.upper_bound((node){id[i],0});
		if(it2!=q1.end()&&it2->l==id[i]+1)
		{
			set<node>::iterator it1=q1.upper_bound((node){id[i],0});
			it1--;
			int L=it1->l,R=it2->r,len1=it1->r-it1->l+1,len2=it2->r-it2->l+1;
			num1-=1LL*(len1+1)*len1/2+1LL*(len2+1)*len2/2;
			num1+=1LL*(len1+len2+1)*(len1+len2)/2;
			q1.erase(it1);
			q1.erase(it2);
			q1.insert((node){L,R});
		}
		it=q1.upper_bound((node){id[i],0});
		it--;
		ans+=1LL*(id[i]-it->l+1)*(it->r-id[i]+1)*i;
        //右子树删除节点,分裂区间
		it=q2.upper_bound((node){id[i+1],0});
		it--;
		ans-=1LL*(id[i+1]-it->l+1)*(it->r-id[i+1]+1)*(i+1);
		num2-=1LL*(id[i+1]-it->l+1)*(it->r-id[i+1]+1);
		split(id[i+1]+1,q2);
		it=split(id[i+1],q2);
		q2.erase(it);
		printf("%lld\n",ans+(1LL*(n+1)*n/2-num1-num2)*(i+1));
	}
	return 0;
}
100pts

我们想延续链上的做法,但是这里每个点的子树个数不再为 2 2 2 ,需要维护的信息很多,不太能做。

我们考虑如果一个根定了,先计算每个节点对答案的贡献,再换根。

怎么计算每个节点对答案的贡献?我们记 A x A_x Ax 表示有多少个区间在 x x x 的子树内,那么显然贡献为 A x − ∑ y ∈ s o n x A y A_x-\sum\limits_{y\in son_x}A_y AxysonxAy

怎么换根?容易发现换成以 x x x 为根的时候只有 x x x f a x fa_x fax 的贡献会变,也只有这两个点的 A A A 数组会变,我们只要求出两者改变后的 A A A 数组,问题就迎刃而解了。 A x A_x Ax 显然等于总区间数,我们记 B x B_x Bx 表示有多少个区间在 x x x 的子树外,那么就有 A f a x = B x A_{fa_x}=B_x Afax=Bx ,之后用之前的答案减去之前 x x x f a x fa_x fax 的贡献,再加上现在 x x x f a x fa_x fax 的贡献即可。

最后就是如何求解 A x A_x Ax B x B_x Bx 的问题了,我们可以仿照链的做法,暴力维护区间连续段,再加上一个 d s u    o n    t r e e dsu\;on\;tree dsuontree 即可。

时间复杂度 O ( n   l o g 2 n ) O(n\,log^2n) O(nlog2n) ,可以获得 100   p t s 100\,pts 100pts

//100pts
#include<bits/stdc++.h>
using namespace std;
int n,p[300010],id[300010],l[300010],r[300010],siz[300010],son[300010],head[300010],tot=-1;
long long ans,num1,num2,a[300010],b[300010],sum[300010];
struct E
{
	int to,nex;
} edge[600010];
struct node
{
	int l,r;
	bool operator <(const node &y)const
	{
		return l<y.l;
	}
};
set<node> q1,q2;
stack<int> st;
void add(int x,int y)
{
	edge[++tot].nex=head[x];
	edge[tot].to=y;
	head[x]=tot;
}
set<node>::iterator split(int x,set<node> &q)
{
	set<node>::iterator it=q.lower_bound((node){x,0});
	if(it!=q.end()&&it->l==x)
	return it;
	it--;
	int L=it->l,R=it->r;
	if(R<x)
	return q.end();
	q.erase(it);
	q.insert((node){L,x-1});
	return q.insert((node){x,R}).first;
}
void Add(int x,set<node> &q,long long &num)//加点
{
	set<node>::iterator it=q.lower_bound((node){id[x],0});
	if(it!=q.begin())
	it--;
	if(it!=q.end()&&it->r+1==id[x])
	{
		int L=it->l,len=it->r-it->l+1;
		num-=1LL*(len+1)*len/2;
		q.erase(it);
		it=q.insert((node){L,id[x]}).first;
		num+=1LL*(len+2)*(len+1)/2;
	}
	else
	{
		num++;
		q.insert((node){id[x],id[x]});
	}
	set<node>::iterator it2=q.upper_bound((node){id[x],0});
	if(it2!=q.end()&&it2->l==id[x]+1)
	{
		set<node>::iterator it1=q.upper_bound((node){id[x],0});
		it1--;
		int L=it1->l,R=it2->r,len1=it1->r-it1->l+1,len2=it2->r-it2->l+1;
		num-=1LL*(len1+1)*len1/2+1LL*(len2+1)*len2/2;
		num+=1LL*(len1+len2+1)*(len1+len2)/2;
		q.erase(it1);
		q.erase(it2);
		q.insert((node){L,R});
	}
	it=q.upper_bound((node){id[x],0});
	it--;
}
void Erase(int x,set<node> &q,long long &num)//删点
{
	set<node>::iterator it=q.upper_bound((node){id[x],0});
	it--;
	num-=1LL*(id[x]-it->l+1)*(it->r-id[x]+1);
	split(id[x]+1,q);
	it=split(id[x],q);
	q.erase(it);
}
void D(int x,int fa)
{
	siz[x]=1;
	for(int i=head[x];~i;i=edge[i].nex)
	{
		if(edge[i].to==fa)
		continue;
		D(edge[i].to,x);
		siz[x]+=siz[edge[i].to];
		if(siz[edge[i].to]>siz[son[x]])
		son[x]=edge[i].to;
	}
}
void Clear(int x,int fa)
{
	Erase(x,q1,num1);
	Add(x,q2,num2);
	for(int i=head[x];~i;i=edge[i].nex)
	{
		if(edge[i].to==fa)
		continue;
		Clear(edge[i].to,x);
	}
}
void Dfs(int x,int fa)
{
	Erase(x,q2,num2);
	Add(x,q1,num1);
	for(int i=head[x];~i;i=edge[i].nex)
	{
		if(edge[i].to==fa)
		continue;
		Dfs(edge[i].to,x);
	}
}
void dfs(int x,int fa)//dsu on tree
{
    for(int i=head[x];~i;i=edge[i].nex)
    {
    	if(edge[i].to==fa||edge[i].to==son[x])
    	continue;
    	dfs(edge[i].to,x);
    	Clear(edge[i].to,x);
	}
	if(son[x])
	dfs(son[x],x);
	for(int i=head[x];~i;i=edge[i].nex)
	{
		if(edge[i].to!=fa&&edge[i].to!=son[x])
		Dfs(edge[i].to,x);
	}
	Erase(x,q2,num2);
	Add(x,q1,num1);
	a[x]=num1;
	b[x]=num2;
}
void getans(int x,int fa)
{
	ans+=a[x]*x;
	for(int i=head[x];~i;i=edge[i].nex)
	{
		if(edge[i].to==fa)
		continue;
		ans-=a[edge[i].to]*x;
		getans(edge[i].to,x);
	}
}
void hg(int x,int fa,long long y)//换根
{
	if(x==1)
	{
		for(int i=head[x];~i;i=edge[i].nex)
		hg(edge[i].to,x,sum[x]);
		return;
	}
	long long jl1=a[x],jl2=b[x],jl3=a[fa],jl4=b[fa];
	sum[x]=y-1LL*n*(n+1)/2*(fa-x)+(b[x]+a[x])*fa-a[x]*x;
	a[fa]=b[x];
	b[fa]=a[x];
	a[x]=1LL*n*(n+1)/2;
	b[x]=0;
	sum[x]-=a[fa]*x;
	for(int i=head[x];~i;i=edge[i].nex)
	{
		if(edge[i].to==fa)
		continue;
		hg(edge[i].to,x,sum[x]);
	}
	a[x]=jl1;
	b[x]=jl2;
	a[fa]=jl3;
	b[fa]=jl4;
}
int main()
{
	freopen("count.in","r",stdin);
	freopen("count.out","w",stdout);
	memset(head,-1,sizeof head);
	scanf("%d",&n);
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&p[i]);
		id[p[i]]=i;
	}
	q2.insert((node){1,n});
	num2=1LL*(n+1)*n/2;
	D(1,0);
	dfs(1,0);
	getans(1,0);
	sum[1]=ans;
	hg(1,0,ans);
	for(int i=1;i<=n;i++)
	printf("%lld\n",sum[i]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值