結果

問題 No.2296 Union Path Query (Hard)
ユーザー shkiiii_shkiiii_
提出日時 2024-04-29 10:20:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 861 ms / 7,000 ms
コード長 3,671 bytes
コンパイル時間 5,153 ms
コンパイル使用メモリ 375,480 KB
実行使用メモリ 71,372 KB
最終ジャッジ日時 2024-04-29 10:20:44
合計ジャッジ時間 30,182 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 69 ms
58,684 KB
testcase_02 AC 65 ms
58,692 KB
testcase_03 AC 66 ms
58,624 KB
testcase_04 AC 354 ms
62,712 KB
testcase_05 AC 329 ms
62,464 KB
testcase_06 AC 396 ms
64,768 KB
testcase_07 AC 369 ms
66,404 KB
testcase_08 AC 367 ms
66,384 KB
testcase_09 AC 316 ms
40,152 KB
testcase_10 AC 306 ms
40,320 KB
testcase_11 AC 316 ms
40,192 KB
testcase_12 AC 290 ms
35,712 KB
testcase_13 AC 125 ms
6,528 KB
testcase_14 AC 111 ms
5,376 KB
testcase_15 AC 467 ms
65,820 KB
testcase_16 AC 404 ms
50,944 KB
testcase_17 AC 224 ms
19,712 KB
testcase_18 AC 520 ms
67,700 KB
testcase_19 AC 386 ms
36,096 KB
testcase_20 AC 525 ms
67,724 KB
testcase_21 AC 491 ms
65,888 KB
testcase_22 AC 485 ms
65,664 KB
testcase_23 AC 310 ms
68,124 KB
testcase_24 AC 232 ms
35,656 KB
testcase_25 AC 316 ms
69,632 KB
testcase_26 AC 308 ms
69,760 KB
testcase_27 AC 635 ms
69,632 KB
testcase_28 AC 630 ms
69,572 KB
testcase_29 AC 659 ms
71,372 KB
testcase_30 AC 680 ms
71,168 KB
testcase_31 AC 858 ms
70,224 KB
testcase_32 AC 839 ms
69,760 KB
testcase_33 AC 766 ms
67,968 KB
testcase_34 AC 590 ms
65,920 KB
testcase_35 AC 508 ms
65,200 KB
testcase_36 AC 463 ms
62,640 KB
testcase_37 AC 792 ms
50,048 KB
testcase_38 AC 861 ms
49,920 KB
testcase_39 AC 829 ms
49,920 KB
testcase_40 AC 803 ms
50,048 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 424 ms
68,252 KB
testcase_45 AC 412 ms
68,224 KB
testcase_46 AC 455 ms
69,376 KB
testcase_47 AC 474 ms
70,400 KB
testcase_48 AC 440 ms
68,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
#include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}


class UnionFind{
  public:
  vector<int> par;
  UnionFind(long long size) : par(size,-1){}

  bool unite(int x,int y){
    x = root(x),y = root(y);
    if(x == y)return false;
    if(par[y] < par[x])swap(x,y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  bool find(int x,int y){
    return root(x) == root(y);
  }

  int root(int x){
    return par[x] < 0 ? x : par[x] = root(par[x]);
  }

  int size(int x){
    return -par[root(x)];
  }
};


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  ll n,X,Q;
  cin >> n >> X >> Q;
  vi d(n),D(n);
  vvi nxt(n,vi(19));
  rep(i,n)rep(j,19)nxt[i][j] = i;
  UnionFind uf(n);
  vector<vector<P>> v(n);
  vvi cand(n,vi(2));
  rep(i,n)rep(j,2)cand[i][j] = i;
  auto dfs = [&](auto &dfs,int ov,int pre)->void{
    nxt[ov][0] = pre;
    rep(i,18)nxt[ov][i+1] = nxt[nxt[ov][i]][i];
    for(auto [nv,we] : v[ov]){
      if(nv == pre)continue;
      d[nv] = d[ov] + 1;
      D[nv] = D[ov] + we;
      dfs(dfs,nv,ov);
    }
  };
  auto LCA = [&](ll a,ll b)->ll{
    if(d[a] < d[b])swap(a,b);
    for(int i = 18;i >= 0;i--)if(d[nxt[a][i]] >= d[b])a = nxt[a][i];
    if(a == b)return a;
    for(int i = 18;i >= 0;i--)if(nxt[a][i] != nxt[b][i])a = nxt[a][i],b = nxt[b][i];
    return nxt[a][0];
  };
  auto dist = [&](ll a,ll b)->ll{
    if(!uf.find(a,b))return -1;
    return D[a] + D[b]-2*D[LCA(a,b)];
  };
  auto ch = [&](ll a,ll b)->void{
    ll mx = dist(cand[a][0],cand[a][1]);
    P k = {cand[a][0],cand[a][1]};
    if(dist(cand[b][0],cand[b][1]) > mx){
      k = {cand[b][0],cand[b][1]};
      mx = dist(cand[b][0],cand[b][1]);
    }
    rep(i,2)rep(j,2){
      if(dist(cand[a][i],cand[b][j]) > mx){
        mx = dist(cand[a][i],cand[b][j]);
        k = {cand[a][i],cand[b][j]};
      }
    }
    cand[b][0] = k.first;
    cand[b][1] = k.second;
  };
  while(Q--){
    ll t;cin >> t;
    if(t == 1){
      ll a,w;cin >> a >> w;
      ll b = X;
      if(uf.size(b) < uf.size(a))swap(b,a);
      D[a] = D[b] + w;
      d[a] = d[b] + 1;
      dfs(dfs,a,b);
      v[a].emplace_back(b,w);
      v[b].emplace_back(a,w);
      a = uf.root(a),b = uf.root(b);
      uf.unite(b,a);
      ch(a,b);
    }else if(t == 2){
      ll a,b;cin >> a >> b;
      ll res = dist(a,b);
      cout << res << "\n";
      if(res != -1)X = (X + res)%n;
    }else if(t == 3){
      ll a;cin >> a;
      a = uf.root(a);
      cout << dist(cand[a][0],cand[a][1]) << "\n";
    }else{
      ll val;cin >> val;
      X = (X + val)%n;
    }
  }
  

  return 0;
}
0