結果

問題 No.2340 Triple Tree Query (Easy)
ユーザー SSRSSSRS
提出日時 2023-05-27 04:42:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 488 ms / 5,000 ms
コード長 2,973 bytes
コンパイル時間 1,923 ms
コンパイル使用メモリ 181,952 KB
実行使用メモリ 26,132 KB
最終ジャッジ日時 2023-08-28 00:40:47
合計ジャッジ時間 19,277 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 10 ms
4,380 KB
testcase_02 AC 10 ms
4,380 KB
testcase_03 AC 9 ms
4,380 KB
testcase_04 AC 10 ms
4,380 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 452 ms
19,356 KB
testcase_07 AC 451 ms
19,204 KB
testcase_08 AC 447 ms
19,556 KB
testcase_09 AC 451 ms
19,260 KB
testcase_10 AC 452 ms
19,288 KB
testcase_11 AC 450 ms
19,468 KB
testcase_12 AC 449 ms
19,680 KB
testcase_13 AC 452 ms
19,372 KB
testcase_14 AC 452 ms
19,276 KB
testcase_15 AC 458 ms
19,272 KB
testcase_16 AC 488 ms
26,132 KB
testcase_17 AC 486 ms
24,388 KB
testcase_18 AC 483 ms
24,016 KB
testcase_19 AC 485 ms
25,528 KB
testcase_20 AC 487 ms
23,996 KB
testcase_21 AC 401 ms
19,012 KB
testcase_22 AC 399 ms
19,064 KB
testcase_23 AC 402 ms
19,012 KB
testcase_24 AC 483 ms
20,844 KB
testcase_25 AC 478 ms
20,872 KB
testcase_26 AC 483 ms
20,904 KB
testcase_27 AC 483 ms
20,896 KB
testcase_28 AC 485 ms
20,888 KB
testcase_29 AC 412 ms
19,096 KB
testcase_30 AC 410 ms
19,188 KB
testcase_31 AC 411 ms
19,264 KB
testcase_32 AC 432 ms
19,236 KB
testcase_33 AC 430 ms
19,192 KB
testcase_34 AC 433 ms
19,292 KB
testcase_35 AC 433 ms
19,352 KB
testcase_36 AC 431 ms
19,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
struct affine{
  long long a, b;
  affine(){
    a = 1;
    b = 0;
  }
  affine(int a, int b): a(a), b(b){
  }
};
affine composite(affine A, affine B){
  return affine(A.a * B.a % MOD, (A.b * B.a + B.b) % MOD);
}
int value(affine A, int x){
  return (A.a * x + A.b) % MOD;
}
template <typename T>
struct dual_segment_tree{
  int N;
  vector<T> ST;
  function<T(T, T)> f;
  T E;
  dual_segment_tree(int n, function<T(T, T)> f, T E): f(f), E(E){
    N = 1;
    while (N < n){
      N *= 2;
    }
    ST = vector<T>(N * 2 - 1, E);
  }
  void push(int i){
    if (i < N - 1){
      ST[i * 2 + 1] = f(ST[i * 2 + 1], ST[i]);
      ST[i * 2 + 2] = f(ST[i * 2 + 2], ST[i]);
      ST[i] = E;
    }
  }
  T operator [](int k){
    int v = 0;
    for (int i = N / 2; i >= 1; i >>= 1){
      push(v);
      if ((k & i) == 0){
        v = v * 2 + 1;
      } else {
        v = v * 2 + 2;
      }
    }
    return ST[v];
  }
  void range_apply(int L, int R, T x, int i, int l, int r){
    if (r <= L || R <= l){
    } else if (L <= l && r <= R){
      ST[i] = f(ST[i], x);
    } else {
      push(i);
      int m = (l + r) / 2;
      range_apply(L, R, x, i * 2 + 1, l, m);
      range_apply(L, R, x, i * 2 + 2, m, r);
    }
  }
  void range_apply(int L, int R, T x){
    range_apply(L, R, x, 0, 0, N);
  }
};
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    E[A].push_back(B);
    E[B].push_back(A);
  }
  vector<int> X(N);
  for (int i = 0; i < N; i++){
    cin >> X[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);
      }
    }
  }
  vector<int> pos(N);
  pos[0] = 0;
  int t = 1;
  vector<int> in(N), out(N), mn(N), mx(N);
  auto dfs = [&](auto dfs, int v = 0) -> void {
    in[v] = t;
    mn[v] = t;
    for (int w : c[v]){
      pos[w] = t;
      t++;
    }
    mx[v] = t;
    for (int w : c[v]){
      dfs(dfs, w);
    }
    out[v] = t;
  };
  dfs(dfs);
  dual_segment_tree<affine> ST(N, composite, affine());
  for (int i = 0; i < Q; i++){
    int T;
    cin >> T;
    if (T == 1){
      int V;
      cin >> V;
      V--;
      cout << value(ST[pos[V]], X[V]) << endl;
    }
    if (T == 2){
      int V, K;
      long long C, D;
      cin >> V >> K >> C >> D;
      V--;
      if (V != 0){
        ST.range_apply(pos[p[V]], pos[p[V]] + 1, affine(C, D));
      }
      ST.range_apply(pos[V], pos[V] + 1, affine(C, D));
      ST.range_apply(mn[V], mx[V], affine(C, D));
    }
    if (T == 3){
      int V;
      long long C, D;
      cin >> V >> C >> D;
      V--;
      ST.range_apply(pos[V], pos[V] + 1, affine(C, D));
      ST.range_apply(in[V], out[V], affine(C, D));
    }
  }
}
0