#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long #define double long double typedef vector VI; typedef pair pii; typedef vector VP; typedef vector VS; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i, greater > q2; struct UnionFind { VI par, s; UnionFind(int N) :par(N), s(N) { REP(i, N)par[i] = i; REP(i, N)s[i] = 1; } int root(int x) { if (par[x] == x)return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry)return; if (!(same(x, y)))s[ry] += s[rx]; par[rx] = ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } int size(int x) { return s[root(x)]; } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; int T, A, B; UnionFind uni(N); VI po(N, 0), ans(N, 0); vectorpa(N); REP(i, N)pa[i].push_back(i); while (Q--) { scanf("%lld%lld%lld", &T, &A, &B); A--; if (T == 1) { B--; pa[A].push_back(B); ans[A] -= po[uni.root(B)]; uni.unite(A, B); } else if (T == 2) { po[uni.root(A)] += B; } else if (T == 3) { int cnt = 0; fore(i, pa[A])cnt += po[i]; //if (A == 1)cout << ans[A] << " " << cnt << endl; cout << ans[A] + cnt << '\n'; } } return 0; }