#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return vector(arg, init); else return vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template auto distance(const Container &c, decltype(begin(c)) iter) { return distance(begin(c), iter); } template ::value_type>> auto isort(RIter first, RIter last, Compare comp = Compare()) { vector i(distance(first, last)); iota(begin(i), end(i), 0); sort(begin(i), end(i), [&](auto x, auto y) { return comp(*(first + x), *(first + y)); }); return i; } template typename, typename = void_t<>> struct detect : false_type {}; template typename Check> struct detect>> : true_type {}; template typename Check> constexpr inline bool detect_v = detect::value; template using has_member_sort = decltype(declval().sort()); template > auto sorted(Container c, Compare comp = Compare()) { if constexpr (detect_v) { c.sort(comp); return c; } else { sort(begin(c), end(c), comp); return c; } } template > auto uniqued(Container c, Compare comp = Compare()) { c.erase(unique(begin(c), end(c), comp), end(c)); return c; } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } #ifndef STCP_SEGMENT_SET_HPP #define STCP_SEGMENT_SET_HPP #include #ifndef STCP_BASE_HPP #define STCP_BASE_HPP #include #include #include #include #include #include #include #include namespace stcp { using std::size_t; using std::int8_t; using std::uint8_t; using std::int16_t; using std::uint16_t; using std::int32_t; using std::uint32_t; using std::int64_t; using std::uint64_t; using std::pair; using std::optional; using std::nullopt; using std::make_pair; template const inline auto inf = std::numeric_limits::max(); template T &chmin(T &l, const T &r) { return l = std::min(l, r); } template T &chmin(T &l, const T &r, Compare c) { return l = std::min(l, r, std::move(c)); } template T &chmax(T &l, const T &r) { return l = std::max(l, r); } template T &chmax(T &l, const T &r, Compare c) { return l = std::max(l, r, std::move(c)); } } #ifdef STCP_ENABLE_MULTIPRECISION #include namespace stcp { using bint = boost::multiprecision::cpp_int; template <> const inline bint inf = (bint(1) << 1023) - 1; } #endif // STCP_ENABLE_MULTIPRECISION #endif // STCP_BASE_HPP namespace stcp { template struct segment_set { using int_type = Int; segment_set(): seg_() { } public: int_type insert(int_type l, int_type r, const bool connect_adjacent_segment = false) { if (r <= l) { return 0; } auto iter1 = seg_.upper_bound(l); auto iter2 = seg_.lower_bound(r + connect_adjacent_segment); if (iter1 != std::begin(seg_)) { std::advance(iter1, -1); if (l < iter1->second + connect_adjacent_segment) { l = iter1->first; } std::advance(iter1, +1); } if (iter2 != std::begin(seg_)) { std::advance(iter2, -1); if (r <= iter2->second) { r = iter2->second; } std::advance(iter2, +1); } seg_.erase(iter1, iter2); seg_[l] = r; return r - l; } void remove_covered(int_type l, int_type r) { if (r <= l) { return; } auto iter = seg_.upper_bound(l); if (iter != std::begin(seg_)) { std::advance(iter, -1); } while (iter->first < r) { if (l < iter->second) { iter = seg_.erase(iter); } else { std::advance(iter, +1); } } } void remove_covered(int_type x) { remove_covered(x, x + 1); } pair covered(int_type x) const { auto iter = seg_.upper_bound(x); if (iter == std::begin(seg_)) { return { 0, 0 }; } std::advance(iter, -1); if (iter->second <= x) { return { 0, 0 }; } return *iter; } bool same(int_type x, int_type y) const { auto [l, r] = covered(x); return l <= y && y < r; } private: std::map seg_; }; } #endif // STCP_SEGMENT_SET_HPP int main() { int64_t d; cin >> d; stcp::segment_set seg; int64_t max = 0; int q; cin >> q; while (q--) { int64_t a, b; cin >> a >> b; ++b; chmax(max, seg.insert(a, b, true)); cout << max << endl; } }