#include using namespace std; using ll = long long; template struct Multiple_LCA_Tree{ int MAX_LOGV, N; std::vector>> G; //重み付きグラフ std::vector> parent; //親の頂点のダブリング配列 std::vector> dp; //辺の重みのダブリング配列 std::vector depth; //連結成分の代表元からの深さ std::vector parent_or_size; //Union Find std::vector> diameter_and_vertex; //直径と頂点を入れる std::queue que; //BFS用 Multiple_LCA_Tree(int _n) : N(_n){ MAX_LOGV = std::__lg(N) + 1; G.resize(N); parent.resize(MAX_LOGV, std::vector(N, -1)); dp.resize(MAX_LOGV, std::vector(N, e())); depth.resize(N); parent_or_size.resize(N, -1); diameter_and_vertex.resize(N); for(int i = 0; i < N; i++) diameter_and_vertex[i] = std::make_tuple(0, i, i); } int leader(int v){ if(parent_or_size[v] < 0) return v; return parent_or_size[v] = leader(parent_or_size[v]); } //頂点uと頂点vを重さwの辺で結ぶ void merge(int u, int v, S w){ int x = leader(u), y = leader(v); if(x == y) return; if(-parent_or_size[x] < -parent_or_size[y]) { std::swap(x, y); std::swap(u, v); } //x-u の連結成分側を親とする parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; //辺を追加してDFSでダブリングを更新 G[u].emplace_back(v, w); G[v].emplace_back(u, w); int lim_logv = std::__lg(-parent_or_size[x]) + 1; register_parent(v, u, w); que.push(v); while(!que.empty()){ v = que.front(); update_dp_table(v, lim_logv); que.pop(); for(auto &edge : G[v]){ std::tie(u, w) = edge; if(parent[0][v] == u) continue; register_parent(u, v, w); que.push(u); } } update_diameter(x, y); } S dist(int u, int v){ if(leader(u) != leader(v)) return -1ll; if(depth[u] > depth[v]) std::swap(u, v); S result = e(); //頂点 v を頂点 u と高さが同じになるようにする for(int i = 0; i < MAX_LOGV; i++){ if((depth[v] - depth[u]) >> i & 1){ result = op(result, dp[i][v]); v = parent[i][v]; } } if(u == v) return result; for(int i = MAX_LOGV - 1; i >= 0; i--){ if(parent[i][u] != parent[i][v]){ result = op(result, dp[i][u]); result = op(result, dp[i][v]); u = parent[i][u]; v = parent[i][v]; } } result = op(result, dp[0][u]); result = op(result, dp[0][v]); return result; } private: void update_dp_table(int v, int lim_logv){ for(int i = 0; i + 1 < lim_logv; i++){ if(parent[i][v] == -1) { parent[i + 1][v] = -1; dp[i + 1][v] = dp[i][v]; } else { parent[i + 1][v] = parent[i][parent[i][v]]; dp[i + 1][v] = op(dp[i][parent[i][v]], dp[i][v]); } } } void register_parent(int v, int _parent, S weight){ dp[0][v] = weight; depth[v] = depth[_parent] + 1; parent[0][v] = _parent; } //yの連結成分とxの連結成分の直径を更新 void update_diameter(int x, int y){ S temp; std::array candidate_vertex{}; std::tie(temp, candidate_vertex[0], candidate_vertex[1]) = diameter_and_vertex[x]; std::tie(temp, candidate_vertex[2], candidate_vertex[3]) = diameter_and_vertex[y]; std::tuple result = std::max(diameter_and_vertex[x], diameter_and_vertex[y]); for(int i = 0; i < 2; i++) { int v1 = candidate_vertex[i]; for(int j = 2; j < 4; j++) { int v2 = candidate_vertex[j]; S length = dist(v1, v2); result = std::max(result, std::make_tuple(length, v1, v2)); } } diameter_and_vertex[x] = result; } }; ll op(ll lhs, ll rhs){ return lhs + rhs; } ll e() { return 0ll; } int main(){ ios::sync_with_stdio(false); cin.tie(0); ll N, X, Q; cin >> N >> X >> Q; Multiple_LCA_Tree MLT(N); while(Q--){ int type, v, u, w; cin >> type; if(type == 1){ cin >> v >> w; MLT.merge(v, X, w); }else if(type == 2){ cin >> u >> v; ll d = MLT.dist(u, v); cout << d << '\n'; if(d != -1) (X += d) %= N; }else if(type == 3){ cin >> v; cout << get<0>(MLT.diameter_and_vertex[MLT.leader(v)]) << '\n'; }else{ cin >> v; (X += v) %= N; } } }