結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー |
|
提出日時 | 2023-05-05 23:12:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,533 bytes |
コンパイル時間 | 978 ms |
コンパイル使用メモリ | 84,260 KB |
実行使用メモリ | 81,792 KB |
最終ジャッジ日時 | 2024-11-23 11:42:33 |
合計ジャッジ時間 | 49,453 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 44 TLE * 1 |
ソースコード
#include<iostream> #include<vector> #include<set> #include<cassert> using namespace std; int N,X,Q; vector<pair<int,int> >G[2<<17]; int par[2<<17]; int prD[2<<17]; int pr[18][2<<17],depth[2<<17]; long long D[2<<17]; set<pair<long long,int> >mD[2<<17]; vector<int>V[2<<17]; long long ans[2<<17]; long long dfs(int u,int p,int dep,long long dist,int prd,int AP) { par[u]=AP; pr[0][u]=p; for(int k=1;k<18;k++) { if(pr[k-1][u]==-1)pr[k][u]=-1; else pr[k][u]=pr[k-1][pr[k-1][u]]; } depth[u]=dep; D[u]=dist; prD[u]=prd; V[AP].push_back(u); mD[u].clear(); long long md=0; for(pair<int,int>e:G[u])if(e.first!=p) { long long d=dfs(e.first,u,dep+1,dist+e.second,e.second,AP)+e.second; mD[u].insert(make_pair(d,e.first)); md=max(md,d); } return md; } int lca(int u,int v) { if(depth[u]>depth[v])swap(u,v); for(int k=0;k<18;k++)if(depth[v]-depth[u]>>k&1)v=pr[k][v]; if(u==v)return u; for(int k=18;k--;)if(pr[k][u]!=pr[k][v]) { u=pr[k][u]; v=pr[k][v]; } return pr[0][u]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin>>N>>X>>Q; for(int i=0;i<N;i++) { V[i].push_back(i); for(int k=0;k<18;k++)pr[k][i]=-1; par[i]=i; } for(;Q--;) { int op;cin>>op; if(op==1) { int v,w;cin>>v>>w; int u=X; if(V[par[u]].size()<V[par[v]].size())swap(u,v); ans[par[u]]=max(ans[par[u]],ans[par[v]]); long long nd=dfs(v,u,depth[u]+1,D[u]+w,w,par[u])+w; if(mD[u].empty()) { ans[par[u]]=max(ans[par[u]],nd); } else { auto it=mD[u].end();it--; ans[par[u]]=max(ans[par[u]],it->first+nd); } mD[u].insert(make_pair(nd,v)); { int x=u; while(pr[0][x]!=-1) { int y=pr[0][x]; auto it=mD[x].end();it--; while(!mD[y].empty()&&mD[y].rbegin()->second==x) { auto jt=mD[y].end();jt--; mD[y].erase(jt); } if(mD[y].empty()) { ans[par[u]]=max(ans[par[u]],it->first+prD[x]); } else { auto jt=mD[y].end(); jt--; assert(jt->second!=x); ans[par[u]]=max(ans[par[u]],it->first+prD[x]+jt->first); } mD[y].insert(make_pair(it->first+prD[x],x)); x=y; } } G[u].push_back(make_pair(v,w)); G[v].push_back(make_pair(u,w)); } else if(op==2) { int u,v;cin>>u>>v; if(par[u]!=par[v])cout<<"-1\n"; else { int w=lca(u,v); long long d=D[u]+D[v]-2*D[w]; cout<<d<<"\n"; X=(X+d%N)%N; } } else if(op==3) { int v;cin>>v; v=par[v]; cout<<ans[v]<<"\n"; } else { int v;cin>>v; X=(X+v)%N; } } }