#define LOCAL #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include using namespace std; #define rep(i,x) for(ll i = 0; i < (ll)(x); i++) #define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--) #define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++) #define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair Pll; typedef vector vl; typedef vector vvl; typedef vector vvvl; constexpr ll INF = numeric_limits::max()/4; constexpr ll n_max = 2e5+10; #define int ll template string to_string(pair p); string to_string(const string &s) {return '"' + s + '"';} string to_string(const char c) {return to_string((string) &c);} string to_string(bool b) {return (b ? "true" : "false");} template string to_string(bitset v){ string res = ""; for(size_t i = 0; i < N; i++) res += static_cast('0' + v[i]); return res; } template string to_string(A v) { bool first = true; string res = "{"; for(const auto &x : v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } template string to_string(pair p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";} void debug_out() {cerr << endl;} template void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif struct UnionFind{ private: vector par; vector rank; vector sz; vector> table; public: //n要素で親を初期化、par[x]はxの親を表す UnionFind(int n){ par.resize(n,0); rank.resize(n,0); sz.resize(n,1); table.resize(n); rep(i,n){ par[i] = i; table[i].emplace_back(i); } } //木の根を求める int root(int x){ if(par[x] == x) return x; else return par[x] = root(par[x]); } //xとyの属する集合を併合 void unite(int x, int y){ x = root(x); y = root(y); if(x == y) return; if(x > y){ swap(x,y); } par[y] = x; sz[x] += sz[y]; copy(all(table[y]), back_inserter(table[x])); table[y].clear(); if(rank[x] == rank[y]) rank[x]++; } //xとyが同じ集合に属するか否か bool same(int x, int y){ return root(x) == root(y); } //xが属する集合のサイズを返す int size(int x){ return sz[root(x)]; } // 集合の数を返す int num_of_s(){ set st; rep(i,par.size()) st.insert(root(i)); return st.size(); } vvl data(){ return table; } int back(int x){ return table[root(x)].back(); } }; template< typename Monoid, typename OperatorMonoid = Monoid > struct LazySegmentTree { using F = function< Monoid(Monoid, Monoid) >; // using G = function< Monoid(Monoid, OperatorMonoid) >; using G = function< Monoid(Monoid, OperatorMonoid, int) >; using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >; int sz, height; vector< Monoid > data; vector< OperatorMonoid > 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(sz < n) sz <<= 1, height++; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build(vector< Monoid > &vec) { for(int i = 0; i < vec.size(); i++)set(i,vec[i]); for(int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } inline void propagate(int k) { if(lazy[k] != OM0) { 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]); return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k], length(k)); } int length(int k) { int l = sz; while(k >>= 1)l >>= 1; return l; } inline void recalc(int k) { 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 < r; 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(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 < r; 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); } template< typename C > int find_subtree(int a, const C &check, Monoid &M, bool type) { while(a < sz) { propagate(a); Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type)); if(check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template< typename C > int find_first(int a, const C &check) { Monoid L = M1; if(a <= 0) { if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false); return -1; } thrust(a + sz); int b = sz; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) { Monoid nxt = f(L, reflect(a)); if(check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template< typename C > int find_last(int b, const C &check) { Monoid R = M1; if(b >= sz) { if(check(f(reflect(1), R))) return find_subtree(1, check, R, true); return -1; } thrust(b + sz - 1); int a = sz; for(b += sz; a < b; a >>= 1, b >>= 1) { if(b & 1) { Monoid nxt = f(reflect(--b), R); if(check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n,q; cin >> n >> q; UnionFind uf(n); vvl query(q); rep(i,q){ ll t,a,b; cin >> t >> a >> b; query[i] = {t,a,b}; if(t == 1){ a--;b--; uf.unite(a,b); } } auto table = uf.data(); ll id = 0; map mp; rep(i,n){ for(auto &t : table[i])mp[t] = id++; } auto f = [](ll a, ll b){ return a + b; }; auto g = [](ll a, ll x, int num){ return a + x * num; }; auto h = [](ll a, ll b){ return a + b; }; LazySegmentTree seg(n,f,g,h,0,0); UnionFind uf2(n); rep(i,q){ ll t = query[i][0]; ll a = query[i][1]; ll b = query[i][2]; if(t == 1){ a--;b--; uf2.unite(a,b); } if(t == 2){ a--; ll c = uf2.back(a); // debug(c); seg.update(mp[uf2.root(a)], mp[c] + 1, b); } if(t == 3){ a--; cout << seg.query(mp[a], mp[a] + 1) << "\n"; } } }