結果

問題 No.674 n連勤
ユーザー rokahikou1
提出日時 2021-02-26 13:00:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,719 bytes
コンパイル時間 1,906 ms
コンパイル使用メモリ 176,192 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-02 05:32:18
合計ジャッジ時間 3,139 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template <typename T> class IntervalSet : std::set<std::pair<T, T>> {
  private:
    const T INF = std::numeric_limits<T>::max() - 65;
    bool isMergeAdjacent;

  public:
    /**
     * @brief Construct a new Interval Set object
     *
     * @param f if merge adjacent interval
     */
    IntervalSet(bool f) : isMergeAdjacent(f) {
        this->emplace(-INF - 1, -INF);
        this->emplace(INF, INF + 1);
    }

    auto get(T p) const {
        auto it = this->upper_bound({p, INF});
        if(it == this->begin() || (--it)->second < p)
            return this->end();
        return it;
    }

    void insert(T l, T r) {
        auto it = std::prev(this->lower_bound({l, r}));
        if(it->first <= l && l + !isMergeAdjacent <= it->second) {
            l = std::min(l, it->first);
            r = std::max(r, it->second);
            this->erase(it);
        }
        it = this->lower_bound({l, r});
        while(1) {
            if(l <= it->first && it->first <= r) {
                r = std::max(r, it->second);
                it = this->erase(it);
            } else
                break;
        }
        this->emplace(l, r);
    }

    void remove(T l, T r) {}

    bool same(T p, T q) const {
        auto it = get(p);
        return it != this->end() && it->first <= q && q <= it->second;
    }
};

int main() {
    long long d, q;
    std::cin >> d >> q;
    IntervalSet<long long> st(true);
    long long res = 0;
    for(int i = 0; i < q; i++) {
        long long x, y;
        std::cin >> x >> y;
        y++;
        st.insert(x, y);
        auto it = st.get(x);
        res = std::max(it->second - it->first, res);
        std::cout << res << std::endl;
    }
}
0