#line 1 "c.cpp" #include #include #define rep(i,n) for (int i = 0; i < int(n); ++i) #define repp(i,n,m) for (int i = m; i < int(n); ++i) #define repb(i,n) for (int i = int(n)-1; i >= 0; --i) #define all(v) v.begin(),v.end() using namespace std; using namespace atcoder; using ll = long long; using ld = long double; using P = pair; using PL = pair; using pdd = pair; using pil = pair; using pli = pair; using ppi = pair; using pip = pair; const int INF = 1001001007; const long long mod1 = 1000000007LL; const long long mod2 = 998244353LL; const ll inf = 2e18; const ld pi = 3.14159265358979323; const ld eps = 1e-7; templatevoid o(T a); templateistream &operator>>(istream &is,vector &v){for(auto &e:v)is>>e;return is;} templatebool range(T a,T b,T x){return (a<=x&&xbool rrange(T a,T b,T c,T d,T x,T y){return (range(a,c,x)&&range(b,d,y));} templatevoid rev(vector &v){reverse(v.begin(),v.end());} void revs(string &s) {reverse(s.begin(),s.end());} templatevoid sor(vector &v, int f=0){sort(v.begin(),v.end());if(f!=0) rev(v);} templatebool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;} templatebool chmax(T &a,const T &b){if(avoid uniq(vector &v){sor(v);v.erase(unique(v.begin(),v.end()),v.end());} templateT cel(T a,T b){return (a+b-1)/b;} templatevoid print(pair a); templatevoid print(vector v); templatevoid print(vector> v); void print(){ putchar(' '); } void print(bool a){ printf("%d", a); } void print(int a){ printf("%d", a); } void print(long a){ printf("%ld", a); } void print(long long a){ printf("%lld", a); } void print(char a){ printf("%c", a); } void print(char a[]){ printf("%s", a); } void print(const char a[]){ printf("%s", a); } void print(long double a){ printf("%.15Lf", a); } void print(const string& a){ for(auto&& i : a) print(i); } void print(unsigned int a){ printf("%u", a); } template void print(const T& a){ cout << a; } int out(){ putchar('\n'); return 0; } template int out(const T& t){ print(t); putchar('\n'); return 0; } template int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; } void o(){cout<<"!?"<void o(T a){cout<void print(pair a){print(a.first);print(),print(a.second);} templatevoid print(vector v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())print();}} templatevoid print(vector> v){for(auto ite=v.begin();ite!=v.end();){print(*ite);if(++ite!=v.end())out();}} void yes(){out("Yes");} void no (){out("No");} void yn (bool t){if(t)yes();else no();} vector dx = {0,1,0,-1,1,1,-1,-1}; vector dy = {1,0,-1,0,1,-1,-1,1}; const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string alp = "abcdefghijklmnopqrstuvwxyz"; const string NUM = "0123456789"; void fast_io(){cin.tie(0); ios::sync_with_stdio(0); cout< a){vector b(a.size()); rep(i,a.size()) b[i] = a[i].val(); out(b);} void out(vector> a){for (auto v : a) out(v);} istream &operator>>(istream &is,vector &v){for(auto &e:v){ll _x;is>>_x;e=_x;}return is;} #line 2 "UFtree.hpp" #line 4 "UFtree.hpp" namespace noya2 { struct UFtree{ int n, m; std::vector rot, par; UFtree (int _n = 0) : n(_n) {init();} int parent(int u){ if (rot[u] < 0) return u; return rot[u] = parent(rot[u]); } bool same(int u, int v){ return parent(u) == parent(v); } int merge(int u, int v){ if (same(u,v)) return -1; u = parent(u); v = parent(v); rot[u] = n+m; rot[v] = n+m; par[u] = n+m; par[v] = n+m; return m++; } void init(){ rot.resize(n+n-1,-1); par.resize(n+n-1,-1); m = 0; } }; } // namespace noya2 #line 2 "Tree.hpp" namespace noya2{ using ll = long long; using P = pair; struct edge{ int to, idx; ll cost; edge (int _to = -1, ll _cost = 1, int _idx = -1) : to(_to), cost(_cost), idx(_idx) {} }; struct Tree{ Tree (int _n = 0, int _root = 0) : n(_n), root(_root) { assert(0 <= _root && _root < n); initialize(); } int add_edge(int u, int v, ll cost = 1){ int res = edges.size(); vs[u].emplace_back(edge(v,cost,res)); vs[v].emplace_back(edge(u,cost,res)); edges.emplace_back(P(u,v)); return res; } void build(){ dfs_init(root); int t = 0; dfs_hld(root,t); } int lca(int u, int v){ while (nxt[u] != nxt[v]){ if (down[u] < down[v]) swap(u,v); u = par[nxt[u]]; } return depth[u] < depth[v] ? u : v; } vector point_HLD(){ return hld_order; } vector edge_HLD(){ vector res(n,-1); for (int i = 1; i < n; i++){ res[i] = par_edge_idx[hld_order[i]]; } return res; } P getidx(int v){return P(down[v],up[v]);} vector child(int v){ vector res; for (edge x : vs[v]) if (x.to != par[v]) res.emplace_back(x.to); return res; } int parent(int v){return par[v];} int deepth(int v){return depth[v];} template void path_query(int u, int v, bool vertex, const F &f){ // f is function takes (left, right) as argument, range = [left,right). int l = lca(u,v); for (auto &p : ascend(u,l)){ int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1) s > t ? f(t,s) : f(s,t); } if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point for (auto &p : descend(l,v)){ int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1) s > t ? f(t,s) : f(s,t); } } template void path_noncommutative_query(int u, int v, bool vertex, const F &f){ // op(l,r) != op(r,l), so prod[u->...->v] != prod[v->...->u] int l = lca(u,v); for (auto &p : ascend(u,l)){ int s = p.first + 1, t = p.second; // p.first + 1 : depth(p.first) > depth(p.second), so [p.second,p.first] = [p.second,p.first+1) f(s,t); // le > ri ok } if (vertex) f(down[l],down[l]+1); // vertex is true : query is for point for (auto &p : descend(l,v)){ int s = p.first, t = p.second + 1; // p.second +1 : depth(p.first) < depth(p.second), so [p.first,p.second] = [p.first,p.second+1) f(s,t); // le > ri ok } } template void subtree_query(int v, bool vertex, const F &f){ f(down[v] + (vertex ? 0 : 1), up[v]); } private: int n; int root; vector

edges; vector> vs; vector size, par, depth, up, down, nxt, hld_order; // nxt[i] : most shallow vertex in connected component of vertex i vector par_edge_idx; // index of the edge to par void initialize(){ vs.resize(n); size.resize(n,0); par.resize(n,root); depth.resize(n,0); up.resize(n,-1); down.resize(n,-1); nxt.resize(n,root); hld_order.resize(n,-1); par_edge_idx.resize(n,-1); } void dfs_init(int cur){ size[cur] = 1; for (edge &e : vs[cur]){ if (e.to == par[cur]){ if (vs[cur].size() >= 2 && e.to == vs[cur][0].to){ swap(vs[cur][0],vs[cur][1]); // if cur is not leaf, vs[cur][0] is not cur's parent } else continue; } par[e.to] = cur; par_edge_idx[e.to] = e.idx; depth[e.to] = depth[cur] + 1; dfs_init(e.to); size[cur] += size[e.to]; if (size[e.to] > size[vs[cur][0].to]){ swap(e,vs[cur][0]); // to maximize vs[cur][0]'s subtree_size } } } void dfs_hld(int cur, int &tnow){ down[cur] = tnow++; // down[0,...,n-1] is permutation of 0,...,n-1 hld_order[down[cur]] = cur; // hld_order[i] is ith vertex visited on Euler tour for (edge e : vs[cur]){ if (e.to == par[cur]) continue; nxt[e.to] = (e.to == vs[cur][0].to ? nxt[cur] : e.to); dfs_hld(e.to,tnow); } up[cur] = tnow; // up[0,...,n-1] is NOT permutation, up[*] <= n } vector

ascend(int u, int v) const { // [u,v), depth[u] > depth[v] vector

res; while (nxt[u] != nxt[v]){ res.emplace_back(P(down[u],down[nxt[u]])); // [s1,t1], [s2,t2], ... u = par[nxt[u]]; } if (u != v) res.emplace_back(P(down[u],down[v]+1)); // [s,t). v is not in the range (down[] is ordered opposite direction of depth) return res; } vector

descend(int u, int v) const { // (u,v], depth[u] < depth[v] if (u == v) return {}; if (nxt[u] == nxt[v]){ return {P(down[u]+1,down[v])}; // (s,t]. u is not in the range } vector

res = descend(u,par[nxt[v]]); res.emplace_back(P(down[nxt[v]],down[v])); // [s1,t1], [s2,t2], ... return res; } }; } //namespace noya2 #line 77 "c.cpp" using namespace noya2; ll op(ll a, ll b){return max(a,b);} ll e(){return -inf;} ll mapping(ll f, ll x){return f + x;} ll composition(ll f, ll g){return f + g;} ll id(){return 0LL;} void solve(){ int n, q; cin >> n >> q; vector> query(q,vector(3)); cin >> query; UFtree d(n); rep(i,q){ query[i][1]--; if (query[i][0] == 3) continue; if (query[i][0] == 1){ query[i][2]--; d.merge(query[i][1],query[i][2]); } else { query[i][1] = d.parent(query[i][1]); } } Tree g(n+n,n+n-1); rep(i,n+n-1){ if (d.par[i] > 0) g.add_edge(i,d.par[i]); else g.add_edge(i,n+n-1); } g.build(); auto v = g.point_HLD(); lazy_segtree seg(n+n); rep(i,n+n) seg.set(i,0); ll val = 0; auto f = [&](int l, int r){ seg.apply(l,r,val); }; rep(i,q){ if (query[i][0] == 1) continue; if (query[i][0] == 2){ val = query[i][2]; g.subtree_query(query[i][1],true,f); } else { out(seg.get(g.getidx(query[i][1]).first)); } } } int main(){ fast_io(); int t = 1; //cin >> t; while(t--) solve(); }