#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; const ll MOD=1e9+7; using Matrix=vector>; Matrix matrixmul(int l, int m, int n, Matrix a, Matrix b){ Matrix c(l, vector(n)); for(int i=0; i struct SegmentTree{ using F=function; int sz; vector seg; const F f; const Monoid e; SegmentTree(int n, const F f, const Monoid &e): f(f), e(e){ sz=1; while(sz v): f(f), e(e){ sz=1; while(sz=1; i--){ seg[i]=f(seg[2*i], seg[2*i+1]); } } void update(int k, const Monoid &x){ k+=sz; seg[k]=x; while(k>1){ k>>=1; seg[k]=f(seg[2*k], seg[2*k+1]); } } Monoid query(int a, int b){ a+=sz, b+=sz; Monoid retl=e, retr=e; for(;a>=1, b>>=1){ if(b&1) retr=f(seg[--b], retr); if(a&1) retl=f(retl, seg[a++]); } return f(retl, retr); } Monoid operator[](const int &k) const{ return seg[k+sz]; } }; template< typename G > struct HeavyLightDecomposition { G &g; vector< int > sz, in, out, head, rev, par; HeavyLightDecomposition(G &g) : g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {} void dfs_sz(int idx, int p) { par[idx] = p; sz[idx] = 1; if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back()); for(auto &to : g[idx]) { if(to == p) continue; dfs_sz(to, idx); sz[idx] += sz[to]; if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to); } } void dfs_hld(int idx, int par, int ×) { in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]) { if(to == par) continue; head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times); } out[idx] = times; } void build() { dfs_sz(0, -1); int t = 0; dfs_hld(0, -1, t); } int la(int v, int k) { while(1) { int u = head[v]; if(in[v] - k >= in[u]) return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) return u; } } template< typename T, typename Q, typename F > T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) { T l = ti;//r = ti; for(;; v = par[head[v]]) { //if(in[u] > in[v]) swap(u, v), swap(l, r); if(head[u] == head[v]) break; l = f(q(in[head[v]], in[v] + 1), l); } return f(q(in[u] + edge, in[v] + 1), l); // return {f(q(in[u] + edge, in[v] + 1), l), r}; } template< typename Q > void add(int u, int v, const Q &q, bool edge = false) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) break; q(in[head[v]], in[v] + 1); } q(in[u] + edge, in[v] + 1); } }; int main() { int n; cin>>n; vector> g(n); int a[100010], b[100010]; for(int i=0; i>a[i]>>b[i]; g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } HeavyLightDecomposition>> hld(g); hld.build(); Matrix e(2, vector(2)); e[0][0]=e[1][1]=1; using F=function; auto f=[&](Matrix a, Matrix b){ return matrixmul(2, 2, 2, a, b);}; SegmentTree seg(n, f, e); using Q=function; auto q=[&](int a, int b){ return seg.query(a, b);}; int qc; cin>>qc; for(int t=0; t>c; if(c=='g'){ int i, j; cin>>i>>j; Matrix ans=hld.query(i, j, e, q, f, true); cout<(2)); cin>>i>>x[0][0]>>x[0][1]>>x[1][0]>>x[1][1]; if(hld.in[a[i]]>hld.in[b[i]]) swap(a[i], b[i]); seg.update(hld.in[b[i]], x); } } return 0; }