結果

問題 No.674 n連勤
ユーザー 👑 hitonanodehitonanode
提出日時 2020-11-15 20:54:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,079 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 83,132 KB
実行使用メモリ 4,392 KB
最終ジャッジ日時 2023-09-30 07:06:54
合計ジャッジ時間 2,359 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <map>
#include <utility>

// CUT begin
// Add/erase ranges on \mathbb{Z}
// Reference: <https://satanic0258.github.io/snippets/data-structure/SegmentMap.html> (Almost transcribed from this code)
template <typename Int>
struct integer_segments {
    const Int INVALID = -1;
    std::map<Int, Int> mp;
    integer_segments() = default;

    // Get the range [l, r] that satisfies l <= x <= r, or [INVALID, INVALID] otherwise
    std::pair<Int, Int> 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<Int, Int> 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 <iostream>
#include <set>
using namespace std;

int main()
{
    cin.tie(nullptr), ios::sync_with_stdio(false);
    long long D;
    int Q;
    cin >> D >> Q;
    integer_segments<long long> 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';
    }
}
0