#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Yay!! #define endl codeforces // macros for iterator #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) // alias using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; template using vec = std::vector; template using vvec = vec>; // variadic min/max template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } // variadic chmin/chmax template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } // multi demension array template struct multi_dim_array { using type = std::array::type, Head>; }; template struct multi_dim_array { using type = std::array; }; template using mdarray = typename multi_dim_array::type; // fill container template void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable::value) { t = f(args...); } else { for (ssize_t i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } } // make multi dimension vector template vec make_v(ssize_t sz) { return vec(sz); } template auto make_v(ssize_t hs, Tail&&... ts) { auto v = std::move(make_v(std::forward(ts)...)); return vec(hs, v); } // init namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } struct UnionFind { vec rank, par, val; UnionFind(ll n) : rank(n, 1), par(n), val(n, 0) { std::iota(ALL(par), 0); } pll find_aux(ll n, ll sum) { if (par[n] == n) { return pll(n, sum + val[n]); } else { auto [ p, s ] = find_aux(par[n], sum + val[n]); ll v = s - sum; val[n] = v - val[p]; par[n] = p; return pll(p, s); } } ll find(ll n) { return find_aux(n, 0).first; } bool unit(ll x, ll y) { x = find(x), y = find(y); if (x == y) return false; if (rank[x] < rank[y]) std::swap(x, y); par[y] = x; val[y] -= val[x]; if (rank[x] == rank[y]) rank[x]++; return true; } void add(ll n, ll v) { ll p = find(n); val[p] += v; } ll get(ll n) { find(n); return val[n] + (n != par[n] ? val[par[n]] : 0); } }; int main() { ll n, q; std::cin >> n >> q; UnionFind uf(n); while (q--) { ll t, a, b; std::cin >> t >> a >> b; if (t == 1) { uf.unit(a - 1, b - 1); } else if (t == 2) { uf.add(a - 1, b); } else { std::cout << uf.get(a - 1) << "\n"; } } return 0; }