P3384树链剖分模板

#include <bits/stdc++.h>
#define lson rt << 1
#define rson rt << 1 | 1
#define int long long
using namespace std;
const int maxn = 1e5 + 10;
struct Edge
{
	int next, to;
} edges[maxn * 2];
int cnt, tot, head[maxn], fa[maxn], deep[maxn], son[maxn], sz[maxn], top[maxn], id[maxn], rk[maxn];
int n, m, r, mod, v[maxn];
void init()
{
	cnt = tot = 0;
	memset(head, -1, sizeof(head));
}
void addEdge(int x, int y)
{
	edges[tot].next = head[x];
	edges[tot].to = y;
	head[x] = tot++;
}
/*树链剖分部分*/
void dfs1(int x)
{
	sz[x] = 1, deep[x] = deep[fa[x]] + 1;
	for (int v, i = head[x]; ~i; i = edges[i].next)
		if ((v = edges[i].to) != fa[x])
		{
			fa[v] = x, dfs1(v), sz[x] += sz[v];
			if (sz[son[x]] < sz[v])
				son[x] = v;
		}
}
void dfs2(int x, int tp)
{
	top[x] = tp, id[x] = ++cnt, rk[cnt] = x;
	if (son[x])
		dfs2(son[x], tp);
	for (int v, i = head[x]; ~i; i = edges[i].next)
		if ((v = edges[i].to) != fa[x] && v != son[x])
			dfs2(v, v);
}

/*线段树部分*/
struct node
{
	int l, r;
	int sum;
	int segflag;
} seg[maxn << 2];

void push_up(int rt)
{
	seg[rt].sum = (seg[lson].sum + seg[rson].sum) % mod;
}

void push_down(int rt)
{
	(seg[lson].segflag += seg[rt].segflag) %= mod;
	(seg[rson].segflag += seg[rt].segflag) %= mod;
	(seg[lson].sum += (seg[lson].r - seg[lson].l + 1) * seg[rt].segflag) %= mod;
	(seg[rson].sum += (seg[rson].r - seg[rson].l + 1) * seg[rt].segflag) %= mod;
	seg[rt].segflag = 0;
}

void build(int rt, int l, int r)
{
	seg[rt].segflag = 0;
	seg[rt].l = l;
	seg[rt].r = r;
	if (l == r)
	{
		seg[rt].sum = v[rk[l]];
		return;
	}
	int mid = (l + r) >> 1;
	build(lson, l, mid);
	build(rson, mid + 1, r);
	push_up(rt);
}

void update(int rt, int l, int r, int val)
{
	if (seg[rt].l == l && seg[rt].r == r)
	{
		(seg[rt].segflag += val) %= mod;
		(seg[rt].sum += (seg[rt].r - seg[rt].l + 1) * val) %= mod;
		return;
	}
	push_down(rt);
	int mid = (seg[rt].l + seg[rt].r) >> 1;
	if (r <= mid)
	{
		update(lson, l, r, val);
	}
	else if (l >= mid + 1)
	{
		update(rson, l, r, val);
	}
	else
		update(lson, l, mid, val), update(rson, mid + 1, r, val);
	push_up(rt);
}

int query(int rt, int l, int r)
{
	if (seg[rt].l == l && seg[rt].r == r)
	{
		return seg[rt].sum;
	}
	push_down(rt);
	int mid = (seg[rt].l + seg[rt].r) >> 1;
	if (r <= mid)
	{
		return query(lson, l, r);
	}
	else if (l >= mid + 1)
	{
		return query(rson, l, r);
	}
	else
		return (query(lson, l, mid) + query(rson, mid + 1, r)) % mod;
}

inline int sum(int x, int y)
{
	int ret = 0;
	while (top[x] != top[y])
	{
		if (deep[top[x]] < deep[top[y]])
			swap(x, y);
		(ret += query(1, id[top[x]], id[x])) %= mod;
		x = fa[top[x]];
	}
	if (id[x] > id[y])
		swap(x, y);
	return (ret + query(1, id[x], id[y])) % mod;
}
inline void updates(int x, int y, int c)
{
	while (top[x] != top[y])
	{
		if (deep[top[x]] < deep[top[y]])
			swap(x, y);
		update(1, id[top[x]], id[x], c);
		x = fa[top[x]];
	}
	if (id[x] > id[y])
		swap(x, y);
	update(1, id[x], id[y], c);
}
signed main()
{
	scanf("%lld%lld%lld%lld", &n, &m, &r, &mod);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &v[i]);
	init();
	for (int x, y, i = 1; i < n; i++)
	{
		scanf("%lld%lld", &x, &y);
		addEdge(x, y), addEdge(y, x);
	}
	dfs1(r), dfs2(r, r);
	build(1, 1, n);
	for (int op, x, y, k, i = 1; i <= m; i++)
	{
		scanf("%lld", &op);
		if (op == 1)
		{
			scanf("%lld%lld%lld", &x, &y, &k);
			updates(x, y, k);
		}
		else if (op == 2)
		{
			scanf("%lld%lld", &x, &y);
			printf("%lld\n", sum(x, y));
		}
		else if (op == 3)
		{
			scanf("%lld%lld", &x, &y);
			update(1, id[x], id[x] + sz[x] - 1, y);
		}
		else
		{
			scanf("%lld", &x);
			printf("%lld\n", query(1, id[x], id[x] + sz[x] - 1));
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值