#include #include using namespace std; struct UnionFind{ vector rank, par; void init(int n){ rank.resize(n); par.resize(n); for(int i = 0; i < n; i++){ rank[i] = 1; par[i] = i; } } int root(int x){ if(par[x] == x) return x; return par[x] = root(par[x]); } bool same(int x, int y){ return root(x) == root(y); } int size(int x){ return rank[root(x)]; } void unite(int x, int y){ x = root(x); y = root(y); if(x == y) return; if(rank[y] > rank[x]) swap(x, y); par[y] = x; rank[x] += rank[y]; } }; int main(){ long long N, Q; scanf("%lld %lld", &N, &Q); UnionFind U; vector sum(N, 0); vector sa(N, 0); U.init(N); for(int i = 0; i < Q; i++){ int t, a, b; scanf("%d %d %d", &t, &a, &b); a--; b--; if(t == 1) { if(U.size(a) >= U.size(b)) sa[b] += sum[U.root(a)] - sum[U.root(b)]; else sa[a] += sum[U.root(b)] - sum[U.root(a)]; U.unite(a, b); } else if(t == 2){ b++; sum[U.root(a)] += b; } else if(t == 3){ printf("%lld\n", sum[U.root(a)] - sa[a]); } } }