Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

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

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

板子题。

因为 $dfn$ 是连续的,所以以 $x$ 为根节点的子树范围是 dfn[x], dfn[x] + sz[x] - 1

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
int n, m, rt, mod, w[N];
int idx, hd[N], nxt[M], edg[M], wt[M];
int stmp, d[N], p[N], sz[N], son[N], top[N], rnk[N], dfn[N];
struct Node
{
int l, r, v, mark;
}tr[N << 2];
void pushup (int x)
{
tr[x].v = (tr[x << 1].v + tr[x << 1 | 1].v) % mod;
}
void pushdown (int x)
{
tr[x << 1].mark = (tr[x << 1].mark + tr[x].mark) % mod;
tr[x << 1 | 1].mark =(tr[x << 1 | 1].mark + tr[x].mark) % mod;
tr[x << 1].v = (tr[x << 1].v + tr[x].mark * (tr[x << 1].r - tr[x << 1].l + 1) % mod) % mod;
tr[x << 1 | 1].v = (tr[x << 1 | 1].v + tr[x].mark * (tr[x << 1 | 1].r - tr[x << 1 | 1].l + 1) % mod) % mod;
tr[x].mark = 0;
}
int query(int x, int l, int r)
{
if (tr[x].l >= l && tr[x].r <= r)
return tr[x].v;
if (tr[x].mark)
pushdown(x);
int res = 0;
int mid = tr[x].l + tr[x].r >> 1;
if (l <= mid)
res = (res + query(x << 1, l, r)) % mod;
if (r > mid)
res = (res + query(x << 1 | 1, l, r)) % mod;
return res;
}
void modify (int x, int l, int r, int k)
{
if (tr[x].l >= l && tr[x].r <= r)
{
tr[x].mark = (tr[x].mark + k) % mod;
tr[x].v = (tr[x].v + k * (tr[x].r - tr[x].l + 1)) % mod;
return;
}
if (tr[x].mark)
pushdown(x);
int mid = tr[x].l + tr[x].r >> 1;
if (l <= mid)
modify(x << 1, l, r, k);
if (r > mid)
modify(x << 1 | 1, l, r, k);
pushup(x);
}
void build (int x, int l, int r)
{
tr[x].l = l;
tr[x].r = r;
if (l == r)
{
tr[x].v = w[rnk[l]] % mod;
return;
}
int mid = l + r >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
pushup(x);
}
void AddPath(int x, int y, int k)
{
while (top[x] != top[y])
{
if (d[top[x]] < d[top[y]])
swap(x, y);
modify(1, dfn[top[x]], dfn[x], k);
x = p[top[x]];
}
if (d[x] > d[y])
swap(x, y);
modify(1, dfn[x], dfn[y], k);
}
int QueryPath (int x, int y)
{
int res = 0;
while (top[x] != top[y])
{
if (d[top[x]] < d[top[y]])
swap(x, y);
res = (res + query(1, dfn[top[x]], dfn[x])) % mod;
x = p[top[x]];
}
if (d[x] > d[y])
swap(x, y);
return (res + query(1, dfn[x], dfn[y])) % mod;
}
void AddTree(int x, int k)
{
modify(1, dfn[x], dfn[x] + sz[x] - 1, k);
}
int QueryTree (int x)
{
return query(1, dfn[x], dfn[x] + sz[x] - 1);
}
void add (int a, int b)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
}
void dfs1 (int x)
{
son[x] = -1;
sz[x] = 1;
for (int i = hd[x]; ~i; i = nxt[i])
if (!d[edg[i]])
{
d[edg[i]] = d[x] + 1;
p[edg[i]] = x;
dfs1(edg[i]);
sz[x] += sz[edg[i]];
if (son[x] == -1 || sz[edg[i]] > sz[son[x]])
son[x] = edg[i];
}
}
void dfs2 (int x, int t)
{
top[x] = t;
dfn[x] = ++stmp;
rnk[stmp] = x;
if (son[x] == -1)
return;
dfs2(son[x], t);
for (int i = hd[x]; ~i; i = nxt[i])
if (edg[i] != son[x] && edg[i] != p[x])
dfs2(edg[i], edg[i]);
}
int main ()
{
cin >> n >> m >> rt >> mod;
for (int i = 1; i <= n; i++)
hd[i] = -1;
for (int i = 1; i <= n; i++)
cin >> w[i];
for (int i = 1, a, b; i < n; i++)
{
cin >> a >> b;
add(a, b);
add(b, a);
}
d[rt] = 1;
dfs1(rt);
dfs2(rt, rt);
build(1, 1, n);
for (int type; m; m--)
{
cin >> type;
if (type == 1)
{
int a, b, c;
cin >> a >> b >> c;
AddPath(a, b, c % mod);
}
else if (type == 2)
{
int a, b;
cin >> a >> b;
cout << QueryPath(a, b) << endl;
}
else if (type == 3)
{
int a, b;
cin >> a >> b;
AddTree(a, b % mod);
}
else
{
int a;
cin >> a;
cout << QueryTree(a) << endl;
}
}
return 0;
}