注意事项:无。

代码:

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
int fa[MAXn + 10], dep[MAXn + 10], siz[MAXn + 10], hson[MAXn + 10];
void dfs1(int cur, int f) {
fa[cur] = f;
dep[cur] = dep[f] + 1;
siz[cur] = 1;
int mxsonsiz = 0;
for (int i = head[cur]; i; i = nex[i]) {
if (to[i] == fa[cur]) continue;
dfs1(to[i], cur);
siz[cur] += siz[to[i]];
if (mxsonsiz < siz[to[i]]) {
mxsonsiz = siz[to[i]];
hson[cur] = to[i];
}
}
}
int top[MAXn + 10], cntdfs, nddfs[MAXn + 10], idxdfs[MAXn + 10], bottom[MAXn + 10];
void dfs2(int cur, int tp) {
top[cur] = tp;
nddfs[cur] = ++cntdfs;
idxdfs[cntdfs] = cur;
if (hson[cur]) {
dfs2(hson[cur], top[cur]);
}
for (int i = head[cur]; i; i = nex[i]) {
if (to[i] == fa[cur] || to[i] == hson[cur]) continue;
dfs2(to[i], to[i]);
}
bottom[cur] = cntdfs;
}
int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
y = fa[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
return x;
}
void pathmodify(int x, int y, int v) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
modify(1, nddfs[top[y]], nddfs[y], v);
y = fa[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
modify(1, nddfs[x], nddfs[y], v);
}
int pathquery(int x, int y) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
ans += query(1, nddfs[top[y]], nddfs[y]);
y = fa[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
ans += query(1, nddfs[x], nddfs[y]);
return ans;
}
void subtmodify(int x, int v) {
modify(1, nddfs[x], bottom[x], v);
}
int subtquery(int x) {
return query(1, nddfs[x], bottom[x]);
}

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

代码(该代码不带取模,不能 AC):

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
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXn = 1e5;

template <typename T>
inline void read(T &a) {
char c;for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar());bool f = c == '-';T x = f ? 0 : (c ^ '0');for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) {x = x * 10 + (c ^ '0');}a = f ? -x : x;
}
template <typename T, typename ...Argv>
inline void read(T &a, Argv &...argv) {
read(a), read(argv...);
}

int mod;

#define ls (id << 1)
#define rs (id << 1 | 1)
const int MAXnd = MAXn * 4;
int le[MAXnd + 10], ri[MAXnd + 10], mid[MAXnd + 10], len[MAXnd + 10];
int sum[MAXnd + 10];
int lzadd[MAXnd + 10];
inline void putadd(int id, int v) {
sum[id] += len[id] * v;
lzadd[id] += v;
}
inline void pushdown(int id) {
if (lzadd[id]) {
putadd(ls, lzadd[id]);
putadd(rs, lzadd[id]);
lzadd[id] = 0;
}
}
inline void pushup(int id) {
sum[id] = sum[ls] + sum[rs];
}
void build(int id, int *a, int l, int r) {
le[id] = l;
ri[id] = r;
mid[id] = (l + r) >> 1;
len[id] = r - l + 1;
if (le[id] == ri[id]) {
sum[id] = a[le[id]];
} else {
build(ls, a, le[id], mid[id]);
build(rs, a, mid[id] + 1, ri[id]);
pushup(id);
}
}
void modify(int id, int l, int r, int v) {
if (le[id] >= l && ri[id] <= r) {
putadd(id, v);
} else {
pushdown(id);
if (l <= mid[id]) modify(ls, l, r, v);
if (r > mid[id]) modify(rs, l, r, v);
pushup(id);
}
}
int query(int id, int l, int r) {
if (le[id] >= l && ri[id] <= r) {
return sum[id];
} else {
pushdown(id);
int ans = 0;
if (l <= mid[id]) ans += query(ls, l, r);
if (r > mid[id]) ans += query(rs, l, r);
return ans;
}
}
#undef ls
#undef rs

int head[MAXn + 10], cntnex, nex[MAXn * 2 + 10], to[MAXn * 2 + 10];
inline void connect(int u, int v) {
nex[++cntnex] = head[u];
head[u] = cntnex;
to[cntnex] = v;
}

int fa[MAXn + 10], dep[MAXn + 10], siz[MAXn + 10], hson[MAXn + 10];
void dfs1(int cur, int f) {
fa[cur] = f;
dep[cur] = dep[f] + 1;
siz[cur] = 1;
int mxsonsiz = 0;
for (int i = head[cur]; i; i = nex[i]) {
if (to[i] == fa[cur]) continue;
dfs1(to[i], cur);
siz[cur] += siz[to[i]];
if (mxsonsiz < siz[to[i]]) {
mxsonsiz = siz[to[i]];
hson[cur] = to[i];
}
}
}
int top[MAXn + 10], cntdfs, nddfs[MAXn + 10], idxdfs[MAXn + 10], bottom[MAXn + 10];
void dfs2(int cur, int tp) {
top[cur] = tp;
nddfs[cur] = ++cntdfs;
idxdfs[cntdfs] = cur;
if (hson[cur]) {
dfs2(hson[cur], top[cur]);
}
for (int i = head[cur]; i; i = nex[i]) {
if (to[i] == fa[cur] || to[i] == hson[cur]) continue;
dfs2(to[i], to[i]);
}
bottom[cur] = cntdfs;
}
int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
y = fa[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
return x;
}
void pathmodify(int x, int y, int v) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
modify(1, nddfs[top[y]], nddfs[y], v);
y = fa[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
modify(1, nddfs[x], nddfs[y], v);
}
int pathquery(int x, int y) {
int ans = 0;
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
ans += query(1, nddfs[top[y]], nddfs[y]);
y = fa[top[y]];
}
if (dep[x] > dep[y]) swap(x, y);
ans += query(1, nddfs[x], nddfs[y]);
return ans;
}
void subtmodify(int x, int v) {
modify(1, nddfs[x], bottom[x], v);
}
int subtquery(int x) {
return query(1, nddfs[x], bottom[x]);
}

int n, m, root;
int a[MAXn + 10], aidxdfs[MAXn + 10];

signed main() {
read(n, m, root, mod);
for (int i = 1; i <= n; ++i) {
read(a[i]);
}
for (int i = 1, u, v; i < n; ++i) {
read(u, v);
connect(u, v); connect(v, u);
}
dfs1(root, 0);
dfs2(root, root);
for (int i = 1; i <= n; ++i) {
aidxdfs[i] = a[idxdfs[i]];
}
build(1, aidxdfs, 1, n);
for (int i = 1, opt, x, y, v; i <= m; ++i) {
read(opt);
if (opt == 1) {
read(x, y, v);
pathmodify(x, y, v);
} else if (opt == 2) {
read(x, y);
printf("%lld\n", pathquery(x, y));
} else if (opt == 3) {
read(x, v);
subtmodify(x, v);
} else {
read(x);
printf("%lld\n", subtquery(x));
}
}
return 0;
}