結果

問題 No.674 n連勤
ユーザー gyouzasushigyouzasushi
提出日時 2023-01-19 00:09:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,332 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 82,748 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 17:05:44
合計ジャッジ時間 3,259 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 69 ms
4,380 KB
testcase_14 AC 73 ms
4,376 KB
testcase_15 AC 71 ms
4,384 KB
testcase_16 AC 90 ms
4,380 KB
testcase_17 AC 91 ms
4,376 KB
testcase_18 AC 84 ms
4,376 KB
testcase_19 AC 73 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "test/yukicoder/1601.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/1601"

#include <iostream>

#line 1 "datastructure/range_set.hpp"
#include <cassert>
#include <map>
template <typename T>
struct range_set {
public:
    range_set(bool merge_adjacent_segment = true)
        : merge_adjacent_segment(merge_adjacent_segment) {
    }
    void clear() {
        mp.clear();
    }
    size_t size() {
        return mp.size();
    }
    bool is_covered(T l, T r) {
        assert(l <= r);
        auto it = mp.upper_bound(l);
        return it != mp.begin() && std::prev(it)->first <= l &&
               r <= std::prev(it)->second;
    }
    bool is_covered(T p) {
        return is_covered(p, p);
    }
    auto covered_by(T l, T r) {
        assert(l <= r);
        assert(is_covered(l, r));
        return std::prev(mp.upper_bound(l));
    }
    auto covered_by(T p) {
        return covered_by(p, p);
    }
    void insert(T l, T r) {
        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 >= l - int(merge_adjacent_segment)) {
                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);
        }
        mp[l] = r;
    }
    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 >= l) {
                it_l--;
            }
        }
        int nl = std::min(l, it_l->first);
        int nr = std::max(r, std::prev(it_r)->second);
        mp.erase(it_l, it_r);
        if (nl < l) mp[nl] = l - 1;
        if (r < nr) mp[r + 1] = nr;
    }

private:
    bool merge_adjacent_segment;
    std::map<T, T> mp;
};
#line 6 "test/yukicoder/1601.test.cpp"
int main() {
    long long d;
    int q;
    std::cin >> d >> q;
    range_set<long long> st;
    long long ans = 0;
    while (q--) {
        long long a, b;
        std::cin >> a >> b;
        st.insert(a, b);
        auto [l, r] = *st.covered_by(a);
        ans = std::max(ans, r - l + 1);
        std::cout << ans << '\n';
    }
}
0