#pragma GCC optimize("Ofast") #include #include #include using namespace std; using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; //constexpr long long MOD = 998244353LL; template inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; } template inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; } //////////////////////////////////////////////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,Q; cin >> N >> Q; vector arr(N); vector root(N); vector> dic(N); iota(all(root),0); rep(i,N) dic[i].emplace(i); while (Q--) { int T,A,B; cin >> T >> A >> B; if (T==1) { A--; B--; if (root[A] == root[B]) continue; if (dic[root[A]].size() > dic[root[B]].size()) swap(A,B); int r = root[A]; ll val = arr[root[A]]; for (auto it : dic[root[A]]) { if (it==r) arr[it] -= arr[root[B]]; else arr[it] += val - arr[root[B]]; root[it] = root[B]; dic[root[B]].emplace(it); } } else if (T==2) { A--; arr[root[A]] += B; } else { A--; if (A==root[A]) cout << arr[A] << ln; else cout << arr[A] + arr[root[A]] << ln; } } }