public import std; DList!string scan_buffer; DList!char char_buffer; static this() { scan_buffer = DList!(string)(); char_buffer = DList!(char)(); } void cin()() { } void cin(T, A...)(ref T a, ref A tail) { import std.traits : isArray; static if (typeof(a).stringof == "string") { if (!char_buffer.empty) { a = char_buffer.array.map!(x => x.to!string).join(); char_buffer.clear(); } else { while (scan_buffer.empty) { foreach (t; readln.split) { if (t.length == 0) { continue; } scan_buffer.insert(t); } } auto token = scan_buffer.front; scan_buffer.removeFront(); a = token; } } else static if (typeof(a).stringof == "char") { if (!char_buffer.empty) { a = char_buffer.front(); char_buffer.removeFront(); } else { while (scan_buffer.empty) { foreach (t; readln.split) { if (t.length == 0) { continue; } scan_buffer.insert(t); } } auto token = scan_buffer.front; scan_buffer.removeFront(); a = token[0]; if (token.length > 1) { foreach (c; token[1 .. $]) { char_buffer.insertBack(c); } } } } else static if (typeof(a).stringof == "char[]") { if (a.length == 0) { string token; cin(token); foreach (c; token) { a ~= c; } } else { foreach (ref v; a) { cin(v); } } } else static if (isArray!(typeof(a))) { foreach (ref v; a) { cin(v); } } else static if (isTuple!(typeof(a))) { foreach (i, _; a) { cin(a[i]); } } else { if (!char_buffer.empty) { writeln(char_buffer.array.map!(x => x.to!string)); a = char_buffer.array.map!(x => x.to!string).join().to!T; char_buffer.clear(); } else { while (scan_buffer.empty) { foreach (t; readln.split) { if (t.length == 0) { continue; } scan_buffer.insert(t); } } auto token = scan_buffer.front; scan_buffer.removeFront(); a = token.to!T; } } cin(tail); } bool chmin(T)(ref T a, T b) { if (a > b) { a = b; return true; } return false; } bool chmax(T)(ref T a, T b) { if (a < b) { a = b; return true; } return false; } alias PQueue(T = long, alias less = "a min(r, p.r)) { break; } l.chmin(p.l); r.chmax(p.r); vec ~= p; } foreach (p; vec) { set.removeKey(p); } set.insert(P(l, r)); return P(l, r); } void unmerge(long l, long r) { auto it = set.upperBound(P(-INF, l)); auto vec = new P[](0); auto vec2 = new P[](0); foreach (p; it) { if (max(l, p.l) > min(r, p.r)) { break; } if (p.l < l) { vec2 ~= (P(p.l, l)); } if (r < p.r) { vec2 ~= (P(r, p.r)); } vec ~= p; } foreach (p; vec) { set.removeKey(p); } foreach (p; vec2) { set.insert(p); } } Nullable!P getInterval(long i) { auto it = set.upperBound(P(-INF, i)); if (it.empty) { return Nullable!P(); } else if (it.front.l <= i && i <= it.front.r) { return it.front.nullable; } else { return Nullable!P(); } } bool opBinary(string op)(long i) { return !getInterval(i).isNull(); } } void main() { int N, Q; cin(N, Q); auto dsu = new IntervalUnionFind(); foreach (_; 0 .. Q) { int k; cin(k); if (k == 1) { int l, r; cin(l, r); dsu.merge(l, r); } else if (k == 2) { int l, r; cin(l, r); dsu.unmerge(l, r); } else if (k == 3) { int u, v; cin(u, v); auto p = dsu.getInterval(u); if (p.isNull()) { if (u == v) { 1.writeln; } else { 0.writeln; } } else { auto pp = p.get(); if (pp.l <= v && v <= pp.r) { 1.writeln; } else { 0.writeln; } } } else if (k == 4) { int u; cin(u); auto p = dsu.getInterval(u); if (p.isNull()) { 1.writeln; } else { auto pp = p.get(); (pp.r - pp.l + 1).writeln; } } } }