其他分享
首页 > 其他分享> > P3384 【模板】轻重链剖分

P3384 【模板】轻重链剖分

作者:互联网

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
int arr[maxn],w[maxn],n,p;
struct node
{
	int v,nxt;
}e[maxn<<1];
int head[maxn],cnt;
int dep[maxn],fa[maxn],siz[maxn],son[maxn],id[maxn],top[maxn],num;
int tree[4*maxn],laz[4*maxn];
void add(int u,int v)
{
	e[++cnt].v=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
void dfs1(int u,int f,int deep)
{
	dep[u]=deep;//深度
	fa[u]=f;//父亲
	siz[u]=1;//自己及儿子节点数
	int maxson=-1;
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==f)continue;
		dfs1(v,u,deep+1);
		siz[u]+=siz[v];
		if(siz[v]>maxson)
		{
			maxson=siz[v];
			son[u]=v;//重儿子
		}
	}
}
void dfs2(int u,int topf)
{
	id[u]=++num;//dfs序
	w[num]=arr[u];//重新编号完的数组
	top[u]=topf;//重链父亲
	if(!son[u])return;
	dfs2(son[u],topf);
	for(int i=head[u];i;i=e[i].nxt)
	{
		int v=e[i].v;
		if(v==fa[u]||v==son[u])continue;
		dfs2(v,v);
	}
}
void buildtree(int node,int start,int end)
{
	if(start==end)tree[node]=w[start]%p;
	else
	{
		int mid=(start+end)/2;
		int lnode=node*2, rnode=node*2+1;
		buildtree(lnode,start,mid);
		buildtree(rnode,mid+1,end);
		tree[node]=(tree[lnode]+tree[rnode])%p;
	}
}
void pushdown(int node,int start,int end)
{
	int mid=(start+end)/2;
	tree[node*2]+=(mid-start+1)*laz[node];
	tree[node*2+1]+=(end-mid)*laz[node];
	tree[node*2]%=p, tree[node*2+1]%=p;
	laz[node*2]+=laz[node];
	laz[node*2+1]+=laz[node];
	laz[node]=0;
}
void updatetree(int node,int start,int end,int L,int R,int val)
{
	if(L<=start&&end<=R)
	{
		tree[node]+=(end-start+1)*val;
		tree[node]%=p;
		laz[node]+=val;
		return;
	}
	if(laz[node])pushdown(node,start,end);
	int mid=(start+end)/2;
	int lnode=node*2, rnode=node*2+1;
	if(L<=mid)updatetree(lnode,start,mid,L,R,val);
	if(R>mid)updatetree(rnode,mid+1,end,L,R,val);
	tree[node]=(tree[lnode]+tree[rnode])%p;
}
int querytree(int node,int start,int end,int L,int R)
{
	int ans=0;
	if(L<=start&&end<=R)
	{
		return tree[node];
	}
	if(laz[node])pushdown(node,start,end);
	int mid=(start+end)/2;
	if(L<=mid)ans=(ans+querytree(node*2,start,mid,L,R))%p;
	if(R>mid)ans=(ans+querytree(node*2+1,mid+1,end,L,R))%p;
	return ans;
}
int qrange(int x,int y)
{
	int ans=0;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		ans+=querytree(1,1,n,id[top[x]],id[x]);
		ans%=p;
		x=fa[top[x]];
	}
	if(dep[x]>dep[y])swap(x,y);
	ans+=querytree(1,1,n,id[x],id[y]);
	return ans%p;
}
void updarange(int x,int y,int val)
{
	val%=p;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		updatetree(1,1,n,id[top[x]],id[x],val);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y])swap(x,y);
	updatetree(1,1,n,id[x],id[y],val);
}
int qson(int x)
{
	int ans=querytree(1,1,n,id[x],id[x]+siz[x]-1);
	return ans;
}
void updason(int x,int val)
{
	val%=p;
	updatetree(1,1,n,id[x],id[x]+siz[x]-1,val);
}
int main()
{
	int m,r;
	scanf("%d%d%d%d",&n,&m,&r,&p);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&arr[i]);
	}
	int u,v;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&u,&v);
		add(u,v),add(v,u);
	}
	dfs1(r,0,1);
	dfs2(r,r);
	buildtree(1,1,n);
	int k,x,y,z;
	while(m--)
	{
		scanf("%d",&k);
		if(k==1)
		{
			scanf("%d%d%d",&x,&y,&z);
			updarange(x,y,z);
		}
		else if(k==2)
		{
			scanf("%d%d",&x,&y);
			printf("%d\n",qrange(x,y));
		}
		else if(k==3)
		{
			scanf("%d%d",&x,&z);
			updason(x,z);
		}
		else
		{
			scanf("%d",&x);
			printf("%d\n",qson(x));
		}
	}
	return 0;
}

标签:node,链剖分,end,int,tree,mid,start,P3384,模板
来源: https://blog.csdn.net/weixin_45714726/article/details/111243331