結果

問題 No.2421 entersys?
ユーザー 👑 emthrmemthrm
提出日時 2023-08-12 13:57:03
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 609 ms / 3,000 ms
コード長 7,950 bytes
コンパイル時間 4,105 ms
コンパイル使用メモリ 282,648 KB
実行使用メモリ 45,576 KB
最終ジャッジ日時 2024-04-30 04:31:32
合計ジャッジ時間 14,749 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 454 ms
33,020 KB
testcase_12 AC 442 ms
32,956 KB
testcase_13 AC 432 ms
33,020 KB
testcase_14 AC 436 ms
32,896 KB
testcase_15 AC 437 ms
32,896 KB
testcase_16 AC 509 ms
38,652 KB
testcase_17 AC 539 ms
38,528 KB
testcase_18 AC 516 ms
38,532 KB
testcase_19 AC 490 ms
38,652 KB
testcase_20 AC 477 ms
38,656 KB
testcase_21 AC 436 ms
36,856 KB
testcase_22 AC 351 ms
32,740 KB
testcase_23 AC 609 ms
45,576 KB
testcase_24 AC 602 ms
45,516 KB
testcase_25 AC 609 ms
45,496 KB
testcase_26 AC 242 ms
23,164 KB
testcase_27 AC 250 ms
23,164 KB
testcase_28 AC 252 ms
23,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int 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 IntervalsManagedBySet {
  using IntervalsType = std::set<std::pair<T, T>>;
  IntervalsType intervals{
      {std::numeric_limits<T>::lowest(), std::numeric_limits<T>::lowest()},
      {std::numeric_limits<T>::max(), std::numeric_limits<T>::max()}};

  IntervalsManagedBySet() = default;

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

  bool contains(const T left, const T right) const {
    return find(left, right) != intervals.end();
  }

  std::pair<typename IntervalsType::const_iterator, bool> erase(const T x) {
    typename IntervalsType::const_iterator it = intervals.lower_bound({x, x});
    if (it->first == x) {
      const T right = it->second;
      it = intervals.erase(it);
      if (x + 1 <= right) it = intervals.emplace(x + 1, right).first;
    } else {
      it = std::prev(it);
      const auto [left, right] = *it;
      if (right < x) return {std::next(it), false};
      intervals.erase(it);
      it = std::next(intervals.emplace(left, x - 1).first);
      if (x + 1 <= right) it = intervals.emplace(x + 1, right).first;
    }
    return {it, true};
  }

  std::pair<typename IntervalsType::const_iterator, T> erase(
      const T left, const T right) {
    assert(left <= right);
    typename IntervalsType::const_iterator it =
        intervals.lower_bound({left, left});
    T res = 0;
    for (; it->second <= right; it = intervals.erase(it)) {
      res += it->second - it->first + 1;
    }
    if (it->first <= right) {
      res += right - it->first + 1;
      const T r = it->second;
      intervals.erase(it);
      it = intervals.emplace(right + 1, r).first;
    }
    if (left <= std::prev(it)->second) {
      it = std::prev(it);
      const auto [l, r] = *it;
      intervals.erase(it);
      if (right < r) {
        res += right - left + 1;
        intervals.emplace(right + 1, r);
      } else {
        res += r - left + 1;
      }
      it = std::next(intervals.emplace(l, left - 1).first);
    }
    return {it, res};
  }

  typename IntervalsType::const_iterator find(const T x) const {
    return find(x, x);
  }

  typename IntervalsType::const_iterator find(
      const T left, const T right) const {
    typename IntervalsType::const_iterator it =
        intervals.lower_bound({left, left});
    if (left < it->first) it = std::prev(it);
    return it->first <= left && right <= it->second ? it : intervals.end();
  }

  std::pair<typename IntervalsType::const_iterator, bool> insert(const T x) {
    typename IntervalsType::const_iterator it = intervals.lower_bound({x, x});
    if (it->first == x) return {it, false};
    if (x <= std::prev(it)->second) return {std::prev(it), false};
    T left = x, right = x;
    if (x + 1 == it->first) {
      right = it->second;
      it = intervals.erase(it);
    }
    if (std::prev(it)->second == x - 1) {
      it = std::prev(it);
      left = it->first;
      intervals.erase(it);
    }
    return {intervals.emplace(left, right).first, true};
  }

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

  T mex(const T x = 0) const {
    auto it = intervals.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 IntervalsManagedBySet& x) {
    if (x.intervals.size() == 2) return os;
    auto it = next(x.intervals.begin());
    while (true) {
      os << '[' << it->first << ", " << it->second << ']';
      it = next(it);
      if (next(it) == x.intervals.end()) break;
      os << ' ';
    }
    return os;
  }
};

template <typename Abelian>
struct FenwickTreeSupportingRangeAddQuery {
  explicit FenwickTreeSupportingRangeAddQuery(
      const int n_, const Abelian ID = 0)
      : n(n_ + 1), ID(ID) {
    data_const.assign(n, ID);
    data_linear.assign(n, ID);
  }

  void add(int left, const int right, const Abelian val) {
    if (right < ++left) [[unlikely]] return;
    for (int i = left; i < n; i += i & -i) {
      data_const[i] -= val * (left - 1);
      data_linear[i] += val;
    }
    for (int i = right + 1; i < n; i += i & -i) {
      data_const[i] += val * right;
      data_linear[i] -= val;
    }
  }

  Abelian sum(const int idx) const {
    Abelian res = ID;
    for (int i = idx; i > 0; i -= i & -i) {
      res += data_linear[i];
    }
    res *= idx;
    for (int i = idx; i > 0; i -= i & -i) {
      res += data_const[i];
    }
    return res;
  }

  Abelian sum(const int left, const int right) const {
    return left < right ? sum(right) - sum(left) : ID;
  }

  Abelian operator[](const int idx) const { return sum(idx, idx + 1); }

 private:
  const int n;
  const Abelian ID;
  std::vector<Abelian> data_const, data_linear;
};

int main() {
  int n; cin >> n;
  vector<int> type(n, 3), l(n), r(n);
  vector<string> x(n);
  REP(i, n) cin >> x[i] >> l[i] >> r[i];
  int q; cin >> q;
  type.resize(n + q);
  l.resize(n + q);
  r.resize(n + q);
  x.resize(n + q);
  vector<int> t(n + q);
  REP(i, q) {
    cin >> type[i + n];
    if (type[i + n] == 1) {
      cin >> x[i + n] >> t[i + n];
    } else if (type[i + n] == 2) {
      cin >> t[i + n];
    } else if (type[i + n] == 3) {
      cin >> x[i + n] >> l[i + n] >> r[i + n];
    }
  }
  vector<int> times;
  REP(i, n + q) {
    if (type[i] == 1 || type[i] == 2) {
      times.emplace_back(t[i]);
    } else if (type[i] == 3) {
      times.emplace_back(l[i]);
      times.emplace_back(r[i]);
    }
  }
  ranges::sort(times);
  times.erase(unique(times.begin(), times.end()), times.end());
  const int len = times.size();
  map<string, IntervalsManagedBySet<int>> imbs;
  FenwickTreeSupportingRangeAddQuery<ll> bit(len);
  REP(i, n + q) {
    if (type[i] == 1) {
      cout << (imbs[x[i]].contains(t[i]) ? "Yes\n" : "No\n");
    } else if (type[i] == 2) {
      cout << bit[distance(times.begin(), ranges::lower_bound(times, t[i]))] << '\n';
    } else if (type[i] == 3) {
      imbs[x[i]].insert(l[i], r[i]);
      bit.add(distance(times.begin(), ranges::lower_bound(times, l[i])),
              distance(times.begin(), ranges::lower_bound(times, r[i])) + 1,
              1);
    }
  }
  return 0;
}
0