結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | t98slider |
提出日時 | 2023-03-08 17:48:56 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 399 ms / 7,000 ms |
コード長 | 4,017 bytes |
コンパイル時間 | 1,937 ms |
コンパイル使用メモリ | 186,012 KB |
実行使用メモリ | 37,848 KB |
最終ジャッジ日時 | 2024-11-23 04:21:57 |
合計ジャッジ時間 | 16,074 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 22 ms
28,880 KB |
testcase_02 | AC | 21 ms
29,008 KB |
testcase_03 | AC | 20 ms
29,008 KB |
testcase_04 | AC | 167 ms
32,088 KB |
testcase_05 | AC | 182 ms
32,020 KB |
testcase_06 | AC | 214 ms
33,620 KB |
testcase_07 | AC | 211 ms
35,032 KB |
testcase_08 | AC | 208 ms
35,028 KB |
testcase_09 | AC | 171 ms
21,536 KB |
testcase_10 | AC | 173 ms
21,408 KB |
testcase_11 | AC | 177 ms
21,532 KB |
testcase_12 | AC | 163 ms
19,216 KB |
testcase_13 | AC | 94 ms
5,248 KB |
testcase_14 | AC | 89 ms
5,248 KB |
testcase_15 | AC | 233 ms
34,260 KB |
testcase_16 | AC | 209 ms
27,128 KB |
testcase_17 | AC | 138 ms
11,136 KB |
testcase_18 | AC | 289 ms
35,920 KB |
testcase_19 | AC | 208 ms
19,600 KB |
testcase_20 | AC | 289 ms
35,928 KB |
testcase_21 | AC | 267 ms
34,388 KB |
testcase_22 | AC | 275 ms
34,256 KB |
testcase_23 | AC | 149 ms
36,820 KB |
testcase_24 | AC | 124 ms
19,676 KB |
testcase_25 | AC | 135 ms
35,536 KB |
testcase_26 | AC | 136 ms
35,416 KB |
testcase_27 | AC | 222 ms
35,536 KB |
testcase_28 | AC | 235 ms
35,540 KB |
testcase_29 | AC | 252 ms
36,436 KB |
testcase_30 | AC | 261 ms
36,432 KB |
testcase_31 | AC | 399 ms
33,748 KB |
testcase_32 | AC | 379 ms
33,492 KB |
testcase_33 | AC | 338 ms
32,848 KB |
testcase_34 | AC | 236 ms
32,212 KB |
testcase_35 | AC | 220 ms
31,832 KB |
testcase_36 | AC | 224 ms
30,804 KB |
testcase_37 | AC | 323 ms
24,368 KB |
testcase_38 | AC | 330 ms
23,816 KB |
testcase_39 | AC | 334 ms
24,240 KB |
testcase_40 | AC | 319 ms
24,236 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 | 209 ms
36,928 KB |
testcase_45 | AC | 219 ms
37,072 KB |
testcase_46 | AC | 218 ms
37,840 KB |
testcase_47 | AC | 233 ms
37,848 KB |
testcase_48 | AC | 221 ms
37,332 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> log_table; 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), parent(MAX_LOGV, vector<int>(n, -1)), parent_or_size(n, -1), G(n), dp(n), depth(n), diameter(n), diameter_vertex(n), log_table(n) { diameter_vertex[0] = {0, 0}; diameter_vertex[1] = {1, 1}; for(int i = 2; i < N; i++) { diameter_vertex[i] = {i, i}; log_table[i] = log_table[i / 2] + 1; } } int leader(const 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(const int &v, const int &par, const int &w){ parent[0][v] = par; int depth_log = log_table[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(const int &x, const 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++){ int v1 = diameter_vertex[x][i]; for(int j = 0; j < 2; j++){ int v2 = diameter_vertex[y][j]; ll d = dist(v1, v2); if(d > mx){ mx = d; u = v1, v = v2; } } } 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[log_table[d & -d]][v]; d -= d & -d; } if(u == v) return res - 2 * dp[v]; for(int i = log_table[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; } } }