結果
問題 | No.2341 Triple Tree Query (Medium) |
ユーザー | SSRS |
提出日時 | 2023-05-27 04:08:27 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 510 ms / 5,000 ms |
コード長 | 4,055 bytes |
コンパイル時間 | 2,127 ms |
コンパイル使用メモリ | 182,884 KB |
実行使用メモリ | 29,796 KB |
最終ジャッジ日時 | 2024-06-08 20:19:54 |
合計ジャッジ時間 | 19,506 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,376 KB |
testcase_02 | AC | 502 ms
19,204 KB |
testcase_03 | AC | 510 ms
19,200 KB |
testcase_04 | AC | 494 ms
19,176 KB |
testcase_05 | AC | 505 ms
19,352 KB |
testcase_06 | AC | 500 ms
19,336 KB |
testcase_07 | AC | 391 ms
27,876 KB |
testcase_08 | AC | 390 ms
26,852 KB |
testcase_09 | AC | 397 ms
29,796 KB |
testcase_10 | AC | 401 ms
28,004 KB |
testcase_11 | AC | 403 ms
29,284 KB |
testcase_12 | AC | 415 ms
26,468 KB |
testcase_13 | AC | 442 ms
27,744 KB |
testcase_14 | AC | 395 ms
27,240 KB |
testcase_15 | AC | 393 ms
29,156 KB |
testcase_16 | AC | 398 ms
25,832 KB |
testcase_17 | AC | 332 ms
18,956 KB |
testcase_18 | AC | 330 ms
18,964 KB |
testcase_19 | AC | 327 ms
18,880 KB |
testcase_20 | AC | 323 ms
18,956 KB |
testcase_21 | AC | 334 ms
18,956 KB |
testcase_22 | AC | 337 ms
18,956 KB |
testcase_23 | AC | 339 ms
18,864 KB |
testcase_24 | AC | 329 ms
18,960 KB |
testcase_25 | AC | 451 ms
20,812 KB |
testcase_26 | AC | 450 ms
20,688 KB |
testcase_27 | AC | 449 ms
20,940 KB |
testcase_28 | AC | 447 ms
20,820 KB |
testcase_29 | AC | 450 ms
20,692 KB |
testcase_30 | AC | 329 ms
18,944 KB |
testcase_31 | AC | 332 ms
18,948 KB |
testcase_32 | AC | 331 ms
18,944 KB |
testcase_33 | AC | 406 ms
19,184 KB |
testcase_34 | AC | 410 ms
19,124 KB |
testcase_35 | AC | 413 ms
19,156 KB |
testcase_36 | AC | 419 ms
19,032 KB |
testcase_37 | AC | 417 ms
19,052 KB |
ソースコード
#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); vector<int> b; queue<int> q; q.push(0); while (!q.empty()){ int v = q.front(); q.pop(); b.push_back(v); for (int w : E[v]){ if (w != p[v]){ p[w] = v; c[v].push_back(w); q.push(w); } } } reverse(b.begin(), b.end()); vector<int> sz(N, 1); for (int v : b){ for (int &w : c[v]){ sz[v] += sz[w]; if (sz[w] > sz[c[v][0]]){ swap(w, c[v][0]); } } } vector<int> in(N); in[0] = 0; int t = 1; vector<int> next(N); for (int i = 0; i < N; i++){ next[i] = i; } auto dfs = [&](auto dfs, int v = 0) -> void { if (v != 0){ if (in[p[v]] == in[v] - 1){ next[v] = next[p[v]]; } } if (!c[v].empty()){ in[c[v][0]] = t; t++; dfs(dfs, c[v][0]); int cnt = c[v].size(); for (int i = 1; i < cnt; i++){ in[c[v][i]] = t; t++; } for (int i = 1; i < cnt; i++){ dfs(dfs, c[v][i]); } } }; 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[in[V]], X[V]) << endl; } if (T == 2){ int V, K; long long B, C; cin >> V >> K >> B >> C; V--; if (V != 0){ ST.range_apply(in[p[V]], in[p[V]] + 1, affine(B, C)); } ST.range_apply(in[V], in[V] + 1, affine(B, C)); if (!c[V].empty()){ ST.range_apply(in[c[V][0]], in[c[V][0]] + 1, affine(B, C)); } if (c[V].size() >= 2){ ST.range_apply(in[c[V][1]], in[c[V].back()] + 1, affine(B, C)); } } if (T == 3){ int V; long long B, C; cin >> V >> B >> C; V--; ST.range_apply(in[V], in[V] + 1, affine(B, C)); if (!c[V].empty()){ ST.range_apply(in[c[V][0]], in[c[V][0]] + sz[V] - 1, affine(B, C)); } } if (T == 4){ int U, V; long long B, C; cin >> U >> V >> B >> C; U--; V--; while (true){ if (in[U] > in[V]){ swap(U, V); } if (next[U] == next[V]){ ST.range_apply(in[U], in[V] + 1, affine(B, C)); break; } ST.range_apply(in[next[V]], in[V] + 1, affine(B, C)); V = p[next[V]]; } } } }