#include #include #include #include #include #include #include #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false) template void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; } #else #define dump(...) do{ } while(false) #endif template vector make_v(size_t a, T b) { return vector(a, b); } template auto make_v(size_t a, Ts... ts) { return vector(a, make_v(ts...)); } template bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; } template bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; } template void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; } template void input(Ts&... ts) { (cin >> ... >> ts); } template istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on class UnionFind { public: vector par; int N; UnionFind(int n) { par.resize(n, -1); N = n; } int Find(int n) { return par[n] < 0 ? n : par[n] = Find(par[n]); } bool Union(int x, int y) { x = Find(x); y = Find(y); if (x == y) return false; if (-par[x] > -par[y]) swap(x, y); par[y] += par[x]; par[x] = y; N--; return true; } bool Same(int x, int y) { return Find(x) == Find(y); } int size(int x) { return -par[Find(x)]; } int size() { return N; } }; #include template class SegmentTreeAllOne { using Func = function; public: vector data; int n; T init; Func update_func; SegmentTreeAllOne(int _n, T _init, Func up) { init = _init; update_func = up; for (n = 1; n < _n; n *= 2) ; data.resize(2 * n - 1, init); } void update(int l, int r, T val) { for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) { if (!(l & 1)) { data[l] = update_func(data[l], val); } if (!(r & 1)) { data[r - 1] = update_func(data[r - 1], val); } } } T query(int pos) { pos += n - 1; T res = data[pos]; while (pos > 0) { pos = (pos - 1) / 2; res = update_func(res, data[pos]); } return res; } }; using Query = tuple; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, q; cin, n, q; vector query(q); rep(i, 0, q) { int t, a, b; cin, t, a, b; if (t == 1) { b--; } a--; query[i] = Query(t, a, b); } UnionFind uf(n); vector> tree(n); rep(i, 0, q) { auto &[t, a, b] = query[i]; if (t == 1) { a = uf.Find(a); b = uf.Find(b); if (a != b) { uf.Union(a, b); tree[a].push_back(b); tree[b].push_back(a); } } else if (t == 2) { a = uf.Find(a); } } vector flatten; vector

ranges(n, P(-1, -1)); auto dfs = [&](auto &f, int v, int p) -> void { ranges[v].first = flatten.size(); flatten.push_back(v); ranges[v].second = flatten.size(); for (auto nv : tree[v]) { if (nv == p) continue; f(f, nv, v); flatten.push_back(v); } }; vector used(n, false); rep(i, 0, n) { int p = uf.Find(i); if (!used[p]) { used[p] = true; dfs(dfs, p, -1); } } auto SUM = [&](ll a, ll b) { return a + b; }; SegmentTreeAllOne seg(flatten.size(), 0LL, SUM); for (auto [t, a, b] : query) { if (t == 1) { P p1 = ranges[a], p2 = ranges[b]; if (p1.first < p2.first) { ranges[a] = P(p1.first, max(p1.second, p2.second)); } else { ranges[b] = P(p2.first, max(p1.second, p2.second)); } } else if (t == 2) { auto [l, r] = ranges[a]; seg.update(l, r, b); } else { auto [l, _] = ranges[a]; print(l == -1 ? 0 : seg.query(l)); } } return 0; }