#include #include // CUT begin // Add/erase ranges on \mathbb{Z} // Reference: (Almost transcribed from this code) template struct integer_segments { const Int INVALID = -1; std::map mp; integer_segments() = default; // Get the range [l, r] that satisfies l <= x <= r, or [INVALID, INVALID] otherwise std::pair get(Int x) const { auto itr = mp.upper_bound(x); if (itr == mp.begin() or (--itr)->second < x) { return std::make_pair(INVALID, INVALID); } return *itr; } // Add {l, l + 1, ..., r} and return the merged segment which [l, r] belongs to std::pair insert(Int l, Int r) { auto itl = mp.upper_bound(l), itr = mp.upper_bound(r + 1); if (itl != mp.begin() and (--itl)->second < l - 1) { ++itl; } if (itl != itr) { l = std::min(l, itl->first), r = std::max(r, std::prev(itr)->second); mp.erase(itl, itr); } mp[l] = r; return std::make_pair(l, r); } // Remove {l, l + 1, ..., r} void remove(Int l, Int r) { auto itl = mp.upper_bound(l), itr = mp.upper_bound(r); if (itl != mp.begin() and (--itl)->second < l) { ++itl; } if (itl == itr) { return; } Int tl = std::min(l, itl->first), tr = std::max(r, std::prev(itr)->second); mp.erase(itl, itr); if (tl < l) { mp[tl] = l - 1; } if (r < tr) { mp[r + 1] = tr; } } }; #include #include using namespace std; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); long long D; int Q; cin >> D >> Q; integer_segments seg; long long ret = 0; while (Q--) { long long a, b; cin >> a >> b; auto [l, r] = seg.insert(a, b); ret = max(ret, r - l + 1); cout << ret << '\n'; } }