rt 求查错/wtcl
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define lc(p) (p << 1)
#define rc(p) ((p << 1) + 1)
int n, Q, rt, mod;
vector<int> v[MAXN];
int fa[MAXN], siz[MAXN], son[MAXN], dep[MAXN];
int id[MAXN], w[MAXN], wt[MAXN], top[MAXN];
int cnt;
inline void add(int x, int y)
{
v[x].push_back(y);
v[y].push_back(x);
}
struct SegmentTree {
int l, r;
int sum, add;
} tr[MAXN << 2];
void build(int p, int l, int r)
{
tr[p].l = l, tr[p].r = r;
if (l == r) {
tr[p].sum = w[l];
return;
}
int mid = (l + r) >> 1;
build(lc(p), l, mid);
build(rc(p), mid + 1, r);
tr[p].sum = (tr[lc(p)].sum + tr[rc(p)].sum) % mod;
}
void spr(int p)
{
if (tr[p].add) {
tr[lc(p)].sum += tr[p].add * (tr[lc(p)].r - tr[lc(p)].l + 1);
tr[rc(p)].sum += tr[p].add * (tr[rc(p)].r - tr[rc(p)].l + 1);
tr[lc(p)].add += tr[p].add;
tr[rc(p)].add += tr[p].add;
tr[p].add = 0;
}
}
void update(int p, int l, int r, int d)
{
if (l <= tr[p].l && r >= tr[p].r) {
tr[p].sum += d * (tr[p].r - tr[p].l + 1);
tr[p].add += d;
return;
}
spr(p);
int mid = (tr[p].l + tr[p].r) >> 1;
if (l <= mid)
update(lc(p), l, r, d);
if (r > mid)
update(rc(p), l, r, d);
tr[p].sum = (tr[lc(p)].sum + tr[rc(p)].sum) % mod;
}
int query(int p, int l, int r)
{
if (l <= tr[p].l && r >= tr[p].r)
return tr[p].sum;
spr(p);
int mid = (tr[p].l + tr[p].r) >> 1;
int ans = 0;
if (l <= mid)
ans += query(lc(p), l, r);
if (r > mid)
ans += query(rc(p), l, r);
return ans;
}
void dfs1(int p, int f, int depth)
{
dep[p] = depth;
fa[p] = f;
siz[p] = 1;
int maxson = -1;
for (vector<int>::iterator it = v[p].begin(); it != v[p].end(); ++it) {
int to = *it;
if (to == f)
continue;
dfs1(to, p, depth + 1);
siz[p] += siz[to];
if (siz[to] > maxson) {
maxson = siz[to];
son[p] = to;
}
}
}
void dfs2(int p, int linktop)
{
id[p] = ++cnt;
top[p] = linktop;
wt[cnt] = w[p];
if (!son[p])
return;
dfs2(son[p], linktop);
for (vector<int>::iterator it = v[p].begin(); it != v[p].end(); ++it) {
int to = *it;
if (to == fa[p] || to == son[p])
continue;
dfs2(to, to);
}
}
int range_query(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, id[top[x]], id[x]);
ans %= mod;
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
ans += query(1, id[x], id[y]);
return ans % mod;
}
int son_query(int p)
{
return query(1, id[p], id[p] + siz[p] - 1) % mod;
}
void range_update(int x, int y, int k)
{
k %= mod;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])
swap(x, y);
update(1, id[top[x]], id[x], k);
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
update(1, id[x], id[y], k);
}
void son_update(int x, int k)
{
k %= mod;
update(1, id[x], id[x] + siz[x] - 1, k);
}
signed main()
{
scanf("%d%d%d%d", &n, &Q, &rt, &mod);
register int i;
for (i = 1; i <= n; ++i)
scanf("%d", &w[i]);
int x, y;
for (i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
add(x, y);
}
dfs1(rt, 0, 1);
dfs2(rt, rt);
build(1, 1, n);
int op, z;
while (Q--) {
scanf("%d%d", &op, &x);
if (op == 1) {
scanf("%d%d", &y, &z);
range_update(x, y, z);
} else if (op == 2) {
scanf("%d", &y);
printf("%d\n", range_query(x, y));
} else if (op == 3) {
scanf("%d", &z);
son_update(x, z);
} else if (op == 4) {
printf("%d\n", son_query(x));
}
}
return 0;
}