/* -*- coding: utf-8 -*- * * 1054.cc: No.1054 Union add query - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 500000; const int MAX_Q = 500000; /* typedef */ template struct ExUFT { int n, links[MAX_N]; T vs[MAX_N]; ExUFT() {} void init(int _n) { n = _n; for (int i = 0; i < n; i++) links[i] = i, vs[i] = 0; } int root(int i) { int i0 = links[i]; while (links[i0] != i0) { vs[i] += vs[i0]; i0 = links[i0]; } return (links[i] = i0); } T getv(int i) { int ri = root(i); return (ri != i) ? vs[i] + vs[ri] : vs[i]; } void addv(int i, T v) { int ri = root(i); vs[ri] += v; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r = n++; links[r] = r, vs[r] = 0; links[root(i0)] = links[root(i1)] = r; return r; } }; /* global variables */ ExUFT uft; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); uft.init(n); while (m--) { int t, a, b; scanf("%d%d%d", &t, &a, &b); if (t == 1) { a--, b--; uft.merge(a, b); } else if (t == 2) { a--; uft.addv(a, b); } else { a--; printf("%d\n", uft.getv(a)); } } return 0; }