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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| #include<bits/stdc++.h> using namespace std;
typedef long long ll;
typedef pair<int , int > pii; typedef unsigned long long ull;
namespace FastIO { template<typename T> inline T read(T& x) { x = 0; int f = 1; char ch; while (!isdigit(ch = getchar())) if (ch == '-') f = -1; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x *= f; return x; } template<typename T, typename... Args> inline void read(T& x, Args &...x_) { read(x); read(x_...); return; } inline ll read() { ll x; read(x); return x; } }; using namespace FastIO;
int P;
struct Node { ll sum; Node() { sum = 0; } Node operator + (const Node& b) const { Node ans; ans.sum = (sum + b.sum) % P; return ans; } }; template<const int N> class SegTree { private: Node node[N]; int lc[N], rc[N], cntn; ll lazy[N]; public : void clear(int rt) { if(!rt) return ; lazy[rt] = 0, node[rt] = Node(); clear(lc[rt]); clear(rc[rt]); lc[rt] = rc[rt] = 0; } inline int newNode() { cntn++; lc[cntn] = rc[cntn] = lazy[cntn] = 0; return cntn; } inline void Add(int rt, int l, int r, ll val) { (node[rt].sum += val * (r - l + 1) % P) %= P; (lazy[rt] += val) %= P; } inline void PushDown(int rt, int l, int r) { if(l == r || !lazy[rt]) return ; int mid = (l + r) >> 1; if(!lc[rt]) lc[rt] = newNode(); Add(lc[rt], l, mid, lazy[rt]); if(!rc[rt]) rc[rt] = newNode(); Add(rc[rt], mid + 1, r, lazy[rt]); lazy[rt] = 0; } void Modify(int &rt, int l, int r, int L, int R, ll val) { if(!rt) rt = newNode(); if(L <= l && r <= R) { Add(rt, l, r, val); return; } PushDown(rt, l, r); int mid = (l + r) >> 1; if(L <= mid) Modify(lc[rt], l, mid, L, R, val); if(R > mid) Modify(rc[rt], mid + 1, r, L, R, val); node[rt] = node[lc[rt]] + node[rc[rt]]; } Node Query(int &rt, int l, int r, int L, int R) { if(!rt) rt = newNode(); if(L <= l && r <= R) return node[rt]; PushDown(rt, l, r); int mid = (l + r) >> 1; Node res; res.sum = 0; if(L <= mid) res = res + Query(lc[rt], l, mid, L, R); if(R > mid) res = res + Query(rc[rt], mid + 1, r, L, R); return res; } };
template<int N, int M> class Graph { private : struct Edge { int to, nt, wt; Edge() {} Edge(int to, int nt, int wt) : to(to), nt(nt), wt(wt) {} }e[M]; int hd[N], cnte; public : inline void clear() { memset(hd, 0, sizeof(hd)), cnte = 0; } inline void AddEdge(int u, int v, int w = 0) { e[++cnte] = Edge(v, hd[u], w); hd[u] = cnte; } inline int head(int u) { return hd[u]; } inline int nt(int u) { return e[u].nt; } inline int to(int u) { return e[u].to; } inline int wt(int u) { return e[u].wt; } };
const int MaxV = 1e5 + 10; const int MaxE = 2e5 + 10;
int n, m, r; int a[MaxV]; Graph< MaxV , MaxE >G; SegTree< MaxV << 5 > Tree; int rt;
inline void Input() { read(n, m, r, P); for(int i = 1; i <= n; i++) { read(a[i]); } int u, v; for(int i = 1; i < n; i++) { read(u, v); G.AddEdge(u, v); G.AddEdge(v, u); } }
int fa[MaxV], dep[MaxV]; int sz[MaxV], son[MaxV]; int top[MaxV]; int id[MaxV], rk[MaxV], cntd;
void Dfs(int u, int F) { fa[u] = F, dep[u] = dep[F] + 1, sz[u] = 1; for(int i = G.head(u); i; i = G.nt(i)) { int v = G.to(i); if(v == F) continue; Dfs(v, u); sz[u] += sz[v]; if(sz[v] > sz[son[u]]) son[u] = v; } }
void Dfs2(int u, int t) { id[u] = ++cntd; rk[cntd] = u; top[u] = t; if(!son[u]) return ; Dfs2(son[u], t); for(int i = G.head(u); i; i = G.nt(i)) { int v = G.to(i); if(v != son[u] && v != fa[u]) Dfs2(v, v); } }
inline void ModifyRange(int x, int y, int z) { z %= P; while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); Tree.Modify(rt, 1, n, id[top[x]], id[x], z); x = fa[top[x]]; } if(id[x] > id[y]) swap(x, y); Tree.Modify(rt, 1, n, id[x], id[y], z); }
inline void QueryRange(int x, int y) { int ans = 0; while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); (ans += Tree.Query(rt, 1, n, id[top[x]], id[x]).sum) %= P; x = fa[top[x]]; } if(id[x] > id[y]) swap(x, y); (ans += Tree.Query(rt, 1, n, id[x], id[y]).sum) %= P; printf("%d\n", ans); }
inline void ModifyTree(int x, int z) { Tree.Modify(rt, 1, n, id[x], id[x] + sz[x] - 1, z); }
inline void QueryTree(int x) { printf("%d\n", Tree.Query(rt, 1, n, id[x], id[x] + sz[x] - 1).sum % P); }
inline void Work() { Dfs(r, r); Dfs2(r, r); for(int i = 1; i <= n; i++) { Tree.Modify(rt, 1, n, id[i], id[i], a[i]); } int op, x, y, z; while(m--) { read(op); if(op == 1) { read(x, y, z); ModifyRange(x, y, z); } else if(op == 2) { read(x, y); QueryRange(x, y); } else if(op == 3) { read(x, z); ModifyTree(x, z); } else { read(x); QueryTree(x); } } }
int main() { int T = 1; while(T--) { Input(); Work(); } return 0; }
|