結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | Kude |
提出日時 | 2024-04-21 02:37:26 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 373 ms / 7,000 ms |
コード長 | 3,036 bytes |
コンパイル時間 | 3,580 ms |
コンパイル使用メモリ | 285,708 KB |
実行使用メモリ | 28,288 KB |
最終ジャッジ日時 | 2024-10-13 01:06:18 |
合計ジャッジ時間 | 20,317 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 10 ms
15,744 KB |
testcase_02 | AC | 11 ms
15,600 KB |
testcase_03 | AC | 11 ms
15,692 KB |
testcase_04 | AC | 160 ms
19,584 KB |
testcase_05 | AC | 163 ms
19,704 KB |
testcase_06 | AC | 211 ms
21,840 KB |
testcase_07 | AC | 200 ms
23,416 KB |
testcase_08 | AC | 195 ms
23,416 KB |
testcase_09 | AC | 176 ms
15,616 KB |
testcase_10 | AC | 173 ms
15,616 KB |
testcase_11 | AC | 183 ms
15,744 KB |
testcase_12 | AC | 173 ms
14,208 KB |
testcase_13 | AC | 94 ms
5,248 KB |
testcase_14 | AC | 90 ms
5,248 KB |
testcase_15 | AC | 224 ms
22,912 KB |
testcase_16 | AC | 217 ms
18,724 KB |
testcase_17 | AC | 140 ms
8,960 KB |
testcase_18 | AC | 285 ms
24,704 KB |
testcase_19 | AC | 214 ms
14,464 KB |
testcase_20 | AC | 267 ms
24,784 KB |
testcase_21 | AC | 265 ms
23,024 KB |
testcase_22 | AC | 274 ms
22,656 KB |
testcase_23 | AC | 141 ms
25,152 KB |
testcase_24 | AC | 123 ms
14,168 KB |
testcase_25 | AC | 130 ms
26,624 KB |
testcase_26 | AC | 127 ms
26,712 KB |
testcase_27 | AC | 259 ms
26,732 KB |
testcase_28 | AC | 268 ms
26,752 KB |
testcase_29 | AC | 310 ms
28,288 KB |
testcase_30 | AC | 283 ms
28,288 KB |
testcase_31 | AC | 339 ms
26,728 KB |
testcase_32 | AC | 320 ms
26,272 KB |
testcase_33 | AC | 322 ms
24,448 KB |
testcase_34 | AC | 220 ms
22,656 KB |
testcase_35 | AC | 212 ms
22,088 KB |
testcase_36 | AC | 231 ms
19,500 KB |
testcase_37 | AC | 344 ms
21,376 KB |
testcase_38 | AC | 373 ms
21,376 KB |
testcase_39 | AC | 339 ms
21,376 KB |
testcase_40 | AC | 319 ms
21,376 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 | 198 ms
25,416 KB |
testcase_45 | AC | 197 ms
25,216 KB |
testcase_46 | AC | 208 ms
26,368 KB |
testcase_47 | AC | 220 ms
27,520 KB |
testcase_48 | AC | 204 ms
26,080 KB |
ソースコード
#include<bits/stdc++.h> namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include<atcoder/all> #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair<int,int>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<ll>; using VVL = vector<VL>; struct Node { int depth, parent, jump; }; struct Diam { int u, v; ll d; friend bool operator<(const Diam& lhs, const Diam& rhs) { return lhs.d < rhs.d; } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, x, q; cin >> n >> x >> q; vector<vector<pair<int, ll>>> g(n); VL ws(n); vector<Node> nodes(n); rep(i, n) nodes[i] = {0, i, i}; vector<Diam> diam(n); rep(i, n) diam[i] = {i, i, 0}; dsu uf(n); auto dist = [&](int u, int v) { ll res = ws[u] + ws[v]; if (nodes[u].depth < nodes[v].depth) swap(u, v); while (nodes[u].depth != nodes[v].depth) { int uj = nodes[u].jump; if (nodes[uj].depth >= nodes[v].depth) u = uj; else u = nodes[u].parent; } while (u != v) { int uj = nodes[u].jump, vj = nodes[v].jump; if (uj != vj) u = uj, v = vj; else u = nodes[u].parent, v = nodes[v].parent; } res -= 2 * ws[u]; return res; }; while (q--) { int type; cin >> type; if (type == 1) { int v, w; cin >> v >> w; int u = x; if (uf.size(u) > uf.size(v)) swap(u, v); int lu = uf.leader(u), lv = uf.leader(v); int l = uf.merge(u, v); auto dfs = [&](auto&& self, int u, int p, ll w_now) -> void { int pj = nodes[p].jump, pjj = nodes[pj].jump; nodes[u] = { nodes[p].depth + 1, p, nodes[p].depth + nodes[pjj].depth == 2 * nodes[pj].depth ? pjj : p }; ws[u] = w_now; for (auto [vi, wi] : g[u]) if (vi != p) self(self, vi, u, w_now + wi); }; dfs(dfs, u, v, w + ws[v]); g[u].emplace_back(v, w); g[v].emplace_back(u, w); Diam d = max(diam[lu], diam[lv]); for (int u : {diam[lu].u, diam[lu].v}) { for (int v : {diam[lv].u, diam[lv].v}) { chmax(d, Diam{u, v, dist(u, v)}); } } diam[l] = d; } else if (type == 2) { int u, v; cin >> u >> v; if (!uf.same(u, v)) { cout << -1 << '\n'; } else { ll ans = dist(u, v); cout << ans << '\n'; x = (x + ans) % n; } } else if (type == 3) { int v; cin >> v; cout << diam[uf.leader(v)].d << '\n'; } else { int value; cin >> value; x = (x + value) % n; } } }