//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 0xccccccc; const ll LINF = 922337203685477580LL; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} struct UnionFind { vector par; // 親を指すvector,-par[親]は木のサイズ UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり inline void init() { rep(i, par.size()) par[i] = -1; } int root(int x) { // 親をたどる&データの整理 if(par[x] < 0) return x; return par[x] = root(par[x]); } bool unite(int x, int y) { // データの結合 x = root(x); y = root(y); if(x == y) return false; //if(par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } bool same(int x, int y) {return root(x) == root(y);} // 所属判定 int size(int x) {return -par[root(x)];} // 木のサイズ }; const int N = 5e5+10; //head int n, q; string t; int a[N], b[N]; UnionFind uft(N); int last[N], front[N]; int nex[N]; int place[N]; P info[N]; int bit[N]; void add(int i, int x) { while(i <= n) { bit[i] += x; i += i&-i; } } int sum(int i) { int res = 0; while(i) { res += bit[i]; i &= i-1; } return res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); memset(nex, -1, sizeof(nex)); cin >> n >> q; rep(i, n) { info[i] = P(i+1, i+2); last[i] = i; front[i] = i; } rep(i, q) { char u; cin >> u >> a[i] >> b[i]; a[i]--; b[i]--; t += u; if(u == '1' and !uft.same(a[i], b[i])) { a[i] = uft.root(a[i]); b[i] = uft.root(b[i]); if(a[i] > b[i]) swap(a[i], b[i]); nex[last[uft.root(a[i])]] = front[uft.root(b[i])]; last[uft.root(a[i])] = last[uft.root(b[i])]; front[uft.root(b[i])] = front[uft.root(a[i])]; uft.unite(a[i], b[i]); } } int cnt = 0; rep(i, n) if(i == uft.root(i)) { int now = i; while(now != -1) { place[now] = cnt++; now = nex[now]; } } //rep(i, n) cout << place[i] << endl; uft.init(); rep(i, q) { if(t[i] == '1') { a[i] = place[a[i]]; b[i] = place[b[i]]; if(uft.same(a[i], b[i])) continue; P &u = info[uft.root(a[i])], &v = info[uft.root(b[i])]; u.first = v.first = min(u.first, v.first); u.second = v.second = max(u.second, v.second); uft.unite(a[i], b[i]); } else if(t[i] == '2') { a[i] = uft.root(place[a[i]]); b[i]++; add(info[a[i]].first, b[i]); add(info[a[i]].second, -b[i]); } else { a[i] = place[a[i]]; cout << sum(a[i]+1) << '\n'; } //cout << i << endl; //rep(i, n) cout << sum(i+1) << (i == n-1?'\n':' '); } }