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
| #include<bits/stdc++.h> #define N 112345 using namespace std; inline int read(){ int x=0; int sign=1; register char c=getchar(); while(c>'9'||c<'0'){if(c=='-')sign=-1; c=getchar();} while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0'; c=getchar();} return x*sign; } long long P; int n, m, r;
struct TREE{ vector<int> g[N]; int wght[N]; int fa[N], deep[N], siz[N], hson[N]; int top[N], dfn[N], rnk[N], tot;
void ini(){ for(int i = 1; i <= n; i++) wght[i] = read(); for(int i = 1; i <= n-1; i++){ int a = read(), b = read(); g[a].push_back(b); g[b].push_back(a); } }
void build_tree(int u, int dep){ siz[u] = 1; deep[u] = dep; for(unsigned int i = 0; i < g[u].size(); ++i){ int v = g[u][i]; if (v == fa[u]) continue; fa[v] = u; build_tree(v, dep + 1); siz[u] += siz[v]; if (siz[v] > siz[hson[u]]) hson[u] = v; } }
void tree_decom(int u, int t){ top[u] = t; dfn[u] = ++tot; rnk[tot] = u; if (hson[u]){ tree_decom(hson[u], t); for(unsigned int i = 0; i < g[u].size(); i++){ int v = g[u][i]; if (v == fa[u] || v == hson[u]) continue; tree_decom(v, v); } } } };
struct NODE{ int l, r; long long sum, add; };
struct SEGTREE{ NODE SegTree[N << 2]; long long a[N];
#define lft(u) SegTree[u].l #define rgt(u) SegTree[u].r #define len(u) (rgt(u)-lft(u)+1) #define sum(p) SegTree[p].sum #define add(p) SegTree[p].add #define mid(p) ((lft(p)+rgt(p))>>1) #define lson(p) (p<<1) #define rson(p) (p<<1|1)
void build(int u, int l, int r){ lft(u) = l; rgt(u) = r; add(u) = 0; if (l == r){ sum(u) = a[l]; } else { build(lson(u), l, mid(u)); build(rson(u), mid(u)+1, r); sum(u) = sum(lson(u)) + sum(rson(u)); sum(u) %= P; } }
void spread(int u){ if (!add(u)) return; sum(lson(u)) += add(u) * len(lson(u)); sum(lson(u)) %= P; sum(rson(u)) += add(u) * len(rson(u)); sum(rson(u)) %= P; add(lson(u)) += add(u); add(lson(u)) %= P; add(rson(u)) += add(u); add(rson(u)) %= P; add(u) = 0; }
void change(int u, int l, int r, long long d){ if (l <= lft(u) && rgt(u) <= r) { sum(u) += d * len(u); sum(u) %= P; add(u) += d; add(u) %= P; } else { spread(u); if (l <= mid(u)) change(lson(u), l, r, d); if (mid(u) + 1 <= r) change(rson(u), l, r, d); sum(u) = sum(rson(u)) + sum(lson(u)); sum(u) %= P; } }
long long query(int u, int l, int r){ if (l <= lft(u) && rgt(u) <= r){ return sum(u) % P; } else { spread(u); long long ans = 0; if (l <= mid(u)) {ans += query(lson(u), l, r); ans %= P;} if (mid(u) + 1 <= r) {ans += query(rson(u), l, r); ans %= P;} return ans % P; } } };
struct TREECHAIN{ SEGTREE Sgt; TREE Tree;
#define fa(u) Tree.fa[u] #define deep(u) Tree.deep[u] #define top(u) Tree.top[u] #define dfn(u) Tree.dfn[u] #define rnk(u) Tree.rnk[u] #define btm(u) (Tree.dfn[u]+Tree.siz[u]-1) #define wght(u) Tree.wght[u] #define rnk(u) Tree.rnk[u]
void ini(){ Tree.ini(); Tree.build_tree(r, 1); Tree.tree_decom(r, r); for(int i = 1; i <= n; i++){ Sgt.a[i] = wght(rnk(i)) % P; } Sgt.build(1, 1, n); }
void tree_path_change(int u, int v, long long d){ while(top(u) != top(v)){ if (deep(top(u)) < deep(top(v))) swap(u, v); Sgt.change(1, dfn(top(u)), dfn(u), d); u = fa(top(u)); } if (dfn(u) > dfn(v)) swap(u, v); Sgt.change(1, dfn(u), dfn(v), d); }
long long tree_path_sum(int u, int v){ long long tot = 0; while(top(u) != top(v)){ if (deep(top(u)) < deep(top(v))) swap(u, v); tot += Sgt.query(1, dfn(top(u)), dfn(u)); tot %= P; u = fa(top(u)); } if (dfn(u) > dfn(v)) swap(u, v); tot += Sgt.query(1, dfn(u), dfn(v)); tot %= P; return tot; }
void subtree_change(int u, long long d){ Sgt.change(1, dfn(u), btm(u), d); }
long long subtree_sum(int u){ return Sgt.query(1, dfn(u), btm(u)); }
void work(){ while(m--){ int mode = read();
if (mode == 1){ int u = read(), v = read(); long long d = read(); tree_path_change(u, v, d); } if (mode == 2){ int u = read(), v = read(); printf("%lld\n", tree_path_sum(u, v)); }
if (mode == 3){ int u = read(); long long d = read(); subtree_change(u, d); }
if (mode == 4){ int u = read(); printf("%lld\n", subtree_sum(u)); } } } }Tc;
int main(){ n = read(), m = read(), r = read(), P = read(); Tc.ini(); Tc.work(); }
|