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
|
#include <bits/stdc++.h>
using namespace std;
#define DEBUG #define lc u << 1 #define rc u << 1 | 1 #define File(a) freopen(a".in", "r", stdin); freopen(a".out", "w", stdout)
typedef long long LL; typedef pair<int, int> PII;
const int N = 100010, M = N << 1; const int INF = 0x3f3f3f3f;
int n, m, root, mod; int h[N], e[M], ne[M], idx; int a[N], val[N]; int depth[N], fa[N], sz[N], hson[N]; int dfn[N], timestamp; int top[N], id[N];
struct Tree { struct Node { int l, r, sum, tag; inline int len() {return r - l + 1; } } tr[N << 2]; void pushup(int u) { tr[u].sum = (tr[lc].sum + tr[rc].sum) % mod; } void build(int u, int l, int r) { tr[u].l = l, tr[u].r = r; if (l == r) return tr[u].sum = val[l], void(0); int mid = l + r >> 1; build(lc, l, mid), build(rc, mid + 1, r); pushup(u); } void pushdown(int u) { if (!tr[u].tag) return; tr[lc].sum = (tr[lc].sum + tr[u].tag * tr[lc].len()) % mod; tr[rc].sum = (tr[rc].sum + tr[u].tag * tr[rc].len()) % mod; tr[lc].tag += tr[u].tag, tr[rc].tag += tr[u].tag; tr[u].tag = 0; } void modify(int u, int l, int r, int k) { if (l <= tr[u].l && tr[u].r <= r) { tr[u].sum = (tr[u].sum + tr[u].len() * k) % mod; tr[u].tag += k; return; } pushdown(u); int mid = tr[u].l + tr[u].r >> 1; if (l <= mid) modify(lc, l, r, k); if (r > mid) modify(rc, l, r, k); pushup(u); } int query(int u, int l, int r) { if (l <= tr[u].l && tr[u].r <= r) return tr[u].sum; pushdown(u); int mid = tr[u].l + tr[u].r >> 1; int res = 0; if (l <= mid) res = (res + query(lc, l, r)) % mod; if (r > mid) res = (res + query(rc, l, r)) % mod; return res; } } SGT;
inline void add(int a, int b) { e[++ idx] = b, ne[idx] = h[a], h[a] = idx; }
void dfs1(int ver, int pre, int deep) { depth[ver] = deep, fa[ver] = pre, sz[ver] = 1; int maxn = -1; for (int i = h[ver]; ~i; i = ne[i]) { int j = e[i]; if (j == pre) continue; dfs1(j, ver, deep + 1); sz[ver] += sz[j]; if (maxn == -1 || maxn < sz[j]) maxn = sz[j], hson[ver] = j; } }
void dfs2(int ver, int topf) { dfn[ver] = ++ timestamp, val[timestamp] = a[ver], top[ver] = topf; if (!hson[ver]) return; dfs2(hson[ver], topf); for (int i = h[ver]; ~i; i = ne[i]) { int j = e[i]; if (j == fa[ver] || j == hson[ver]) continue; dfs2(j, j); } }
void modify_range(int x, int y, int k) { while (top[x] != top[y]) { if (depth[top[x]] < depth[top[y]]) swap(x, y); SGT.modify(1, dfn[top[x]], dfn[x], k); x = fa[top[x]]; } if (depth[x] > depth[y]) swap(x, y); SGT.modify(1, dfn[x], dfn[y], k); }
int query_range(int x, int y) { int res = 0; while (top[x] != top[y]) { if (depth[top[x]] < depth[top[y]]) swap(x, y); res = (res + SGT.query(1, dfn[top[x]], dfn[x])) % mod; x = fa[top[x]]; } if (depth[x] > depth[y]) swap(x, y); res = (res + SGT.query(1, dfn[x], dfn[y])) % mod; return res; }
void modify_subtree(int x, int k) { SGT.modify(1, dfn[x], dfn[x] + sz[x] - 1, k); }
int query_subtree(int x) { return SGT.query(1, dfn[x], dfn[x] + sz[x] - 1); }
signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); memset(h, -1, sizeof h); cin >> n >> m >> root >> mod; for (int i = 1; i <= n; i ++) cin >> a[i]; for (int i = 1; i < n; i ++) { int u, v; cin >> u >> v; add(u, v), add(v, u); } dfs1(root, 0, 1); dfs2(root, root); SGT.build(1, 1, n); while (m --) { int opt; cin >> opt; if (opt == 1) { int x, y, z; cin >> x >> y >> z; modify_range(x, y, z); } else if (opt == 2) { int x, y; cin >> x >> y; cout << query_range(x, y) % mod << '\n'; } else if (opt == 3) { int x, y; cin >> x >> y; modify_subtree(x, y); } else { int x; cin >> x; cout << query_subtree(x) % mod << '\n'; } } return 0; }
|