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
| #include<iostream> #include<stdio.h> #include<vector> #include<algorithm> #include<string.h> using namespace std; typedef long long LL; const int MAXN = 100000 + 5; LL P; int aa[MAXN]; int siz[MAXN]; int dep[MAXN]; int top[MAXN]; int fa[MAXN]; int son[MAXN]; int num[MAXN]; int pre[MAXN]; int vis[MAXN]; vector<int>v[MAXN]; LL tre[MAXN << 2]; LL add[MAXN << 2]; int n; LL ans; int tot; void update(int t, int l, int r, int x, int y, LL k); void query(int t, int l, int r, int x, int y, LL ad); int dfs1(int last, int t); void dfs(int t, int ttop); void update_on_tree(int a, int b, LL z); void query_on_tree(int a, int b); int dfs1(int last, int t) { fa[t] = last; dep[t] = dep[last] + 1; siz[t] = 1; vis[t] = 1; int I = -1; int maxson = 0; for (int i = 0; i < v[t].size(); i++) { if (vis[v[t][i]] == 0) { siz[t] += dfs1(t, v[t][i]); if (siz[v[t][i]] > maxson) { I = i; maxson = siz[v[t][i]]; } } } if (I >= 0) son[t] = v[t][I]; else son[t] = -1; return siz[t]; } void dfs(int t, int ttop) { top[t] = ttop; vis[t] = 1; num[t] = tot; pre[tot] = t; tot++; if (son[t] > 0) dfs(son[t], ttop); for (int i = 0; i < v[t].size(); i++) { if (vis[v[t][i]] == 0) { if (v[t][i] == son[t]) continue; dfs(v[t][i], v[t][i]); } } } void update(int t, int l, int r, int x, int y, LL k) { tre[t] += (min(r, y) - max(l, x) + 1)*k; if (l >= x && r <= y) { add[t] += k; return; } int mid = l + r >> 1; if (y <= mid) { update(t << 1, l, mid, x, y, k); } else if (x > mid) { update(t << 1 | 1, mid + 1, r, x, y, k); } else { update(t << 1, l, mid, x, y, k); update(t << 1 | 1, mid + 1, r, x, y, k); } } void query(int t, int l, int r, int x, int y, LL ad) { if (l >= x && r <= y) { ans += tre[t]; ans += ad * (r - l + 1); return; } int mid = l + r >> 1; if (y <= mid) { query(t << 1, l, mid, x, y, ad + add[t]); } else if (x > mid) { query(t << 1 | 1, mid + 1, r, x, y, ad + add[t]); } else { query(t << 1, l, mid, x, y, ad + add[t]); query(t << 1 | 1, mid + 1, r, x, y, ad + add[t]); } } void update_on_tree(int a, int b, LL z) { if (dep[top[a]] < dep[top[b]]) swap(a, b); if (top[a] == top[b]) { a = num[a]; b = num[b]; if (a > b) swap(a, b); update(1, 1, n, a, b, z); return; } update(1, 1, n, num[top[a]], num[a], z); update_on_tree(fa[top[a]], b, z); } void query_on_tree(int a, int b) { if (dep[top[a]] < dep[top[b]]) swap(a, b); if (top[a] == top[b]) { a = num[a]; b = num[b]; if (a > b) swap(a, b); query(1, 1, n, a, b, 0); return; } query(1, 1, n, num[top[a]], num[a], 0); query_on_tree(fa[top[a]], b); return; } void build(int t, int l, int r) { if (l==r) { tre[t] = aa[pre[l]]; return; } int mid = l + r >> 1; build(t << 1, l, mid); build(t << 1 | 1, mid + 1, r); tre[t] = tre[t << 1] + tre[t << 1 | 1]; } int main() { scanf("%d", &n); int m, R; scanf("%d%d", &m, &R); scanf("%d", &P); for (int i = 1; i <= n; i++) { scanf("%d", &aa[i]); } for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } dep[R] = 1; tot = 1; dfs1(R, R); memset(vis, 0, sizeof(vis)); dfs(R, R); build(1, 1, n); for (int i = 0; i < m; i++) { int Q,x,y,z; ans = 0; scanf("%d", &Q); switch (Q) { case 1: scanf("%d%d%d", &x, &y, &z); update_on_tree(x, y, z); break; case 2: scanf("%d%d", &x, &y); query_on_tree(x, y); printf("%lld\n", ans%P); break; case 3: scanf("%d%d",&x,&z); update(1, 1, n, num[x], num[x] + siz[x] - 1, z); break; case 4: scanf("%d", &x); query(1, 1, n, num[x], num[x] + siz[x] - 1, 0); printf("%lld\n", ans%P); break; default: break; } } }
|