#include #include #include using std::cout; using std::endl; using i64 = long long; struct union_find { std::vector par; std::vector Sigma; union_find(int N): par(N * 2, -1) , Sigma(N * 2, 0) { } int root(int x) { if(par[x] < 0) { return x; } int v = x; while(par[par[v]] >= 0) { v = par[v]; Sigma[x] += Sigma[v]; } par[x] = par[v]; return par[v]; } std::tuple unite(int x, int y) { x = root(x); y = root(y); if(x == y) return { -1, -1 }; if(par[x] > par[y]) std::swap(x, y); par[x] += par[y]; par[y] = x; Sigma[y] -= Sigma[x]; return { x, y }; } void add(int v, i64 a) { Sigma[root(v)] += a; } i64 value(int v) { int r = root(v); if(r == v) { return Sigma[v]; } else { return Sigma[v] + Sigma[r]; } } }; #include using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() template static inline std::vector ndvec(size_t&& n, T val) noexcept { return std::vector(n, std::forward(val)); } template static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept { return std::vector(tail)...))>(n, ndvec(std::forward(tail)...)); } template struct chain { Cond cond; chain(Cond cond) : cond(cond) {} bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; } }; template chain make_chain(Cond cond) { return chain(cond); } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; union_find uf(N); while(Q--) { int t, a, b; cin >> t >> a >> b; a--; if(t == 1) { b--; uf.unite(a, b); } else if(t == 2) { uf.add(a, b); } else { cout << uf.value(a) << "\n"; } /* cout << Q << endl; rep(i,0,N) { cout << i << " = " << uf.Sigma[i] << " " << uf.par[i] << endl; }*/ } }