結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | t98slider |
提出日時 | 2023-03-06 17:56:23 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 489 ms / 7,000 ms |
コード長 | 3,820 bytes |
コンパイル時間 | 2,507 ms |
コンパイル使用メモリ | 193,504 KB |
実行使用メモリ | 37,076 KB |
最終ジャッジ日時 | 2024-11-23 04:20:03 |
合計ジャッジ時間 | 19,545 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 23 ms
28,240 KB |
testcase_02 | AC | 23 ms
28,244 KB |
testcase_03 | AC | 23 ms
28,240 KB |
testcase_04 | AC | 210 ms
31,184 KB |
testcase_05 | AC | 198 ms
31,316 KB |
testcase_06 | AC | 250 ms
32,848 KB |
testcase_07 | AC | 253 ms
34,128 KB |
testcase_08 | AC | 246 ms
34,128 KB |
testcase_09 | AC | 209 ms
21,152 KB |
testcase_10 | AC | 233 ms
21,152 KB |
testcase_11 | AC | 232 ms
20,892 KB |
testcase_12 | AC | 205 ms
18,964 KB |
testcase_13 | AC | 97 ms
5,248 KB |
testcase_14 | AC | 92 ms
5,248 KB |
testcase_15 | AC | 287 ms
33,616 KB |
testcase_16 | AC | 265 ms
26,488 KB |
testcase_17 | AC | 158 ms
10,880 KB |
testcase_18 | AC | 366 ms
35,152 KB |
testcase_19 | AC | 273 ms
19,348 KB |
testcase_20 | AC | 362 ms
35,156 KB |
testcase_21 | AC | 353 ms
33,620 KB |
testcase_22 | AC | 346 ms
33,364 KB |
testcase_23 | AC | 156 ms
36,036 KB |
testcase_24 | AC | 135 ms
19,288 KB |
testcase_25 | AC | 150 ms
34,644 KB |
testcase_26 | AC | 154 ms
34,516 KB |
testcase_27 | AC | 293 ms
34,644 KB |
testcase_28 | AC | 308 ms
34,644 KB |
testcase_29 | AC | 332 ms
35,668 KB |
testcase_30 | AC | 326 ms
35,536 KB |
testcase_31 | AC | 489 ms
32,984 KB |
testcase_32 | AC | 464 ms
32,728 KB |
testcase_33 | AC | 437 ms
31,956 KB |
testcase_34 | AC | 358 ms
31,360 KB |
testcase_35 | AC | 292 ms
30,932 KB |
testcase_36 | AC | 294 ms
29,912 KB |
testcase_37 | AC | 462 ms
23,660 KB |
testcase_38 | AC | 470 ms
23,340 KB |
testcase_39 | AC | 462 ms
23,724 KB |
testcase_40 | AC | 450 ms
23,856 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
testcase_44 | AC | 249 ms
36,312 KB |
testcase_45 | AC | 256 ms
36,088 KB |
testcase_46 | AC | 261 ms
37,076 KB |
testcase_47 | AC | 289 ms
36,952 KB |
testcase_48 | AC | 265 ms
36,560 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<ll> diameter; vector<array<int, 2>> diameter_vertex; queue<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); diameter_vertex.resize(N); for(int i = 0; i < N; i++) diameter_vertex[i] = {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]; G[u].emplace_back(v, w); G[v].emplace_back(u, w); update_parent(v, u, w); que.emplace(v); while(!que.empty()){ v = que.front(); que.pop(); parent_or_size[v] = x; for(auto &edge : G[v]){ tie(u, w) = edge; if(parent[0][v] == u) continue; update_parent(u, v, w); que.emplace(u); } } update_diameter(x, y); } void update_parent(int &v, int &par, int &w){ parent[0][v] = par; int depth_log = __lg(max(depth[v], depth[par] + 1)); depth[v] = depth[par] + 1; dp[v] = dp[par] + w; for(int i = 0; i < depth_log; i++){ parent[i + 1][v] = parent[i][v] == -1 ? -1 : parent[i][parent[i][v]]; } } void update_diameter(int &x, int &y){ ll mx; int u, v; if(diameter[x] >= diameter[y]) { mx = diameter[x]; u = diameter_vertex[x][0], v = diameter_vertex[x][1]; } else { mx = diameter[y]; u = diameter_vertex[y][0], v = diameter_vertex[y][1]; } for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ ll d = dist(diameter_vertex[x][i], diameter_vertex[y][j]); if(d > mx){ mx = d; u = diameter_vertex[x][i], v = diameter_vertex[y][j]; } } } diameter[x] = mx; diameter_vertex[x] = {u, v}; } 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]; while(d){ v = parent[__lg(d & -d)][v]; d -= d & -d; } if(u == v) return res - 2 * dp[v]; for(int i = __lg(depth[v]); 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; problem_H uf(N); int type, u, v, w; while(Q--){ cin >> type; if(type == 1){ cin >> v >> w; uf.merge(v, X, w); }else if(type == 2){ cin >> u >> v; ll d = uf.dist(u, v); cout << d << '\n'; if(d != -1) (X += d) %= N; }else if(type == 3){ cin >> v; cout << uf.diameter[uf.leader(v)] << '\n'; }else{ cin >> v; X += v; if(X >= N) X -= N; } } }