#pragma gcc optimize("Ofast") #include using namespace std; const int N = 5e5 + 1; vector p(N, -1), sv(N), dv(N); vector> s(N); int find(int x) { return p[x] < 0 ? x : p[x] = find(p[x]); } void merge(int x, int y) { if ((x = find(x)) == (y = find(y))) return; if (p[x] > p[y]) swap(x, y); p[x] += p[y]; p[y] = x; for (auto e : s[y]) { sv[e] += dv[y] - dv[x]; s[x].emplace_back(e); } s[y].clear(); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, q; cin >> n >> q; for (int i = 1; i <= n; ++i) s[i].emplace_back(i); while (q--) { int t, a, b; cin >> t >> a >> b; if (t == 1) { merge(a, b); } if (t == 2) { dv[find(a)] += b; } if (t == 3) { cout << sv[a] + dv[find(a)] << '\n'; } } }