結果
問題 | No.2342 Triple Tree Query (Hard) |
ユーザー | SSRS |
提出日時 | 2023-05-30 16:37:03 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,339 bytes |
コンパイル時間 | 2,587 ms |
コンパイル使用メモリ | 196,076 KB |
実行使用メモリ | 103,556 KB |
最終ジャッジ日時 | 2024-06-08 20:26:48 |
合計ジャッジ時間 | 29,519 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 15 ms
5,376 KB |
testcase_03 | AC | 15 ms
5,576 KB |
testcase_04 | AC | 13 ms
5,376 KB |
testcase_05 | AC | 14 ms
5,376 KB |
testcase_06 | AC | 15 ms
5,376 KB |
testcase_07 | AC | 936 ms
72,332 KB |
testcase_08 | AC | 967 ms
72,464 KB |
testcase_09 | AC | 1,128 ms
72,472 KB |
testcase_10 | AC | 811 ms
72,336 KB |
testcase_11 | AC | 794 ms
72,332 KB |
testcase_12 | AC | 820 ms
72,332 KB |
testcase_13 | AC | 802 ms
72,388 KB |
testcase_14 | AC | 802 ms
72,332 KB |
testcase_15 | AC | 803 ms
72,336 KB |
testcase_16 | AC | 791 ms
72,340 KB |
testcase_17 | AC | 795 ms
101,632 KB |
testcase_18 | AC | 730 ms
103,556 KB |
testcase_19 | AC | 698 ms
101,504 KB |
testcase_20 | AC | 718 ms
102,652 KB |
testcase_21 | AC | 731 ms
103,420 KB |
testcase_22 | AC | 345 ms
67,888 KB |
testcase_23 | AC | 348 ms
68,016 KB |
testcase_24 | AC | 348 ms
68,012 KB |
testcase_25 | WA | - |
testcase_26 | AC | 1,115 ms
95,708 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 1,047 ms
95,588 KB |
testcase_30 | AC | 389 ms
68,008 KB |
testcase_31 | AC | 390 ms
68,004 KB |
testcase_32 | AC | 373 ms
68,004 KB |
testcase_33 | AC | 650 ms
70,272 KB |
testcase_34 | AC | 638 ms
70,128 KB |
testcase_35 | AC | 670 ms
70,372 KB |
testcase_36 | AC | 705 ms
70,120 KB |
testcase_37 | AC | 648 ms
70,280 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; const int KMAX = 10; const int INF = 1000000; 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(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); 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]; } int N2 = N + KMAX; E.resize(N2); E[0].push_back(N); E[N].push_back(0); for (int i = 0; i < KMAX - 1; i++){ E[N + i].push_back(N + i + 1); E[N + i + 1].push_back(N + i); } vector<int> p(N2, -1); vector<vector<int>> c(N2); vector<int> d(N2, 0); vector<int> b; queue<int> q; q.push(N2 - 1); 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); d[w] = d[v] + 1; q.push(w); } } } reverse(b.begin(), b.end()); vector<int> pk(N2); for (int i = 0; i < N; i++){ if (d[i] >= KMAX){ pk[i] = i; for (int j = 0; j < KMAX; j++){ pk[i] = p[pk[i]]; } } } vector<vector<vector<int>>> ck(KMAX + 1, vector<vector<int>>(N2)); for (int i = 0; i < N2; i++){ ck[0][i].push_back(i); } for (int i = 0; i < KMAX; i++){ for (int j = 0; j < N2; j++){ for (int k : c[j]){ for (int l : ck[i][k]){ ck[i + 1][j].push_back(l); } } } } vector<int> sz(N2, 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]); } } } reverse(b.begin(), b.end()); vector<int> id(N2, 0); for (int v : b){ for (int w : c[v]){ if (w == c[v][0]){ id[w] = id[v] + 1; } else { id[w] = 0; } } } vector<vector<int>> hc(KMAX + 1, vector<int>(N2, -1)); for (int i = 0; i < N2; i++){ hc[0][i] = i; for (int j = 0; j < KMAX; j++){ if (hc[j][i] != -1){ if (!c[hc[j][i]].empty()){ hc[j + 1][i] = c[hc[j][i]][0]; } } } } vector<int> in(N); int t = 0; vector<int> in2(N), out2(N); vector<int> hld(N); int t2 = 0; auto dfs1 = [&](auto dfs1, int v) -> void { if (v < N){ in2[v] = t; hld[v] = t2; t2++; } if (id[v] >= KMAX && v < N){ in[v] = t; t++; } for (int w : c[v]){ dfs1(dfs1, w); } if (v < N){ out2[v] = t; } }; dfs1(dfs1, N2 - 1); vector<int> in3(N), out3(N); auto dfs2 = [&](auto dfs2, int v) -> void { for (int w : ck[KMAX][v]){ if (id[w] < KMAX){ in[w] = t; t++; } } if (v < N){ in3[v] = t; } for (int w : c[v]){ dfs2(dfs2, w); } if (v < N){ out3[v] = t; } }; dfs2(dfs2, N2 - 1); vector<int> next(N, 0); for (int v : b){ if (v < N){ for (int w : c[v]){ if (in[w] == in[v] + 1){ next[w] = next[v]; } else { next[w] = w; } } } } vector<vector<int>> mn(KMAX + 1, vector<int>(N2, INF)); vector<vector<int>> mx(KMAX + 1, vector<int>(N2, -1)); for (int i = 0; i < N; i++){ int v = i; for (int j = 0; j <= KMAX; j++){ if (id[i] < KMAX){ mn[j][v] = min(mn[j][v], in[i]); mx[j][v] = max(mx[j][v], in[i]); } if (v == N2 - 1){ break; } v = p[v]; } } 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]) << '\n'; } if (T == 2){ int V, K; long long C, D; cin >> V >> K >> C >> D; V--; for (int j = 0; j <= K; j++){ for (int k = K - j; k >= max(K - j - 1, 0); k--){ if (mn[k][V] != INF){ ST.range_apply(mn[k][V], mx[k][V] + 1, affine(C, D)); } if (hc[k][V] != -1 && hc[k][V] < N){ if (id[hc[k][V]] >= KMAX){ ST.range_apply(in[hc[k][V]], in[hc[k][V]] + 1, affine(C, D)); } } } V = p[V]; } } if (T == 3){ int V; long long C, D; cin >> V >> C >> D; V--; ST.range_apply(in2[V], out2[V], affine(C, D)); for (int j = 0; j <= KMAX; j++){ if (mn[j][V] != INF){ ST.range_apply(mn[j][V], mx[j][V] + 1, affine(C, D)); } } ST.range_apply(in3[V], out3[V], affine(C, D)); } if (T == 4){ int U, V; long long C, D; cin >> U >> V >> C >> D; U--; V--; while (true){ if (hld[U] > hld[V]){ swap(U, V); } if (next[U] == next[V]){ ST.range_apply(in[U], in[V] + 1, affine(C, D)); break; } ST.range_apply(in[next[V]], in[V] + 1, affine(C, D)); V = p[next[V]]; } } } }