結果
問題 | No.650 行列木クエリ |
ユーザー | ふっぴー |
提出日時 | 2018-02-20 01:38:30 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 6,720 bytes |
コンパイル時間 | 3,433 ms |
コンパイル使用メモリ | 223,940 KB |
実行使用メモリ | 34,464 KB |
最終ジャッジ日時 | 2024-07-16 04:05:01 |
合計ジャッジ時間 | 5,584 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | AC | 3 ms
6,940 KB |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | AC | 3 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | AC | 2 ms
6,940 KB |
ソースコード
#include "bits/stdc++.h" using namespace std; #define DEBUG(x) cout<<#x<<": "<<x<<endl; #define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl typedef long long ll; #define vi vector<int> #define vl vector<ll> #define vii vector< vector<int> > #define vll vector< vector<ll> > #define vs vector<string> #define pii pair<int,int> #define pis pair<int,string> #define psi pair<string,int> #define pll pair<ll,ll> #define fi first #define se second #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() const int inf = 1000000001; const ll INF = 2e18 * 2; #define MOD 1000000007 #define mod 1000000009 #define pi 3.14159265358979323846 #define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl; int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 }; struct Modint { ll n; static const ll Mod = 1000000007; Modint(int n = 0) : n(n) {} }; Modint operator+(Modint a, Modint b) { return Modint((a.n += b.n) >= mod ? a.n - a.Mod : a.n); } Modint operator-(Modint a, Modint b) { return Modint((a.n -= b.n) < 0 ? a.n + a.Mod : a.n); } Modint operator*(Modint a, Modint b) { return Modint(1LL * a.n * b.n % a.Mod); } Modint &operator+=(Modint &a, Modint b) { return a = a + b; } Modint &operator-=(Modint &a, Modint b) { return a = a - b; } Modint &operator*=(Modint &a, Modint b) { return a = a * b; } typedef vector<Modint> vec; typedef vector<vec> mat; mat mul(mat A, mat B) { mat C(A.size(), vec(B[0].size())); for (int i = 0; i < A.size(); i++) { for (int k = 0; k < B.size(); k++) { for (int j = 0; j < B[0].size(); j++) { C[i][j] = C[i][j] + A[i][k] * B[k][j]; } } } return C; } mat pow(mat A, ll n) { mat B(A.size(), vec(A.size())); for (int i = 0; i < A.size(); i++) { B[i][i] = 1; } while (n > 0) { if (n & 1)B = mul(B, A); A = mul(A, A); n >>= 1; } return B; } struct HLDecomposition { int n, pos; vector<vector<int> > G; vector<int> vid, head, sub, hvy, par, dep, inv, type; HLDecomposition() {} HLDecomposition(int sz) : n(sz), pos(0), G(n), vid(n, -1), head(n), sub(n, 1), hvy(n, -1), par(n), dep(n), inv(n), type(n) {} void add_edge(int u, int v) { G[u].push_back(v); G[v].push_back(u); } void build(vector<int> rs = { 0 }) { int c = 0; for (int r : rs) { dfs(r); bfs(r, c++); } } void dfs(int rt) { using T = pair<int, int>; stack<T> st; par[rt] = -1; dep[rt] = 0; st.emplace(rt, 0); while (!st.empty()) { int v = st.top().first; int &i = st.top().second; if (i<(int)G[v].size()) { int u = G[v][i++]; if (u == par[v]) continue; par[u] = v; dep[u] = dep[v] + 1; st.emplace(u, 0); } else { st.pop(); int res = 0; for (int u : G[v]) { if (u == par[v]) continue; sub[v] += sub[u]; if (res<sub[u]) res = sub[u], hvy[v] = u; } } } } void bfs(int r, int c) { int &k = pos; queue<int> q({ r }); while (!q.empty()) { int h = q.front(); q.pop(); for (int i = h; i != -1; i = hvy[i]) { type[i] = c; vid[i] = k++; inv[vid[i]] = i; head[i] = h; for (int j : G[i]) if (j != par[i] && j != hvy[i]) q.push(j); } } } // for_each(vertex) // [l,r] <- attention!! void for_each(int u, int v, const function<void(int, int)>& f) { while (1) { if (vid[u]>vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]); if (head[u] != head[v]) v = par[head[v]]; else break; } } // for_each(edge) // [l,r] <- attention!! void for_each_edge(int u, int v, const function<void(int, int)>& f) { while (1) { if (vid[u]>vid[v]) swap(u, v); if (head[u] != head[v]) { f(vid[head[v]], vid[v]); v = par[head[v]]; } else { if (u != v) f(vid[u] + 1, vid[v]); break; } } } int lca(int u, int v) { while (1) { if (vid[u]>vid[v]) swap(u, v); if (head[u] == head[v]) return u; v = par[head[v]]; } } int distance(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } }; class SegmentTree { public: const ll MAX_N = 1 << 10; const mat E{ {1,0},{0,1} }; int n; vector<mat> dat; SegmentTree(int _n) :n(_n) { dat.resize(2 * MAX_N - 1, E); } //k番目の値をaに変更 void update(int k, mat a, int v, int l, int r) { if (r - l == 1) { dat[v] = a; } else { if (k < (l + r) / 2) update(k, a, 2 * v + 1, l, (l + r) / 2); else { update(k, a, 2 * v + 2, (l + r) / 2, r); } dat[v] = mul(dat[v * 2 + 1], dat[v * 2 + 2]); } } //[a,b)の最小値を求める //後ろのほうの引数は計算の簡単のための引数 //kは接点の番号,l,rはその接点が[l,r)に対応していることを表す //従って、外からはquery(a,b,0,0,n)としてよぶ mat query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return E; } if (a <= l && r <= b) { return dat[k]; } else { mat ul = query(a, b, k * 2 + 1, l, (l + r) / 2); mat ur = query(a, b, k * 2 + 2, (l + r) / 2, r); return mul(ul, ur); } } }; vector<set<int> > child; vii G; void make_child(int parent, int now) { rep(i, G[now].size()) { if (G[now][i] == parent) { continue; } child[now].insert(G[now][i]); make_child(now, G[now][i]); } return; } int main() { int n; cin >> n; child.resize(n); G.resize(n); HLDecomposition hld(n); vi a(n - 1), b(n - 1); rep(i, n - 1) { cin >> a[i] >> b[i]; hld.add_edge(a[i], b[i]); G[a[i]].push_back(b[i]); G[b[i]].push_back(a[i]); } make_child(-1, 0); hld.build(); //DEBUG_VEC(hld.vid); SegmentTree st(n); int q; cin >> q; while (q--) { char c; cin >> c; if (c == 'x') { int i; cin >> i; int u = a[i], v = b[i]; mat x(2, vec(2)); rep(j, 2) { rep(k, 2) { cin >> x[j][k].n; } } if (hld.vid[u] > hld.vid[v]) { swap(u, v); } //DEBUG(u); DEBUG(v); st.update(hld.vid[v], x, 0, 0, st.n); } else { int u, v; cin >> u >> v; mat ans = { {1, 0}, {0, 1} }; while (1) { if (hld.vid[u]>hld.vid[v]) swap(u, v); if (hld.head[u] != hld.head[v]) { ans = mul(st.query(hld.vid[hld.head[v]], hld.vid[v] + 1, 0, 0, st.n), ans); v = hld.par[hld.head[v]]; } else { if (u != v)ans = mul(st.query(hld.vid[u] + 1, hld.vid[v] + 1, 0, 0, st.n), ans); break; } } rep(i, 2) { rep(j, 2) { cout << ans[i][j].n << " "; } } cout << endl; } /* rep(i, n) { auto temp = st.query(hld.vid[i], hld.vid[i] + 1, 0, 0, st.n); cout << "("; rep(j, 2) { rep(k, 2) { cout << temp[j][k].n << " "; } } cout << ") "; } cout << endl; //*/ } }