#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define All(v) (v).begin(), (v).end() #define pb push_back #define MP(a, b) make_pair((a), (b)) using ll = long long; using pii = pair; using pll = pair; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; // UnionFind struct UnionFind { vector par; vector siz; vector add; vector rest; vector> childs; UnionFind(ll N) : par(N), siz(N, 1LL), add(N), rest(N), childs(N) { for(int i = 0; i < N; i++) par[i] = i; } 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(siz[rx] < siz[ry]) swap(rx, ry); siz[rx] += siz[ry]; par[ry] = rx; ll dif = add[ry] - add[rx]; rest[ry] += dif; childs[rx].pb(ry); for(auto c : childs[ry]) { rest[c] += dif; childs[rx].pb(c); } childs[ry].clear(); } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } void addQ(int x, int num) { add[root(x)] += num; } int response(int x) { return add[root(x)] + rest[x]; } ll size(ll x) { return siz[root(x)]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, Q; cin >> N >> Q; UnionFind uf(N); rep(i, Q) { int T, A, B; cin >> T >> A >> B; if(T == 1) { A--, B--; uf.unite(A, B); } else if(T == 2) { A--; uf.addQ(A, B); } else { A--; cout << uf.response(A) << endl; } } return 0; }