#include using namespace std; template struct Range { T l, r; Range(T l, T r) : l(l), r(r) { if (l > r) l = r = 0ll; } Range() : Range(0ll, 0ll) {} Range operator&(Range b) const { T nl = max(l, b.l), nr = min(r, b.r); if (nl < nr) return Range(nl, nr); else return Range(); } Range operator|(Range b) const { if (max(l, b.l) > min(r, b.r)) return Range(); return Range(min(l, b.l), max(r, b.r)); } bool empty() const { return l == r; } bool contains(T x) const { return l <= x && x < r; } T count() const { return r - l; } bool operator<(const Range& b) const { return (l != b.l) ? l < b.l : r < b.r; } }; template struct RangeSet { set> ranges; RangeSet() {} int64_t cnt = 0; void insert(Range r) { if (r.empty()) return; auto it = ranges.lower_bound(r); if (it != ranges.end() && it->l == r.l && r.r <= it->r) return; if (it != ranges.begin()) { auto pit = prev(it); if (r.r <= pit->r) return; if (r.l <= pit->r) { r.l = pit->l; cnt -= pit->count(); ranges.erase(pit); } } while (it != ranges.end() && r.r >= it->l) { r.r = max(r.r, it->r); cnt -= it->count(); it = ranges.erase(it); } cnt += r.count(); ranges.insert(r); } void erase(Range r) { if (r.empty()) return; auto it = ranges.lower_bound(r); if (it != ranges.end() && it->l == r.l && r.r <= it->r) { Range nl = Range(it->l, r.l); Range nr = Range(r.r, it->r); cnt -= it->count(); cnt += nl.count() + nr.count(); ranges.erase(it); if (!nl.empty()) ranges.insert(nl); if (!nr.empty()) ranges.insert(nr); return; } if (it != ranges.begin()) { auto pit = prev(it); if (r.r <= pit->r) { Range nl = Range(pit->l, r.l); Range nr = Range(r.r, pit->r); cnt -= pit->count(); cnt += nl.count() + nr.count(); ranges.erase(pit); if (!nl.empty()) ranges.insert(nl); if (!nr.empty()) ranges.insert(nr); return; } if (r.l <= pit->r) { Range nl = Range(pit->l, r.l); cnt -= pit->count(); ranges.erase(pit); if (!nl.empty()) ranges.insert(nl); } } while (it != ranges.end() && r.r >= it->l) { if (it->r <= r.r) { cnt -= it->count(); it = ranges.erase(it); } else { Range nr = Range(r.r, it->r); cnt -= it->count(); it = ranges.erase(it); if (!nr.empty()) ranges.insert(nr); } } } Range get(T x) { auto it = ranges.lower_bound(Range(x, numeric_limits::max())); if (it != ranges.begin()) { auto pit = prev(it); if (pit->contains(x)) return *pit; } return Range(); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n >> q; RangeSet rs; while (q--) { int t; cin >> t; if (t == 1) { int l, r; cin >> l >> r; l *= 2, r *= 2; rs.insert({l, r + 1}); } else if (t == 2) { int l, r; cin >> l >> r; l *= 2, r *= 2; rs.erase({l + 1, r}); } else if (t == 3) { int u, v; cin >> u >> v; u *= 2, v *= 2; if (u == v) { cout << 1 << "\n"; continue; } auto r = rs.get(u); cout << r.contains(v) << "\n"; } else { int v; cin >> v; v *= 2; auto r = rs.get(v); cout << max(1, (r.count() + 1) / 2) << "\n"; } } }