#include using namespace std; using lint = long long int; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const pair &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; // UnionFind Tree (0-indexed), based on size of each disjoint set struct UnionFind { std::vector par, cou; std::vector L, R; vector conn; UnionFind(int N = 0) : par(N), cou(N, 1), L(N), R(N), conn(N, -1) { iota(par.begin(), par.end(), 0); iota(L.begin(), L.end(), 0); iota(R.begin(), R.end(), 0); } int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return false; if (cou[x] < cou[y]) std::swap(x, y); par[y] = x, cou[x] += cou[y]; conn[R[x]] = L[y]; R[x] = R[y]; return true; } int count(int x) { return cou[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } }; // 1-indexed BIT (i : [1, len]) template struct BIT : std::vector { BIT(int len = 0) : std::vector(len + 1) {} void reset() { fill(this->begin(), this->end(), 0); } void add(int pos, T v) { while (pos > 0 and pos < (int)this->size()) (*this)[pos] += v, pos += pos & -pos; } T sum(int pos) const { // (0, pos] T res = 0; while (pos > 0) res += (*this)[pos], pos -= pos & -pos; return res; } friend std::ostream &operator<<(std::ostream &os, const BIT &bit) { T prv = 0; os << '['; for (int i = 1; i < (int)bit.size(); i++) { T now = bit.sum(i); os << now - prv << ","; prv = now; } os << ']'; return os; } }; int main() { int N, Q; cin >> N >> Q; vector T(Q), A(Q), B(Q); UnionFind uf(N); REP(i, Q) { cin >> T[i] >> A[i] >> B[i]; A[i]--, B[i]--; if (T[i] == 1) uf.unite(A[i], B[i]); } vector prv(N, -1); REP(i, N) if (uf.conn[i] >= 0) prv[uf.conn[i]] = i; vector ord; REP(i, N) if (prv[i] == -1) { int now = i; while (true) { ord.emplace_back(now); now = uf.conn[now]; if (now == -1) break; } } vector ordinv(N); REP(i, N) ordinv[ord[i]] = i; uf = UnionFind(N); BIT bit(N); REP(i, Q) { if (T[i] == 1) { uf.unite(A[i], B[i]); } if (T[i] == 2) { int a = uf.find(A[i]); int l = ordinv[uf.L[a]] + 1, r = ordinv[uf.R[a]] + 1; int ad = B[i] + 1; bit.add(l, ad); bit.add(r + 1, -ad); } if (T[i] == 3) { int p = ordinv[A[i]]; cout << bit.sum(p + 1) << '\n'; } } }