跳到正文

个人记录

P3384 【模板】重链剖分/树链剖分

CaYi约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年4月14日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 6 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define l(x) (x<<1)
#define r(x) (x<<1|1)
using namespace std;
const int con=1e5+7;
typedef long long ll;

struct Segment_tree {
	int pl, pr, sum;
};
Segment_tree t[con<<2];
int n, m, r, mod, cnt, opt;
int w[con], fa[con], top[con], d[con], s[con], wson[con], id_[con];
int tag[con<<2];
int a[con];
vector<int> pa[con];

int read() {
	int ret=0, f=1; char c=getchar();
	while(c<'0'||c>'9') {
		if(c=='-') f*=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') {
		ret=ret*10+c-'0';
		c=getchar();
	}
	return ret*f;
}

void dfs1(int u, int f) {
	fa[u]=f, s[u]=1, d[u]=d[f]+1;
	for(int i : pa[u]) {
		if(i!=f) {//一定要在这里打大括号
			dfs1(i, u);
			s[u]+=s[i];
			if(s[wson[u]]<s[i])
				wson[u]=i;
		}
	}
}

void dfs2(int u, int tp) {//tp记录接下来搜出来一整条的链头
	top[u]=tp;
	id_[u]=++cnt;
	a[cnt]=w[u];//把重链一条一条按照dfn放进辅助数组便于建线段树
	if(wson[u]) dfs2(wson[u], tp);//先搜重儿子
	for(int i :pa[u])
		if(i!=wson[u]&&i!=fa[u])
			dfs2(i, i);
}

//线段树板子不要默错了

void Push_up(int p) {
	t[p].sum=t[l(p)].sum+t[r(p)].sum;
	t[p].pl=t[l(p)].pl;
	t[p].pr=t[r(p)].pr;
}

void Build_tree(int p, int L, int R) {
	if(L==R) {
		t[p].sum=a[L];
		t[p].pl=t[p].pr=L;
		return ;
	}
	int mid=(L+R)>>1;
	Build_tree(l(p), L, mid);
	Build_tree(r(p), mid+1, R);
	Push_up(p);
}

void add_tag(int p, int x) {
	x%=mod;
	tag[p]+=x;
	t[p].sum+=(t[p].pr-t[p].pl+1)*x;
	t[p].sum%=mod;
}

void Spread(int p) {
	if(tag[p]) {
		add_tag(l(p), tag[p]);
		add_tag(r(p), tag[p]);
		tag[p]=0;
	}
}

void add(int p, int L, int R, int x) {
	if(t[p].pl>=L&&t[p].pr<=R) {
		add_tag(p, x);
		return ;
	}
	Spread(p);
	if(t[l(p)].pr>=L) add(l(p), L, R, x);
	if(t[r(p)].pl<=R) add(r(p), L, R, x);
	Push_up(p);
}

ll get(int p, int L, int R) {
	if(t[p].pl>=L&&t[p].pr<=R) return t[p].sum;
	Spread(p);
	ll ret=0;
	if(t[l(p)].pr>=L) ret+=get(l(p), L, R);
	if(t[r(p)].pl<=R) ret+=get(r(p), L, R);
	return ret;
}

void add_path(int x, int y, int z) {//利用重链上dfn连续的特性在线段树上处理
	while(top[x]!=top[y]) {
		if(d[top[x]]>d[top[y]]) swap(x, y);//y的深度大于x
		add(1, id_[top[y]], id_[y], z);
		y=fa[top[y]];
	}
	if(d[x]>d[y]) swap(x, y);//y的深度大于x
	add(1, id_[x], id_[y], z);
}

ll get_path(int x, int y) {
	ll ret=0;
	while(top[x]!=top[y]) {
		if(d[top[x]]>d[top[y]]) swap(x, y);//y的深度大于x
		ret+=get(1, id_[top[y]], id_[y]);
		ret%=mod;
		y=fa[top[y]];
	}
	if(d[x]>d[y]) swap(x, y);//y的深度大于x
	ret+=get(1, id_[x], id_[y]);
	return ret%mod;
}

void add_tree(int x, int z) {
	add(1, id_[x], id_[x]+s[x]-1, z);
}

ll get_tree(int x) {
	return get(1, id_[x], id_[x]+s[x]-1)%mod;
}

int main(){
	n=read(), m=read(), r=read(), mod=read();
	for(int i=1; i<=n; ++i) w[i]=read();
	for(int i=1; i<n; ++i) {
		int u=read(), v=read();
		pa[u].push_back(v);
		pa[v].push_back(u);
	}
	dfs1(r, 0);
	dfs2(r, r);
	Build_tree(1, 1, n);
	while(m--) {
		opt=read();
		if(opt==1) {
			int x=read(), y=read(), z=read();
			add_path(x, y, z);
		}
		if(opt==2) {
			int x=read(), y=read();
			printf("%lld\n", get_path(x, y)%mod);
		}
		if(opt==3) {
			int x=read(), z=read();
			add_tree(x, z);
		}
		if(opt==4) {
			int x=read();
			printf("%lld\n", get_tree(x)%mod);
		}
	}
	return 0;
}

原文