#include #include #include #include #include #include #include using namespace std; typedef long long ll; int N, Q; int par[500000], root[500000], leaf[500000], sz[500000], ch[500000]; int T[500000], A[500000], B[500000]; int idx[500000]; template struct LazySegmentTree { int n; T unit; vector node, lazy; vector lazyFlag; T calc(T a, T b){ T ans; ans = max(a, b); //適宜変える return ans; } T lazyEvaluateNode(T node, T lazy){ return node+lazy; //適宜変える } T clearLazy(){ return 0; //適宜変える } LazySegmentTree(vector v, T UNIT) { int sz = (int)v.size(); unit = UNIT; n = 1; while(n < sz) n *= 2; node.resize(2*n-1, 0); lazy.resize(2*n-1, 0); lazyFlag.resize(2*n-1, false); for(int i=0; i=0; i--) node[i] = calc(node[i*2+1], node[i*2+2]); } void lazyEvaluate(int k, int l, int r) { if(lazyFlag[k]) { node[k] = lazyEvaluateNode(node[k], lazy[k]); if(r - l > 1) { lazy[k*2+1] = lazyEvaluateNode(lazy[k*2+1], lazy[k]); lazy[k*2+2] = lazyEvaluateNode(lazy[k*2+2], lazy[k]); lazyFlag[k*2+1] = lazyFlag[k*2+2] = true; } lazyFlag[k] = false; lazy[k] = clearLazy(); } } //[a, b) //区間[a, b]への更新に対してはupdate(a, b+1, x)と呼ぶ void update(int a, int b, T x, int k=0, int l=0, int r=-1) { if(r < 0) r = n; lazyEvaluate(k, l, r); if(b <= l || r <= a) return; if(a <= l && r <= b) { lazy[k] += x; lazyFlag[k] = true; lazyEvaluate(k, l, r); } else { update(a, b, x, 2*k+1, l, (l+r)/2); update(a, b, x, 2*k+2, (l+r)/2, r); node[k] = calc(node[2*k+1], node[2*k+2]); } } //[a, b) //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ T query(int a, int b, int k=0, int l=0, int r=-1) { if(r < 0) r = n; lazyEvaluate(k, l, r); if(b <= l || r <= a) return unit; if(a <= l && r <= b) return node[k]; T vl = query(a, b, 2*k+1, l, (l+r)/2); T vr = query(a, b, 2*k+2, (l+r)/2, r); return calc(vl, vr); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> N >> Q; for(int i = 0; i < Q; i++){ scanf("%d %d %d", &T[i], &A[i], &B[i]); // cin >> T[i] >> A[i] >> B[i]; A[i]--; if(T[i] == 1) B[i]--; // if(T[i] == 3) cout << 'H' << endl; } for(int i = 0; i < N; i++){ root[i] = i; par[i] = i; leaf[i] = i; sz[i] = 1; ch[i] = -1; } for(int i = 0; i < Q; i++){ if(T[i] == 1){ if(root[A[i]] == root[B[i]]) continue; if(sz[root[A[i]]] > sz[root[B[i]]]){ int tmp = leaf[B[i]]; int sum = sz[root[A[i]]]+sz[root[B[i]]]; par[root[B[i]]] = leaf[root[A[i]]]; leaf[root[A[i]]] = leaf[root[B[i]]]; while(root[tmp] != root[A[i]]){ root[tmp] = root[A[i]]; tmp = par[tmp]; } // cout << A[i] << ' ' << B[i] << ' ' << root[B[i]] << ' ' << leaf[root[B[i]]] << endl; }else{ int tmp = leaf[A[i]]; int sum = sz[root[A[i]]]+sz[root[B[i]]]; par[root[A[i]]] = leaf[root[B[i]]]; leaf[root[B[i]]] = leaf[root[A[i]]]; while(root[tmp] != root[B[i]]){ root[tmp] = root[B[i]]; tmp = par[tmp]; } // cout << A[i] << ' ' << B[i] << ' ' << root[A[i]] << ' ' << leaf[root[A[i]]] << endl; } } } for(int i = 0; i < N; i++){ if(par[i] != i) ch[par[i]] = i; } int cur = 0; for(int i = 0; i < N; i++){ if(par[i] == i){ int tmp = i; idx[tmp] = cur; cur++; while(ch[tmp] != -1){ tmp = ch[tmp]; idx[tmp] = cur; cur++; } } } // cout << "-----" << endl; // for(int i = 0; i < N; i++) cout << par[i] << ' '; // cout << endl; // for(int i = 0; i < N; i++) cout << ch[i] << ' '; // cout << endl; // for(int i = 0; i < N; i++) cout << idx[i] << ' '; // cout << endl; vector v(N, 0); LazySegmentTree sgt(v, -1e9); for(int i = 0; i < N; i++){ root[i] = i; par[i] = i; leaf[i] = i; sz[i] = 1; ch[i] = -1; } for(int i = 0; i < Q; i++){ if(T[i] == 1){ if(root[A[i]] == root[B[i]]) continue; if(sz[root[A[i]]] > sz[root[B[i]]]){ int tmp = leaf[B[i]]; int sum = sz[root[A[i]]]+sz[root[B[i]]]; par[root[B[i]]] = leaf[root[A[i]]]; leaf[root[A[i]]] = leaf[root[B[i]]]; while(root[tmp] != root[A[i]]){ root[tmp] = root[A[i]]; tmp = par[tmp]; } // cout << A[i] << ' ' << B[i] << ' ' << root[B[i]] << ' ' << leaf[root[B[i]]] << endl; }else{ int tmp = leaf[A[i]]; int sum = sz[root[A[i]]]+sz[root[B[i]]]; par[root[A[i]]] = leaf[root[B[i]]]; leaf[root[B[i]]] = leaf[root[A[i]]]; while(root[tmp] != root[B[i]]){ root[tmp] = root[B[i]]; tmp = par[tmp]; } // cout << A[i] << ' ' << B[i] << ' ' << root[A[i]] << ' ' << leaf[root[A[i]]] << endl; } } if(T[i] == 2){ int l = idx[root[A[i]]]; int r = idx[leaf[root[A[i]]]]; // cout << B[i] << ' ' << l << '-' << r << endl; sgt.update(l, r+1, B[i]); } if(T[i] == 3){ int l = idx[A[i]]; cout << sgt.query(l, l+1) << endl; } } }