#line 1 "test/yukicoder/1601.test.cpp" #define PROBLEM "https://yukicoder.me/problems/1601" #include #line 2 "datastructure/range_set.hpp" #include template struct range_set { public: range_set(bool merge_adjacent = true) : _merge_adjacent(merge_adjacent) { } typename std::map::const_iterator get(T p) const { auto it = ranges.upper_bound(p); if (it == ranges.begin()) return ranges.end(); if (std::prev(it)->second < p) return ranges.end(); return it; } void insert(T l, T r) { insert(l, r, [](T, T) {}, [](T, T) {}); } template void insert(T l, T r, const op_insert &f, const op_erase &g) { auto it_l = ranges.upper_bound(l); auto it_r = ranges.upper_bound(r + T(_merge_adjacent)); if (it_l != ranges.begin() && l - T(_merge_adjacent) <= std::prev(it_l)->second) { it_l--; } for (auto it = it_l; it != it_r; it = ranges.erase(it)) { l = std::min(l, it->first); r = std::max(r, it->second); g(it->first, it->second); } ranges[l] = r; f(l, r); } typename std::map::const_iterator begin() const { return ranges.begin(); } typename std::map::const_iterator end() const { return ranges.end(); } private: std::map ranges; bool _merge_adjacent; }; #line 6 "test/yukicoder/1601.test.cpp" int main() { long long d; int q; std::cin >> d >> q; range_set st; long long ans = 0; while (q--) { long long a, b; std::cin >> a >> b; st.insert( a, b, [&](long long l, long long r) { ans = std::max(ans, r - l + 1); }, [](long long, long long) {}); std::cout << ans << '\n'; } }