/* -*- 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; /* typedef */ template struct ExUFT { int n, links[MAX_N], ranks[MAX_N]; T vs[MAX_N]; ExUFT() {} void init(int _n) { n = _n; for (int i = 0; i < n; i++) links[i] = i, ranks[i] = 1, 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 r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; ranks[r0]++; vs[r1] -= vs[r0]; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; vs[r1] -= vs[r0]; mr = r0; } else { links[r0] = r1; vs[r0] -= vs[r1]; mr = r1; } return mr; } }; /* 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; }