結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | shkiiii_ |
提出日時 | 2024-04-29 10:12:00 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 981 ms / 7,000 ms |
コード長 | 3,874 bytes |
コンパイル時間 | 6,384 ms |
コンパイル使用メモリ | 377,068 KB |
実行使用メモリ | 58,752 KB |
最終ジャッジ日時 | 2024-11-18 12:49:14 |
合計ジャッジ時間 | 34,814 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
46,180 KB |
testcase_01 | AC | 53 ms
46,160 KB |
testcase_02 | AC | 53 ms
46,188 KB |
testcase_03 | AC | 53 ms
46,256 KB |
testcase_04 | AC | 403 ms
50,132 KB |
testcase_05 | AC | 366 ms
50,176 KB |
testcase_06 | AC | 472 ms
52,344 KB |
testcase_07 | AC | 401 ms
53,872 KB |
testcase_08 | AC | 409 ms
53,888 KB |
testcase_09 | AC | 359 ms
51,344 KB |
testcase_10 | AC | 353 ms
51,504 KB |
testcase_11 | AC | 357 ms
51,456 KB |
testcase_12 | AC | 352 ms
50,888 KB |
testcase_13 | AC | 177 ms
46,696 KB |
testcase_14 | AC | 173 ms
46,464 KB |
testcase_15 | AC | 481 ms
53,196 KB |
testcase_16 | AC | 453 ms
52,480 KB |
testcase_17 | AC | 295 ms
48,768 KB |
testcase_18 | AC | 550 ms
55,276 KB |
testcase_19 | AC | 450 ms
51,328 KB |
testcase_20 | AC | 559 ms
55,136 KB |
testcase_21 | AC | 525 ms
53,436 KB |
testcase_22 | AC | 531 ms
53,120 KB |
testcase_23 | AC | 292 ms
55,628 KB |
testcase_24 | AC | 274 ms
50,884 KB |
testcase_25 | AC | 308 ms
57,216 KB |
testcase_26 | AC | 309 ms
57,088 KB |
testcase_27 | AC | 704 ms
57,216 KB |
testcase_28 | AC | 708 ms
57,120 KB |
testcase_29 | AC | 776 ms
58,752 KB |
testcase_30 | AC | 742 ms
58,752 KB |
testcase_31 | AC | 950 ms
57,600 KB |
testcase_32 | AC | 981 ms
57,176 KB |
testcase_33 | AC | 851 ms
55,552 KB |
testcase_34 | AC | 670 ms
53,504 KB |
testcase_35 | AC | 604 ms
52,736 KB |
testcase_36 | AC | 519 ms
50,176 KB |
testcase_37 | AC | 906 ms
56,704 KB |
testcase_38 | AC | 900 ms
56,704 KB |
testcase_39 | AC | 889 ms
56,668 KB |
testcase_40 | AC | 931 ms
56,640 KB |
testcase_41 | AC | 49 ms
46,144 KB |
testcase_42 | AC | 49 ms
46,208 KB |
testcase_43 | AC | 50 ms
46,208 KB |
testcase_44 | AC | 417 ms
55,880 KB |
testcase_45 | AC | 418 ms
55,680 KB |
testcase_46 | AC | 417 ms
56,832 KB |
testcase_47 | AC | 466 ms
58,004 KB |
testcase_48 | AC | 428 ms
56,528 KB |
ソースコード
#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)]; } }; const int LG_N = 19; const int MX_N = 200000; vi D(MX_N);//重み付き vi d(MX_N);//辺の数 vector<vector<int>> nxt(MX_N,vector<int>(LG_N)); vector<vector<pair<ll,ll>>> v(MX_N); vector<vector<int>> cand(MX_N,vector<int>(2)); UnionFind uf(MX_N); inline void dfs(ll ov,ll pre){ nxt[ov][0] = pre; rep(i,LG_N-1)nxt[ov][i+1] = nxt[nxt[ov][i]][i]; for(auto [nv,we] : v[ov]){ if(nv == pre)continue; D[nv] = D[ov] + we; d[nv] = d[ov] + 1; dfs(nv,ov); } } inline int LCA(int a,int b){ if(d[a] < d[b])swap(a,b); for(int i = LG_N-1;i >= 0;i--)if(d[nxt[a][i]] >= d[b])a = nxt[a][i]; if(a == b)return a; for(int i = LG_N-1;i >= 0;i--)if(nxt[a][i] != nxt[b][i])a = nxt[a][i],b = nxt[b][i]; return nxt[a][0]; } inline ll dist(int a,int b){ if(!uf.find(a,b))return -1; return D[a] + D[b] - 2*D[LCA(a,b)]; } inline void ch(ll a,ll b){ ll mx = dist(cand[a][0],cand[a][1]); pair<int,int> k = {cand[a][0],cand[a][1]}; ll l = dist(cand[b][0],cand[b][1]); if(l > mx){ k = {cand[b][0],cand[b][1]}; mx = l; } rep(i,2)rep(j,2){ l = dist(cand[a][i],cand[b][j]); if(l > mx){ mx = l; k = {cand[a][i],cand[b][j]}; } } cand[b][0] = k.first; cand[b][1] = k.second; cand[a][0] = k.first; cand[a][1] = k.second; } int main(){ ios_base::sync_with_stdio(0), cin.tie(0); ll n,X,Q; cin >> n >> X >> Q; rep(i,n)rep(j,LG_N)nxt[i][j] = i; rep(i,n)rep(j,2)cand[i][j] = i; 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); assert(uf.size(b) >= uf.size(a)); assert(!uf.find(a,b)); d[a] = d[b] + 1; D[a] = D[b] + w; 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){ int a,b;cin >> a >> b; ll res = dist(a,b); cout << res << "\n"; if(res != -1)X = (X + res%n)%n; }else if(t == 3){ int 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; } } // cout << 1.0l*clock()/CLOCKS_PER_SEC << "\n"; return 0; }