#include using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v).begin(), (v).end() #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) #define DEBUG #define int long long #define INF 1e18 template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template ostream& operator << (ostream &s, pair P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, vector P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, vector > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template ostream& operator << (ostream &s, set P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template ostream& operator << (ostream &s, map P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } templatevoid show(vectorv){for (int i = 0; i < v.size(); i++){cerr< par,val; vector> child; UnionFind(int n) : par(n, -1) ,val(n) , child(n) { REP(i,n) { child[i].push_back(i); } } void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; //大きさが正になるよ int sub = val[y] - val[x]; val[y] = 0; for(auto & u : child[y]){ child[x].push_back(u); val[u] += sub; } //child[y].clear(); return true; } void add( int a, int b ){ val[root(a)] += b; } ll get (ll a){ if(par[a] < 0) return val[a]; return val[a] + val[root(a)]; } int size(int x) { return -par[root(x)]; } }; signed main() { int n, q; cin >> n >> q; UnionFind uni(n); REP(i,q){ int t, a, b; cin >> t >> a >> b; if(t==1){ a--, b--; uni.merge(a, b); } else if(t==2){ a--; uni.add(a, b); } else{ a--; int x = uni.get(a); cout << x << endl; } } return 0; }