結果

問題 No.674 n連勤
ユーザー cutmdo
提出日時 2022-09-04 13:07:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 2,837 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 84,432 KB
最終ジャッジ日時 2025-02-07 02:34:21
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:23:41: warning: use of ‘auto’ in parameter declaration only available with ‘-std=c++20’ or ‘-fconcepts’
   23 |     auto remove(SizeType l, SizeType r, auto& it) {
      |                                         ^~~~

ソースコード

diff #

#include <iostream>
#include <iostream>
#include <map>
#include <stdexcept>
#include <deque>


template<class ValType = long long, class SizeType = long long>
class SegmentMap {

    const SizeType n;
    std::map<SizeType, ValType> mp;

    auto add(SizeType i, ValType val, bool left = true, bool right = true) {
        auto it = std::prev(mp.upper_bound(i));
        if(right && std::next(it)->second == val) { mp.erase(std::next(it)); }
        if(left && it->second == val) { return; }
        mp.emplace(i, val);

    }

    auto remove(SizeType l, SizeType r, auto& it) {
        auto nx = std::next(it)->first;
        auto val = it->second;
        auto ret = std::next(it);
        if(l <= it->first) { ret = mp.erase(it); }
        if(r < nx - 1) { add(r + 1, val, false, true); }
        return ret;
    }

    auto remove(SizeType l, SizeType r) {
        auto it = std::prev(mp.upper_bound(l));
        while(it->first <= r) {
            it = remove(l, r, it);
        }
    }


public:

    SegmentMap(SizeType n) :n(n) {
        mp.emplace(-1, -1);
        mp.emplace(0, -2);
        mp.emplace(n, -1);
    }

    auto output() const {
        for(auto it = std::next(mp.begin()); it != std::prev(mp.end()); ++it) {
            std::cout << "[" << it->first << ", " <<
                std::next(it)->first - 1 << "] :" <<
                it->second << std::endl;
        }
    }

    auto update(SizeType l, SizeType r, ValType val) {
        if(l < 0 || r >= n) { throw std::runtime_error(""); }
        if(l > r) { throw std::runtime_error(""); }
        remove(l, r);
        add(l, val);
    }

    auto query(SizeType l, SizeType r) {
        if(l < 0 || r >= n) { throw std::runtime_error(""); }
        if(l > r) { throw std::runtime_error(""); }
        auto it = std::prev(mp.upper_bound(l));
        std::deque<std::pair<ValType, std::pair<SizeType, SizeType>>> dq;
        while(it->first <= r) {
            auto nx = std::next(it)->first;
            auto nr = std::min(nx - 1, r);
            auto nl = std::max(l, it->first);
            dq.emplace_back(it->second, std::pair{nl,nr});
            ++it;
        }
        return dq;
    }

    auto get(SizeType i) {
        auto it = std::prev(mp.upper_bound(i));
        auto nx = std::next(it)->first;
        auto nr = nx - 1;
        auto nl = it->first;
        return std::pair{it->second,std::pair{nl,nr}};
    }
};

using ll = long long;
using std::cout;
using std::cin;
constexpr char endl = '\n';


signed main() {
    ll d, q;
    cin >> d >> q;

    auto segmap = SegmentMap(d);

    ll ans = 0;
    for(int _ = 0; _ < q; ++_) {
        ll a, b;
        cin >> a >> b;
        segmap.update(a, b, 1);

        auto [__, range] = segmap.get(a);
        ans = std::max(ans, range.second - range.first + 1);
        cout << ans << endl;
    }
}
0