#include using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b par; vector siz; int c;//親の個数(グループ数) void init(int n){ c=n; for(int i=0;i struct LazySegmentTree{ // Monoid: 要素 OperatorMonoid: 作用素 // 各マージ関数 using F=function; using G=function; using H=function; int sz,height; vector data; vector lazy; const F f; const G g; const H h; // 単位元 const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n,const F f,const G g,const H h,const Monoid &M1,const OperatorMonoid OM0) : f(f),g(g),h(h),M1(M1),OM0(OM0) { sz=1;height=0; while(sz0;k--) data[k]=f(data[2*k+0],data[2*k+1]); } //伝播させる inline void propagate(int k){ if(lazy[k]!=OM0){ //子に伝えた後にkの場所のデータのほうの更新 lazy[2*k+0]=h(lazy[2*k+0],lazy[k]); lazy[2*k+1]=h(lazy[2*k+1],lazy[k]); data[k]=reflect(k); lazy[k]=OM0; } } inline Monoid reflect(int k){ return lazy[k]==OM0?data[k]:g(data[k],lazy[k]); } inline void recalc(int k){ //下から上にdataの値を計算しなおす while(k>>=1)data[k]=f(reflect(2*k+0),reflect(2*k+1)); } inline void thrust(int k){ for(int i=height;i>0;i--) propagate(k>>i); } void update(int a,int b,const OperatorMonoid &x){ thrust(a+=sz); thrust(b+=sz-1); for(int l=a,r=b+1;l>=1,r>>=1){ if(l&1) lazy[l]=h(lazy[l],x),l++; if(r&1) --r,lazy[r]=h(lazy[r],x); } //recalcすればpが要らない recalc(a); recalc(b); } Monoid query(int a,int b){ thrust(a+=sz); thrust(b+=sz-1); Monoid L=M1,R=M1; for(int l=a,r=b+1;l>=1,r>>=1){ if(l&1) L=f(L,reflect(l++)); if(r&1) R=f(reflect(--r),R); } return f(L,R); } Monoid operator[](const int &k){ return query(k,k+1); } }; ll segf(ll a,ll b){ return a>b?a:b; } ll segg(ll a,ll b){ return a+b; } ll segh(ll a,ll b){ return a+b; } struct query{ int t,a,b; query(int t,int a,int b):t(t),a(a),b(b){} query(){} }; signed main(){ cin.tie(0); ios::sync_with_stdio(0); int n,q;cin>>n>>q; vector qs; UnionFind uf(n); vector> g(n); rep(i,n) g[i].push_back(i); rep(i,q){ int t,a,b;cin>>t>>a>>b; a--; if(t==1) b--; if(t==1){ if(!uf.sameParent(a,b)){ int pa=uf.root(a),pb=uf.root(b); uf.unite(a,b); if(g[pa].size() v,id(n); rep(i,n){ for(auto x:g[i]) v.push_back(x); } rep(i,n) id[v[i]]=i; // debug(v); //ready set st; rep(i,n+1) st.insert(i); LazySegmentTree seg(n,segf,segg,segh,-LINF,0ll); rep(i,n)seg.set(i,0); seg.build(); UnionFind uf2(n); for(auto &qu:qs){ if(qu.t==3){ // ans cout<