結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | t98slider |
提出日時 | 2023-02-16 09:50:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 622 ms / 7,000 ms |
コード長 | 3,867 bytes |
コンパイル時間 | 2,520 ms |
コンパイル使用メモリ | 198,568 KB |
実行使用メモリ | 37,332 KB |
最終ジャッジ日時 | 2024-05-02 13:55:37 |
合計ジャッジ時間 | 23,519 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 21 ms
28,240 KB |
testcase_02 | AC | 21 ms
28,116 KB |
testcase_03 | AC | 22 ms
28,240 KB |
testcase_04 | AC | 254 ms
31,316 KB |
testcase_05 | AC | 238 ms
31,316 KB |
testcase_06 | AC | 325 ms
32,852 KB |
testcase_07 | AC | 349 ms
34,136 KB |
testcase_08 | AC | 351 ms
34,008 KB |
testcase_09 | AC | 261 ms
21,152 KB |
testcase_10 | AC | 261 ms
21,020 KB |
testcase_11 | AC | 261 ms
21,028 KB |
testcase_12 | AC | 249 ms
18,964 KB |
testcase_13 | AC | 100 ms
5,376 KB |
testcase_14 | AC | 93 ms
5,376 KB |
testcase_15 | AC | 363 ms
33,620 KB |
testcase_16 | AC | 328 ms
26,616 KB |
testcase_17 | AC | 168 ms
11,008 KB |
testcase_18 | AC | 487 ms
35,156 KB |
testcase_19 | AC | 316 ms
19,224 KB |
testcase_20 | AC | 482 ms
35,156 KB |
testcase_21 | AC | 428 ms
33,612 KB |
testcase_22 | AC | 412 ms
33,236 KB |
testcase_23 | AC | 243 ms
36,168 KB |
testcase_24 | AC | 191 ms
19,408 KB |
testcase_25 | AC | 250 ms
34,576 KB |
testcase_26 | AC | 252 ms
34,640 KB |
testcase_27 | AC | 547 ms
34,768 KB |
testcase_28 | AC | 563 ms
34,644 KB |
testcase_29 | AC | 622 ms
35,536 KB |
testcase_30 | AC | 621 ms
35,668 KB |
testcase_31 | AC | 555 ms
32,980 KB |
testcase_32 | AC | 516 ms
32,720 KB |
testcase_33 | AC | 455 ms
32,088 KB |
testcase_34 | AC | 331 ms
31,320 KB |
testcase_35 | AC | 306 ms
30,936 KB |
testcase_36 | AC | 279 ms
30,164 KB |
testcase_37 | AC | 491 ms
23,856 KB |
testcase_38 | AC | 486 ms
23,204 KB |
testcase_39 | AC | 488 ms
23,856 KB |
testcase_40 | AC | 505 ms
23,732 KB |
testcase_41 | AC | 2 ms
5,376 KB |
testcase_42 | AC | 2 ms
5,376 KB |
testcase_43 | AC | 2 ms
5,376 KB |
testcase_44 | AC | 442 ms
37,332 KB |
testcase_45 | AC | 447 ms
36,560 KB |
testcase_46 | AC | 462 ms
37,204 KB |
testcase_47 | AC | 463 ms
37,076 KB |
testcase_48 | AC | 458 ms
36,564 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct problem_H { int MAX_LOGV, N; vector<vector<pair<int, int>>> G; vector<vector<int>> parent; vector<ll> dp; vector<int> depth; vector<int> parent_or_size; vector<tuple<ll, int, int>> diameter; queue<int, list<int>> que; problem_H(int n) : N(n) { MAX_LOGV = __lg(N) + 1; G.resize(N); parent.resize(MAX_LOGV, vector<int>(N, -1)); parent_or_size.resize(N, -1); dp.resize(N), depth.resize(N); diameter.resize(N); for(int i = 0; i < N; i++) diameter[i] = make_tuple(0, i, i); } int leader(int v){ return parent_or_size[v] < 0 ? v : parent_or_size[v]; } void merge(int u, int v, int w){ int x = leader(u), y = leader(v); if(-parent_or_size[x] < -parent_or_size[y]) { swap(x, y); swap(u, v); } parent_or_size[x] += parent_or_size[y]; int log_limit = __lg(-parent_or_size[x]) + 1; G[u].emplace_back(v, w); G[v].emplace_back(u, w); parent[0][v] = u; depth[v] = depth[u] + 1; dp[v] = dp[u] + w; que.emplace(v); while(!que.empty()){ v = que.front(); que.pop(); parent_or_size[v] = x; for(int i = 0; i + 1 < log_limit; i++){ if(parent[i][v] == -1) parent[i + 1][v] = -1; else parent[i + 1][v] = parent[i][parent[i][v]]; } for(auto &edge : G[v]){ tie(u, w) = edge; if(parent[0][v] == u) continue; que.emplace(u); parent[0][u] = v; depth[u] = depth[v] + 1; dp[u] = dp[v] + w; } } update_diameter(x, y); } void update_diameter(int x, int y){ tuple<ll, int, int> mx = max(diameter[x], diameter[y]); array<int, 4> cv; ll temp; tie(temp, cv[0], cv[1]) = diameter[x]; tie(temp, cv[2], cv[3]) = diameter[y]; for(int i = 0; i < 2; i++){ for(int j = 2; j < 4; j++){ mx = max(mx, make_tuple(dist(cv[i], cv[j]), cv[i], cv[j])); } } diameter[x] = mx; } long long dist(int u, int v){ if(leader(u) != leader(v)) return -1; if(depth[v] < depth[u]) swap(v, u); long long res = dp[u] + dp[v]; int d = depth[v] - depth[u]; for(int i = MAX_LOGV - 1; i >= 0; i--){ if(d >> i & 1) v = parent[i][v]; } if(u == v) return res - 2 * dp[v]; for(int i = MAX_LOGV - 1; i >= 0; i--){ if(parent[i][v] != parent[i][u]){ v = parent[i][v]; u = parent[i][u]; } } v = parent[0][v]; return res - 2 * dp[v]; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int N, Q; ll X; cin >> N >> X >> Q; assert(1 <= N && N <= 200000); assert(0 <= X && X < N); assert(1 <= Q && Q <= 300000); problem_H uf(N); int type, u, v, w; while(Q--){ cin >> type; if(type == 1){ cin >> v >> w; assert(0 <= v && v < N); assert(0 <= w && w <= 1000000000); uf.merge(v, X, w); }else if(type == 2){ cin >> u >> v; assert(0 <= u && u < N); assert(0 <= v && v < N); ll d = uf.dist(u, v); cout << d << '\n'; if(d != -1) (X += d) %= N; }else if(type == 3){ cin >> v; assert(0 <= v && v < N); cout << get<0>(uf.diameter[uf.leader(v)]) << '\n'; }else{ cin >> v; assert(0 <= v && v < N); X += v; if(X >= N) X -= N; } } }