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
| #include <bits/stdc++.h> using namespace std;
constexpr int MXV = 100005; int val[MXV]; int DVS; int root, V, opts;
int to[MXV << 1], nxt[MXV << 1], head[MXV], egsz; void addedge(int a, int b) { to[egsz] = b; nxt[egsz] = head[a]; head[a] = egsz++; }
int siz[MXV], depth[MXV], fa[MXV], son[MXV]; void init1(int x, int f, int h) { siz[x] = 1; depth[x] = h; fa[x] = f; son[x] = -1; for (int i(head[x]); ~i; i = nxt[i]) { if (to[i] != f) { init1(to[i], x, h + 1); siz[x] += siz[to[i]]; if (!~son[x] || siz[to[i]] > siz[son[x]]) son[x] = to[i]; } } }
int anc[MXV], ptoi[MXV], itop[MXV], dfn; void init2(int x, int a) { anc[x] = a; ptoi[x] = dfn, itop[dfn++] = x; if (~son[x]) { init2(son[x], a); for (int i(head[x]); ~i; i = nxt[i]) if (to[i] != fa[x] && to[i] != son[x]) init2(to[i], to[i]); } }
int sum[MXV << 2], tag[MXV << 2]; void init3(int cur, int l, int r) { if (r - l == 1) { sum[cur] = val[itop[l]] % DVS; return; } int mid((l + r) >> 1); init3((cur << 1) + 1, l, mid); init3((cur + 1) << 1, mid, r); sum[cur] = (sum[(cur << 1) + 1] + sum[(cur + 1) << 1]) % DVS; }
inline void pushdown(int p, int l, int r) { int ls((p << 1) + 1), rs((p + 1) << 1), mid((r + l) >> 1); tag[ls] = (tag[ls] + tag[p]) % DVS; sum[ls] = (sum[ls] + (mid - l) % DVS * tag[p] % DVS) % DVS; tag[rs] = (tag[rs] + tag[p]) % DVS; sum[rs] = (sum[rs] + (r - mid) % DVS * tag[p] % DVS) % DVS; tag[p] = 0; }
inline void pushup(int p) { sum[p] = (sum[(p << 1) + 1] + sum[(p + 1) << 1]) % DVS; }
void add(int cur, int l, int r, int curl, int curr, int k) { if (curr <= l || curl >= r) return; if (curl >= l && curr <= r) { tag[cur] = (tag[cur] + k) % DVS; sum[cur] = (sum[cur] + (curr - curl) % DVS * k % DVS) % DVS; return; } pushdown(cur, curl, curr); add((cur << 1) + 1, l, r, curl, (curl + curr) >> 1, k); add((cur + 1) << 1, l, r, (curl + curr) >> 1, curr, k); pushup(cur); }
int ask(int cur, int l, int r, int curl, int curr) { if (curr <= l || curl >= r) return 0; if (curl >= l && curr <= r) return sum[cur]; pushdown(cur, curl, curr); return (ask((cur << 1) + 1, l, r, curl, (curl + curr) >> 1) + ask((cur + 1) << 1, l, r, (curl + curr) >> 1, curr)) % DVS; }
int read() { int x(0); char c(getchar()); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x; }
void write(int x) { if (x >= 10) write(x / 10); putchar((x % 10) | 48); }
int main() { V = read(), opts = read(), root = read() - 1, DVS = read(); fill(head, head + V, -1); for (int i(0); i != V; ++i) cin >> val[i]; for (int i(1), a, b; i != V; ++i) { a = read() - 1, b = read() - 1; addedge(a, b), addedge(b, a); } init1(root, -1, 0); init2(root, root); init3(0, 0, V); for (int opt, x, y, z; opts--;) { opt = read(), x = read() - 1; switch (opt) { case 1: { y = read() - 1; z = read() % DVS; while (anc[x] != anc[y]) { if (depth[anc[x]] < depth[anc[y]]) swap(x, y); add(0, ptoi[anc[x]], ptoi[x] + 1, 0, V, z); x = fa[anc[x]]; } if (depth[x] < depth[y]) swap(x, y); add(0, ptoi[y], ptoi[x] + 1, 0, V, z); break; } case 2: { y = read() - 1; int sum(0); while (anc[x] != anc[y]) { if (depth[anc[x]] < depth[anc[y]]) swap(x, y); sum = (sum + ask(0, ptoi[anc[x]], ptoi[x] + 1, 0, V)) % DVS; x = fa[anc[x]]; } if (depth[x] < depth[y]) swap(x, y); write((sum + ask(0, ptoi[y], ptoi[x] + 1, 0, V)) % DVS), putchar('\n'); break; } case 3: { z = read() % DVS; add(0, ptoi[x], ptoi[x] + siz[x], 0, V, z); break; } case 4: { write(ask(0, ptoi[x], ptoi[x] + siz[x], 0, V)), putchar('\n'); break; } } } return 0; }
|