#line 1 "test/yukicoder/1601.test.cpp" #define PROBLEM "https://yukicoder.me/problems/1601" #include #line 2 "datastructure/range_map.hpp" #include #include #include #include #include template struct range_map { public: range_map(bool merge_adjacent_segment = true) : merge_adjacent_segment(merge_adjacent_segment) { } void clear() { mp.clear(); } size_t size() { return mp.size(); } std::optional> contains(T l, T r) { assert(l <= r); auto it = mp.upper_bound(l); if (it == mp.begin()) return std::nullopt; it--; if (it->first > l) return std::nullopt; if (r > it->second.first) return std::nullopt; return std::make_pair(it->first, it->second.first); } std::optional> contains(T p) { return is_covered(p, p); } void insert(T l, T r, U x) { assert(l <= r); auto it_l = mp.upper_bound(l); auto it_r = mp.upper_bound(r + int(merge_adjacent_segment)); if (it_l != mp.begin()) { if (std::prev(it_l)->second.first >= l - int(merge_adjacent_segment) && std::prev(it_l)->second.second == x) { it_l--; } } for (auto it = it_l; it != it_r; it = mp.erase(it)) { l = std::min(l, it->first); r = std::max(r, it->second.first); } mp[l] = {r, x}; } template void insert(T l, T r, U x, const op_erase &f, const op_insert &g) { assert(l <= r); auto it_l = mp.upper_bound(l); auto it_r = mp.upper_bound(r + int(merge_adjacent_segment)); if (it_l != mp.begin()) { if (std::prev(it_l)->second.first >= l - int(merge_adjacent_segment) && std::prev(it_l)->second.second == x) { it_l--; } } for (auto it = it_l; it != it_r; it = mp.erase(it)) { f(it->first, it->second.first, it->second.second); l = std::min(l, it->first); r = std::max(r, it->second.first); } mp[l] = {r, x}; g(l, r, x); } void erase(T l, T r) { assert(l <= r); auto it_l = mp.upper_bound(l); auto it_r = mp.upper_bound(r); if (it_l != mp.begin()) { if (std::prev(it_l)->second.first >= l) { it_l--; } } T nl = std::min(l, it_l->first); T nr = std::max(r, std::prev(it_r)->second.first); U nl_x = it_l->second.second; U nr_x = std::prev(it_r)->second.second; for (auto it = it_l; it != it_r; it = mp.erase(it)) { } if (nl < l) mp[nl] = {l - 1, nl_x}; if (r < nr) mp[r + 1] = {nr, nr_x}; } template void erase(T l, T r, const op_erase &f, const op_insert &g) { assert(l <= r); auto it_l = mp.upper_bound(l); auto it_r = mp.upper_bound(r); if (it_l != mp.begin()) { if (std::prev(it_l)->second.first >= l) { it_l--; } } T nl = std::min(l, it_l->first); T nr = std::max(r, std::prev(it_r)->second.first); U nl_x = it_l->second.second; U nr_x = std::prev(it_r)->second.second; for (auto it = it_l; it != it_r; it = mp.erase(it)) { f(it->first, it->second.first, it->second.second); } if (nl < l) { mp[nl] = {l - 1, nl_x}; g(nl, l - 1, nl_x); } if (r < nr) { mp[r + 1] = {nr, nr_x}; g(r + 1, nr, nr_x); } } private: bool merge_adjacent_segment; std::map> mp; }; #line 3 "datastructure/range_set.hpp" template struct range_set : range_map { using Base = range_map; using Base::range_map; void insert(T l, T r) { Base::insert(l, r, true); } template void insert(T l, T r, const op_erase &f, const op_insert &g) { Base::insert(l, r, true, f, g); } }; #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, bool x) {}, [&](long long l, long long r, bool x) { ans = std::max(ans, r - l + 1); }); std::cout << ans << '\n'; } }