結果

問題 No.674 n連勤
ユーザー 👑 emthrmemthrm
提出日時 2021-02-22 03:28:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 4,566 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 210,588 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-19 23:46:07
合計ジャッジ時間 3,487 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T>
struct SetManagedByInterval {
  std::set<std::pair<T, T>> interval{
    {std::numeric_limits<T>::lowest(), std::numeric_limits<T>::lowest()},
    {std::numeric_limits<T>::max(), std::numeric_limits<T>::max()},
  };

  bool contains(T x) const { return contains(x, x); }

  bool contains(T left, T right) const {
    auto it = interval.lower_bound({left, left});
    if (left < it->first) it = std::prev(it);
    return it->first <= left && right <= it->second;
  }

  bool erase(T x) {
    auto it = interval.lower_bound({x, x});
    if (it->first == x) {
      T right = it->second;
      interval.erase(it);
      if (x + 1 <= right) interval.emplace(x + 1, right);
      return true;
    }
    it = std::prev(it);
    T left, right; std::tie(left, right) = *it;
    if (right < x) return false;
    interval.erase(it);
    interval.emplace(left, x - 1);
    if (x + 1 <= right) interval.emplace(x + 1, right);
    return true;
  }

  T erase(T left, T right) {
    assert(left <= right);
    auto it = interval.lower_bound({left, left});
    T res = 0;
    for (; it->second <= right; it = interval.erase(it)) res += it->second - it->first + 1;
    if (it->first <= right) {
      res += right - it->first + 1;
      T r = it->second;
      interval.erase(it);
      it = interval.emplace(right + 1, r).first;
    }
    if (left <= std::prev(it)->second) {
      it = std::prev(it);
      res += it->second - left + 1;
      T l = it->first;
      interval.erase(it);
      interval.emplace(l, left - 1);
    }
    return res;
  }

  bool insert(T x) {
    auto it = interval.lower_bound({x, x});
    if (it->first == x || x <= std::prev(it)->second) return false;
    T left = x, right = x;
    if (x + 1 == it->first) {
      right = it->second;
      it = interval.erase(it);
    }
    if (std::prev(it)->second == x - 1) {
      it = std::prev(it);
      left = it->first;
      interval.erase(it);
    }
    interval.emplace(left, right);
    return true;
  }

  T insert(T left, T right) {
    assert(left <= right);
    auto it = interval.lower_bound({left, left});
    if (left <= std::prev(it)->second) {
      it = std::prev(it);
      left = it->first;
    }
    if (left == it->first && right <= it->second) return 0;
    T res = 0;
    for (; it->second <= right; it = interval.erase(it)) res -= it->second - it->first + 1;
    if (it->first <= right) {
      res -= it->second - it->first + 1;
      right = it->second;
      it = interval.erase(it);
    }
    res += right - left + 1;
    if (right + 1 == it->first) {
      right = it->second;
      it = interval.erase(it);
    }
    if (std::prev(it)->second == left - 1) {
      it = std::prev(it);
      left = it->first;
      interval.erase(it);
    }
    interval.emplace(left, right);
    return res;
  }

  T mex(T x = 0) const {
    auto it = interval.lower_bound({x, x});
    if (x <= std::prev(it)->second) it = std::prev(it);
    return x < it->first ? x : it->second + 1;
  }

  friend std::ostream &operator<<(std::ostream &os, const SetManagedByInterval &st) {
    if (st.interval.size() == 2) return os;
    auto it = next(st.interval.begin());
    while (true) {
      os << '[' << it->first << ", " << it->second << ']';
      it = next(it);
      if (next(it) == st.interval.end()) {
        break;
      } else {
        os << ' ';
      }
    }
    return os;
  }
};

int main() {
  ll d; int q; cin >> d >> q;
  SetManagedByInterval<ll> st;
  ll ans = 0;
  while (q--) {
    ll a, b; cin >> a >> b;
    st.insert(a, b);
    auto [l, r] = *prev(st.interval.lower_bound({a + 1, a + 1}));
    chmax(ans, r - l + 1);
    cout << ans << '\n';
  }
  return 0;
}
0