/
9158.test.cpp
73 lines (64 loc) · 1.29 KB
/
9158.test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// verification-helper: PROBLEM https://yukicoder.me/problems/9158
#include<bits/stdc++.h>
using namespace std;
#define call_from_test
#include "../../datastructure/unionfind.cpp"
#include "../../toptree/toptree.cpp"
#include "../../toptree/farthest.cpp"
#undef call_from_test
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
using ll = long long;
const char newl = '\n';
const size_t N = 2e5+10;
using Cluster = Farthest<ll>;
TopTree<Vertex, Cluster, N> G;
int n;
ll x;
int q;
cin>>n>>x>>q;
vector<Vertex*> vs(n);
for(int i=0;i<n;i++) vs[i]=G.create();
UnionFind uf(n);
for(int t=0;t<q;t++){
int k;
cin>>k;
if(k==1){
int v;
ll w;
cin>>v>>w;
G.link(vs[v],Cluster(w,v,x),vs[x]);
uf.unite(v,x);
}
if(k==2){
int u,v;
cin>>u>>v;
if(uf.same(u,v)){
ll len=(u==v?0:G.get_path(vs[u],vs[v]).len);
cout<<len<<newl;
x+=len;
x%=n;
}else{
cout<<-1<<newl;
}
}
if(k==3){
int v;
cin>>v;
auto p=G.get_subtree(vs[v]).md;
if(p.dist==0){
cout<<0<<newl;
}else{
cout<<G.get_subtree(vs[p.idx]).md.dist<<newl;
}
}
if(k==4){
int value;
cin>>value;
x+=value;
x%=n;
}
}
return 0;
}