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 250 251 252 253 254 255 256 257
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
#ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const {return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;} int operator == (const typemod<mod> &x) const {return x.z == z;} int operator != (const typemod<mod> &x) const {return x.z != z;} }; typedef typemod<mod> Tm; #endif
const int maxn = 5e4 + 5; const int maxm = maxn << 1;
int head[maxn], cnt; struct Edge { int to, next; }edg[maxn << 1]; void add(int u,int v) { edg[++ cnt] = (Edge) { v, head[u] }, head[u] = cnt; }
int n, m; int son[maxn], fa[maxn], top[maxn], siz[maxn];
void dfs1(int p,int pre) { siz[p] = 1; for(int i = head[p];i;i = edg[i].next) { int to = edg[i].to; if(to == pre) continue; dfs1(to, p); siz[p] += siz[to]; if(siz[to] > siz[son[p]]) son[p] = to; } } int dfn[maxn], dfntot, fdfn[maxn], dep[maxn]; int v[maxn], a[maxn]; void dfs2(int p,int pre,int topf) { dep[p] = dep[pre] + 1; dfn[p] = ++ dfntot; fdfn[dfntot] = p; fa[p] = pre; top[p] = topf; if(son[p]) dfs2(son[p], p, topf); else return ; for(int i = head[p];i;i = edg[i].next) { int to = edg[i].to; if(to == pre || to == son[p]) continue; dfs2(to, p, to); } }
int gcd(int a,int b) { return b == 0 ? a : gcd(b, a % b); }
struct Node { int gd, val; Node(void) {gd = val = 0;} }t[maxn << 2];
struct Seg { #define ls (p << 1) #define rs (p << 1 | 1) #define mid ((l + r) >> 1)
void pushup(int p) { t[p].gd = gcd(t[ls].gd, t[rs].gd); }
void build(int p,int l,int r) { if(l == r) { if(top[fdfn[l]] == fdfn[l]) return t[p].gd = v[fdfn[l]], void(); else return t[p].gd = v[fa[fdfn[l]]] - v[fdfn[l]], void(); } build(ls, l, mid), build(rs, mid + 1, r); pushup(p); }
void ChangeGcd(int p,int l,int r,int pos,int c) { if(l == r) return t[p].gd += c, void(); if(pos <= mid) ChangeGcd(ls, l, mid, pos, c); else ChangeGcd(rs, mid + 1, r, pos, c); pushup(p); }
void ChangeVal(int p,int l,int r,int ll,int rr, int c) { assert(ll <= rr); if(ll <= l && r <= rr) return t[p].val += c, void(); if(ll <= mid) ChangeVal(ls, l, mid, ll, rr, c); if(mid < rr) ChangeVal(rs, mid + 1, r, ll, rr, c); }
int AskVal(int p,int l,int r,int pos) { if(l == r) return v[fdfn[l]] + t[p].val; if(pos <= mid) return AskVal(ls, l, mid, pos) + t[p].val; else return AskVal(rs, mid + 1, r, pos) + t[p].val; }
int AskGcd(int p,int l,int r,int ll,int rr) { if(ll > rr) return 0; if(ll <= l && r <= rr) return t[p].gd; int res(0); if(ll <= mid) res = AskGcd(ls, l, mid, ll, rr); if(mid < rr) res = gcd(res, AskGcd(rs, mid + 1, r, ll, rr)); return res; }
}T;
void ChangeList(int u,int v,int c) { if(u == v) { if(u != top[u]) T.ChangeGcd(1, 1, n, dfn[u], - c); else T.ChangeGcd(1, 1, n, dfn[u], c);
if(son[u]) T.ChangeGcd(1, 1, n, dfn[son[u]], c);
T.ChangeVal(1, 1, n, dfn[u], dfn[u], c); return ; }
while(top[u] != top[v]) { if(dep[top[u]] < dep[top[v]]) swap(u, v); T.ChangeVal(1, 1, n, dfn[top[u]], dfn[u], c); T.ChangeGcd(1, 1, n, dfn[top[u]], c); if(son[u]) T.ChangeGcd(1, 1, n, dfn[son[u]], c); u = fa[top[u]]; }
if(dep[u] < dep[v]) swap(u, v); T.ChangeVal(1, 1, n, dfn[v], dfn[u], c);
if(v != top[v]) T.ChangeGcd(1, 1, n, dfn[v], - c); else T.ChangeGcd(1, 1, n, dfn[v], c); if(son[u] != 0) T.ChangeGcd(1, 1, n, dfn[son[u]], c); }
int AskList(int u,int v) { if(u == v) return T.AskVal(1, 1, n, dfn[u]); int res(0); while(top[u] != top[v]) { if(dep[top[u]] < dep[top[v]]) swap(u, v); res = gcd(res, T.AskGcd(1, 1, n, dfn[top[u]], dfn[u])); u = fa[top[u]]; } if(dep[u] < dep[v]) swap(u, v); if(u != v) res = gcd(res, T.AskGcd(1, 1, n, dfn[v] + 1, dfn[u])); res = gcd(res, T.AskVal(1, 1, n, dfn[v])); return res; }
signed main() {
int i, j; r1(n); for(i = 1; i < n; ++ i) { int u, v; r1(u, v); ++ u, ++ v; add(u, v), add(v, u); } for(i = 1; i <= n; ++ i) r1(v[i]); dfs1(1, 0); dfs2(1, 0, 1); T.build(1, 1, n); int Q; r1(Q); while(Q --) { char s[10]; int u, v, d; scanf("%s", s + 1); r1(u, v); ++ u, ++ v; if(s[1] == 'F') { printf("%d\n", abs(AskList(u, v))); } else { r1(d); ChangeList(u, v, d); } } return 0; }
|