結果

問題 No.2296 Union Path Query (Hard)
ユーザー KudeKude
提出日時 2024-04-21 02:37:26
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 7,000 ms
コード長 3,036 bytes
コンパイル時間 4,319 ms
コンパイル使用メモリ 310,436 KB
実行使用メモリ 28,440 KB
最終ジャッジ日時 2024-04-21 02:37:49
合計ジャッジ時間 20,033 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 10 ms
15,780 KB
testcase_02 AC 10 ms
15,648 KB
testcase_03 AC 9 ms
15,732 KB
testcase_04 AC 148 ms
19,784 KB
testcase_05 AC 170 ms
19,620 KB
testcase_06 AC 191 ms
21,868 KB
testcase_07 AC 167 ms
23,444 KB
testcase_08 AC 175 ms
23,416 KB
testcase_09 AC 141 ms
15,772 KB
testcase_10 AC 151 ms
15,696 KB
testcase_11 AC 159 ms
15,872 KB
testcase_12 AC 152 ms
14,336 KB
testcase_13 AC 88 ms
6,944 KB
testcase_14 AC 83 ms
6,940 KB
testcase_15 AC 198 ms
22,680 KB
testcase_16 AC 183 ms
18,688 KB
testcase_17 AC 127 ms
8,960 KB
testcase_18 AC 238 ms
24,900 KB
testcase_19 AC 186 ms
14,720 KB
testcase_20 AC 275 ms
24,840 KB
testcase_21 AC 229 ms
22,996 KB
testcase_22 AC 217 ms
22,724 KB
testcase_23 AC 124 ms
25,264 KB
testcase_24 AC 114 ms
14,296 KB
testcase_25 AC 136 ms
26,872 KB
testcase_26 AC 121 ms
26,576 KB
testcase_27 AC 185 ms
26,732 KB
testcase_28 AC 192 ms
26,884 KB
testcase_29 AC 200 ms
28,440 KB
testcase_30 AC 195 ms
28,428 KB
testcase_31 AC 263 ms
26,688 KB
testcase_32 AC 238 ms
26,344 KB
testcase_33 AC 220 ms
24,568 KB
testcase_34 AC 170 ms
22,692 KB
testcase_35 AC 169 ms
22,136 KB
testcase_36 AC 238 ms
19,628 KB
testcase_37 AC 216 ms
21,252 KB
testcase_38 AC 240 ms
21,248 KB
testcase_39 AC 228 ms
21,360 KB
testcase_40 AC 225 ms
21,396 KB
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 187 ms
25,424 KB
testcase_45 AC 169 ms
25,104 KB
testcase_46 AC 173 ms
26,388 KB
testcase_47 AC 198 ms
27,640 KB
testcase_48 AC 180 ms
26,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    }
  }
}
0