rt,样例不知道为啥没过,照着教程看了好几遍也没发现错误,有巨巨能帮我调一下吗/kel
#include <iostream>
#include <vector>
using namespace std;
using LL = long long;
const int kN = 1e5 + 1;
int n, m, r, c, o, x, y;
LL p, z;
struct E {
int d, s, h, t, i, f;
vector<int> n;
LL v[2];
} e[kN];
struct T {
struct E {
LL s, t;
} a[kN << 2];
int L(int x) {
return x << 1;
}
int R(int x) {
return L(x) | 1;
}
void PushUp(int x) {
a[x].s = (a[L(x)].s + a[R(x)].s) % p;
}
void PushDown(int x, int l, int r) {
int m = l + r >> 1;
a[L(x)] = {(a[L(x)].s + a[x].t * (m - l + 1)) % p, (a[L(x)].t + a[x].t) % p};
a[R(x)] = {(a[R(x)].s + a[x].t * (r - m)) % p, (a[R(x)].t + a[x].t) % p};
}
void Build(int x, int l, int r) {
if (l == r) {
a[x].s = e[l].v[1];
return;
}
int m = l + r >> 1;
Build(L(x), l, m), Build(R(x), m + 1, r), PushUp(x);
}
void Update(int ul, int ur, int x, int l, int r, LL v) {
if (max(ul, l) > min(ur, r)) {
return;
}
if (ul <= l && r <= ur) {
a[x] = {(a[x].s + v * (r - l + 1)) % p, (a[x].t + v) % p};
return;
}
PushDown(x, l, r);
int m = l + r >> 1;
Update(ul, ur, L(x), l, m, v), Update(ul, ur, R(x), m + 1, r, v);
PushUp(x);
}
LL Query(int ql, int qr, int x, int l, int r) {
if (max(ql, l) > min(qr, r)) {
return 0;
}
if (ql <= l && r <= qr) {
return a[x].s;
}
PushDown(x, l, r);
int m = l + r >> 1;
return (Query(ql, qr, L(x), l, m) + Query(ql, qr, R(x), m + 1, r)) % p;
}
} t;
void D(int x, int f, int d) {
e[x].d = d, e[x].s = 1, e[x].f = f;
for (int i : e[x].n) {
if (i != f) {
D(i, x, d + 1), e[x].s += e[i].s;
if (e[e[x].h].s <= e[i].s) {
e[x].h = i;
}
}
}
}
void G(int x, int t) {
if (!x) {
return;
}
e[x].i = ++c, e[c].v[1] = e[x].v[0] % p, e[x].t = t;
G(e[x].h, t);
for (int i : e[x].n) {
if (i != e[x].f && i != e[x].h) {
G(i, i);
}
}
}
int main() {
cin >> n >> m >> r >> p;
for (int i = 1; i <= n; ++i) {
cin >> e[i].v[0];
}
for (int i = 1, x, y; i < n; ++i) {
cin >> x >> y;
e[x].n.push_back(y), e[y].n.push_back(x);
}
D(r, 0, 1), G(r, r);
t.Build(1, 1, n);
while (m--) {
cin >> o >> x;
if (o == 1) {
cin >> y >> z;
while (e[x].t != e[y].t) {
if (e[e[x].t].d < e[e[y].t].d) {
swap(x, y);
}
t.Update(e[e[x].t].i, e[x].i, 1, 1, n, z % p);
x = e[e[x].t].f;
}
if (e[x].d > e[y].d) {
swap(x, y);
}
t.Update(e[x].i, e[y].i, 1, 1, n, z % p);
} else if (o == 2) {
cin >> y;
LL s = 0;
while (e[x].t != e[y].t) {
if (e[e[x].t].d < e[e[y].t].d) {
swap(x, y);
}
s = (s + t.Query(e[e[x].t].i, e[x].i, 1, 1, n)) % p;
x = e[e[x].t].f;
}
if (e[x].d > e[y].d) {
swap(x, y);
}
cout << (s + t.Query(e[x].i, e[y].i, 1, 1, n)) % p << endl;
} else if (o == 3) {
cin >> z;
t.Update(e[x].i, e[x].i + e[x].s - 1, 1, 1, n, z % p);
} else {
cout << t.Query(e[x].i, e[x].i + e[x].s - 1, 1, 1, n) << endl;
}
}
return 0;
}