#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef LOCAL # include "debug_print.hpp" # define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define debug(...) (static_cast(0)) #endif using namespace std; namespace io { template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template istream &operator>>(istream &is, vector &v) { for (auto &x : v) is >> x; return is; } template istream &operator>>(istream &is, array &v) { for (auto &x : v) is >> x; return is; } template istream& cin_tuple_impl(istream &is, T &t) { if constexpr (N < std::tuple_size::value) { auto &x = std::get(t); is >> x; cin_tuple_impl(is, t); } return is; } template istream &operator>>(istream &is, tuple &t) { return cin_tuple_impl(is, t); } template ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template ostream &operator<<(ostream &os, const vector &v) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template ostream &operator<<(ostream &os, const array &v) { size_t n = v.size(); for (size_t i = 0; i < n; i++) { if (i) os << " "; os << v[i]; } return os; } template ostream& cout_tuple_impl(ostream &os, const T &t) { if constexpr (N < std::tuple_size::value) { if constexpr (N > 0) os << " "; const auto &x = std::get(t); os << x; cout_tuple_impl(os, t); } return os; } template ostream &operator<<(ostream &os, const tuple &t) { return cout_tuple_impl(os, t); } void in() {} template void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } void outr() {} template void outr(const T &t, const U &...u) { cout << t; outr(u...); } void __attribute__((constructor)) _c() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } // namespace io using io::in; using io::out; using io::outr; using ll = long long; using D = double; using LD = long double; using P = pair; using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using i128 = __int128; using u128 = unsigned __int128; using vi = vector; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using vvvvvc = vector>; #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__VA_ARGS__)))) template using PQ = priority_queue>; template using minPQ = priority_queue, greater>; #define rep1(a) for(ll i = 0; i < a; i++) #define rep2(i, a) for(ll i = 0; i < a; i++) #define rep3(i, a, b) for(ll i = a; i < b; i++) #define rep4(i, a, b, c) for(ll i = a; i < b; i += c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rrep1(a) for(ll i = (a)-1; i >= 0; i--) #define rrep2(i, a) for(ll i = (a)-1; i >= 0; i--) #define rrep3(i, a, b) for(ll i = (b)-1; i >= a; i--) #define rrep4(i, a, b, c) for(ll i = (b)-1; i >= a; i -= c) #define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__) #define for_subset(t, s) for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s))) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) #define SZ(v) ll(v.size()) #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define LB(c, x) distance((c).begin(), lower_bound(ALL(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(ALL(c), (x))) template T SUM(const vector &v) { T res = 0; for(auto &&a : v) res += a; return res; } template vector> RLE(const vector &v) { if (v.empty()) return {}; T cur = v.front(); int cnt = 1; vector> res; for (int i = 1; i < (int)v.size(); i++) { if (cur == v[i]) cnt++; else { res.emplace_back(cur, cnt); cnt = 1; cur = v[i]; } } res.emplace_back(cur, cnt); return res; } template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, true : false); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, true : false); } void YESNO(bool flag) { out(flag ? "YES" : "NO"); } void yesno(bool flag) { out(flag ? "Yes" : "No"); } int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } int popcnt_sgn(int x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(ll x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(u64 x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int highbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int highbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int highbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int highbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template T get_bit(T x, int k) { return x >> k & 1; } template T set_bit(T x, int k) { return x | T(1) << k; } template T reset_bit(T x, int k) { return x & ~(T(1) << k); } template T flip_bit(T x, int k) { return x ^ T(1) << k; } template T popf(deque &que) { T a = que.front(); que.pop_front(); return a; } template T popb(deque &que) { T a = que.back(); que.pop_back(); return a; } template T pop(queue &que) { T a = que.front(); que.pop(); return a; } template T pop(stack &que) { T a = que.top(); que.pop(); return a; } template T pop(PQ &que) { T a = que.top(); que.pop(); return a; } template T pop(minPQ &que) { T a = que.top(); que.pop(); return a; } template ll binary_search(F check, ll ok, ll ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } return ok; } template ll binary_search_real(F check, double ok, double ng, int iter = 60) { for (int _ = 0; _ < iter; _++) { double mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } return (ok + ng) / 2; } // max x s.t. b*x <= a ll div_floor(ll a, ll b) { assert(b != 0); if (b < 0) a = -a, b = -b; return a / b - (a % b < 0); } // max x s.t. b*x < a ll div_under(ll a, ll b) { assert(b != 0); if (b < 0) a = -a, b = -b; return a / b - (a % b <= 0); } // min x s.t. b*x >= a ll div_ceil(ll a, ll b) { assert(b != 0); if (b < 0) a = -a, b = -b; return a / b + (a % b > 0); } // min x s.t. b*x > a ll div_over(ll a, ll b) { assert(b != 0); if (b < 0) a = -a, b = -b; return a / b + (a % b >= 0); } // x = a mod b (b > 0), 0 <= x < b ll modulo(ll a, ll b) { assert(b > 0); ll c = a % b; return c < 0 ? c + b : c; } // (q,r) s.t. a = b*q + r, 0 <= r < b (b > 0) // div_floor(a,b), modulo(a,b) pair divmod(ll a, ll b) { ll q = div_floor(a,b); return {q, a - b*q}; } template struct LazyMontgomeryModInt { using mint = LazyMontgomeryModInt; using i32 = int32_t; using u32 = uint32_t; using u64 = uint64_t; static constexpr u32 get_r() { u32 ret = mod; for (i32 i = 0; i < 4; ++i) ret *= 2 - mod * ret; return ret; } static constexpr u32 r = get_r(); static constexpr u32 n2 = -u64(mod) % mod; static_assert(mod < (1 << 30), "invalid, mod >= 2 ^ 30"); static_assert((mod & 1) == 1, "invalid, mod % 2 == 0"); static_assert(r * mod == 1, "this code has bugs."); u32 a; constexpr LazyMontgomeryModInt() : a(0) {} constexpr LazyMontgomeryModInt(const int64_t &b) : a(reduce(u64(b % mod + mod) * n2)){}; static constexpr u32 reduce(const u64 &b) { return (b + u64(u32(b) * u32(-r)) * mod) >> 32; } constexpr mint &operator+=(const mint &b) { if (i32(a += b.a - 2 * mod) < 0) a += 2 * mod; return *this; } constexpr mint &operator-=(const mint &b) { if (i32(a -= b.a) < 0) a += 2 * mod; return *this; } constexpr mint &operator*=(const mint &b) { a = reduce(u64(a) * b.a); return *this; } constexpr mint &operator/=(const mint &b) { *this *= b.inverse(); return *this; } constexpr mint operator+(const mint &b) const { return mint(*this) += b; } constexpr mint operator-(const mint &b) const { return mint(*this) -= b; } constexpr mint operator*(const mint &b) const { return mint(*this) *= b; } constexpr mint operator/(const mint &b) const { return mint(*this) /= b; } constexpr bool operator==(const mint &b) const { return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a); } constexpr bool operator!=(const mint &b) const { return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a); } constexpr mint operator-() const { return mint() - mint(*this); } constexpr mint operator+() const { return mint(*this); } constexpr mint pow(u64 n) const { mint ret(1), mul(*this); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } constexpr mint inverse() const { int x = get(), y = mod, u = 1, v = 0, t = 0, tmp = 0; while (y > 0) { t = x / y; x -= t * y, u -= t * v; tmp = x, x = y, y = tmp; tmp = u, u = v, v = tmp; } return mint{u}; } friend ostream &operator<<(ostream &os, const mint &b) { return os << b.get(); } friend istream &operator>>(istream &is, mint &b) { int64_t t; is >> t; b = LazyMontgomeryModInt(t); return (is); } constexpr u32 get() const { u32 ret = reduce(a); return ret >= mod ? ret - mod : ret; } static constexpr u32 get_mod() { return mod; } }; // const int mod = 998244353; const int mod = 1000000007; using mint = LazyMontgomeryModInt; #define ZERO 0 #define ONE 1 #define UPD(c, a, b) (c) += (a) * (b) template vector> matrix_mul(const vector> &a, const vector> &b, int aR = -1, int bR = -1, int bC = -1) { assert(a.size() > 0 and b.size() > 0); if (aR == -1) aR = a.size(), bR = b.size(), bC = b[0].size(); assert(bR == (int)a[0].size()); vector> c(aR, vector(bC, ZERO)); for (int i = 0; i < aR; i++) for (int k = 0; k < bC; k++) { for (int j = 0; j < bR; j++) UPD(c[i][k], a[i][j], b[j][k]); } return c; } template vector> matrix_pow(vector> a, long long k) { int n = a.size(); vector> b(n, vector (n, ZERO)); for (int i = 0; i < n; i++) b[i][i] = ONE; while (k) { if (k & 1) b = matrix_mul(b, a); k >>= 1; if (k) a = matrix_mul(a, a); } return b; } template array, N> matrix_mul(const array, N> &a, const array, N> &b) { array, N> c{}; for (int i = 0; i < N; i++) for (int k = 0; k < N; k++) { T sum = ZERO; for (int j = 0; j < N; j++) UPD(sum, a[i][j], b[j][k]); c[i][k] = sum; } return c; } template array, N> matrix_pow(array, N> a, long long k) { array, N> b{}; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) b[i][j] = ZERO; for (int i = 0; i < N; i++) b[i][i] = ONE; while (k) { if (k & 1) b = matrix_mul(b, a); k >>= 1; if (k) a = matrix_mul(a, a); } return b; } #undef ZERO #undef ONE #undef UPD using mat = array,2>; struct HeavyLightDecomposition { vector> g; vector head, tin, tout, depth, par; HeavyLightDecomposition(int n_): g(n_), head(n_), tin(n_), tout(n_), depth(n_), par(n_) {} void add_edge(int u, int v) { g[u].push_back(v); g[v].push_back(u); } int dfs_sz(int u, int p, int d) { depth[u] = d; int sz_u = 1; int max_sz_ch = 0; for (int &v : g[u]) if (v != p) { par[v] = u; int sz_v = dfs_sz(v, u, d+1); sz_u += sz_v; if (max_sz_ch < sz_v) { max_sz_ch = sz_v; swap(g[u].front(), v); } } return sz_u; } void dfs_hld(int u, int p, int &k) { tin[u] = k++; for (int v : g[u]) if (v != p) { head[v] = (v == g[u].front() ? head[u] : v); dfs_hld(v, u, k); } tout[u] = k; } void build() { dfs_sz(0, -1, 0); int k = 0; /* head[0] = 0; */ dfs_hld(0, -1, k); } int lca(int u, int v) { while (head[u] != head[v]) { if (depth[head[u]] < depth[head[v]]) v = par[head[v]]; else u = par[head[u]]; } return depth[u] < depth[v] ? u : v; } int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; } /* * path (u to v) -> path_u, path_v (closed intervals) * path_u: u to lca(u,v) * path_v: lca(u,v) to v * path (u to v) = path_u ++ rev(path_v) * どちらも 区間 [l,r] は l < r の順 (深さ l < r) * path_u では euler tour の逆方向に計算する <= 非可換の時注意 * path_v では lca(u,v) to v の順に計算 <= 非可換の時注意 * exclude_lca = true の時 path に lca は含まれない <= 辺に重みがある時に使う */ pair>, vector>> get_path(int u, int v, bool exclude_lca = false) { vector> path_u, path_v; while (head[u] != head[v]) { if (depth[head[u]] < depth[head[v]]) { path_v.emplace_back(tin[head[v]], tin[v]); v = par[head[v]]; } else { path_u.emplace_back(tin[head[u]], tin[u]); u = par[head[u]]; } } if (depth[u] < depth[v]) path_v.emplace_back(tin[u] + (exclude_lca ? 1 : 0), tin[v]); else path_u.emplace_back(tin[v] + (exclude_lca ? 1 : 0), tin[u]); reverse(path_v.begin(), path_v.end()); return {path_u, path_v}; } /* subtree (u) -> [tin, tout) */ pair get_subtree(int u) { return {tin[u], tout[u]}; } }; /* * 辺に重みが付く時 (ui, vi) -> wi * 深いほうの頂点に w_i をつける * etov[i] := depth[ui] < depth[vi] ? vi : ui * query path (u, v) -> get_path(u, v, exclude_lca = true) */ #include "atcoder/segtree.hpp" using S = mat; S op(S a, S b) { return matrix_mul(a,b); } S e() { return { { {1,0}, {0,1} } }; } int main() { int n; in(n); HeavyLightDecomposition hld(n); vector

edges; rep(i,n-1){ int u,v; in(u,v); edges.emplace_back(u,v); hld.add_edge(u,v); } hld.build(); atcoder::segtree seg(n); vector etov(n-1); rep(i,n-1){ auto [u,v] = edges[i]; if(hld.depth[u] < hld.depth[v]) etov[i] = v; else etov[i] = u; } int q; in(q); while(q--){ char c; in(c); if(c == 'x') { int i,x00,x01,x10,x11; in(i,x00,x01,x10,x11); seg.set(hld.tin[etov[i]], { { {x00,x01}, {x10,x11} } }); } else{ int i,j; in(i,j); mat res = e(); auto [pi, pj] = hld.get_path(i,j, true); for (auto [l,r] : pj) res = matrix_mul(res, seg.prod(l,r+1)); out(res[0][0], res[0][1], res[1][0], res[1][1]); } } }