結果

問題 No.650 行列木クエリ
ユーザー SSRSSSRS
提出日時 2022-04-24 04:38:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 3,863 bytes
コンパイル時間 3,477 ms
コンパイル使用メモリ 219,132 KB
実行使用メモリ 29,048 KB
最終ジャッジ日時 2023-09-07 20:12:17
合計ジャッジ時間 4,141 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 71 ms
7,548 KB
testcase_02 AC 159 ms
23,428 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 74 ms
7,528 KB
testcase_05 AC 161 ms
23,692 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 64 ms
8,584 KB
testcase_09 AC 135 ms
29,048 KB
testcase_10 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
array<array<long long, 2>, 2> matrix_multiplication(array<array<long long, 2>, 2> A, array<array<long long, 2>, 2> B){
  array<array<long long, 2>, 2> C;
  for (int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      C[i][j] = (A[i][0] * B[0][j] + A[i][1] * B[1][j]) % MOD;
    }
  }
  return C;
}
struct segment_tree{
  int N;
  vector<array<array<long long, 2>, 2>> ST;
  segment_tree(){
  }
  segment_tree(int N2){
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<array<array<long long, 2>, 2>>(N * 2 - 1, array<array<long long, 2>, 2>{array<long long, 2>{1, 0}, array<long long, 2>{0, 1}});
  }
  void update(int i, array<array<long long, 2>, 2> x){
    i += N - 1;
    ST[i] = x;
    while (i > 0){
      i = (i - 1) / 2;
      ST[i] = matrix_multiplication(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  array<array<long long, 2>, 2> range_fold(int L, int R, int i, int l, int r){
    if (r <= L || R <= l){
      return array<array<long long, 2>, 2>{array<long long, 2>{1, 0}, array<long long, 2>{0, 1}};
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return matrix_multiplication(range_fold(L, R, i * 2 + 1, l, m), range_fold(L, R, i * 2 + 2, m, r));
    }
  }
  array<array<long long, 2>, 2> range_fold(int L, int R){
    return range_fold(L, R, 0, 0, N);
  }
};
struct heavy_light_decomposition{
  vector<int> p, in, sz, next;
  segment_tree ST;
  heavy_light_decomposition(vector<int> &p, vector<vector<int>> &c): p(p){
    int N = p.size();
    sz = vector<int>(N);
    dfs1(c);
    in = vector<int>(N);
    next = vector<int>(N);
    next[0] = 0;
    int t = 0;
    dfs2(c, t);
    ST = segment_tree(N);
  }
  void dfs1(vector<vector<int>> &c, int v = 0){
    sz[v] = 1;
    for (int &w : c[v]){
      dfs1(c, w);
      sz[v] += sz[w];
      if (sz[w] > sz[c[v][0]]){
        swap(w, c[v][0]);
      }
    }
  }
  void dfs2(vector<vector<int>> &c, int &t, int v = 0){
    in[v] = t;
    t++;
    for (int w : c[v]){
      if (w == c[v][0]){
        next[w] = next[v];
      } else {
        next[w] = w;
      }
      dfs2(c, t, w);
    }
  }
  void update(int v, array<array<long long, 2>, 2> x){
    ST.update(in[v], x);
  }
  array<array<long long, 2>, 2> query(int u, int v){
    array<array<long long, 2>, 2> ans = {array<long long, 2>{1, 0}, array<long long, 2>{0, 1}};
    while (next[v] != next[u]){
      ans = matrix_multiplication(ST.range_fold(in[next[v]], in[v] + 1), ans);
      v = p[next[v]];
    }
    ans = matrix_multiplication(ST.range_fold(in[u] + 1, in[v] + 1), ans);
    return ans;
  }
};
int main(){
  int n;
  cin >> n;
  vector<int> a(n - 1), b(n - 1);
  for (int i = 0; i < n - 1; i++){
    cin >> a[i] >> b[i];
  }
  vector<vector<int>> E(n);
  for (int i = 0; i < n - 1; i++){
    E[a[i]].push_back(b[i]);
    E[b[i]].push_back(a[i]);
  }
  vector<int> p(n, -1);
  vector<vector<int>> c(n);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  for (int i = 0; i < n - 1; i++){
    if (b[i] == p[a[i]]){
      swap(a[i], b[i]);
    }
  }
  heavy_light_decomposition T(p, c);
  int q;
  cin >> q;
  for (int k = 0; k < q; k++){
    char t;
    cin >> t;
    if (t == 'x'){
      int i;
      long long x00, x01, x10, x11;
      cin >> i >> x00 >> x01 >> x10 >> x11;
      T.update(b[i], array<array<long long, 2>, 2>{array<long long, 2>{x00, x01}, array<long long, 2>{x10, x11}});
    }
    if (t == 'g'){
      int i, j;
      cin >> i >> j;
      array<array<long long, 2>, 2> ans = T.query(i, j);
      cout << ans[0][0] << ' ' << ans[0][1] << ' ' << ans[1][0] << ' ' << ans[1][1] << endl;
    }
  }
}
0