Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:warning: test/atcoder/abc256_d.test.cpp

Depends on

Code

// competitive-verifier: IGNORE
// competitive-verifier: PROBLEM https://atcoder.jp/contests/abc256/tasks/abc256_d
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include "src/DataStructure/RangeSet.hpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N;
 cin >> N;
 RangeSet<int> rs;
 for (int i= 0; i < N; ++i) {
  int L, R;
  cin >> L >> R;
  rs.insert(L, R - 1);
 }
 for (auto [l, r]: rs) cout << l << " " << r + 1 << '\n';
 return 0;
}
#line 1 "test/atcoder/abc256_d.test.cpp"
// competitive-verifier: IGNORE
// competitive-verifier: PROBLEM https://atcoder.jp/contests/abc256/tasks/abc256_d
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#line 3 "src/DataStructure/RangeSet.hpp"
#include <set>
#include <iterator>
#include <limits>
#include <cassert>
template <class Int, bool merge= true> class RangeSet {
 struct ClosedSection {
  Int l, r;
  Int length() const { return r - l + 1; }
  bool operator<(const ClosedSection &cs) const { return l < cs.l || (l == cs.l && r > cs.r); }
  operator bool() const { return l <= r; }
  friend std::ostream &operator<<(std::ostream &os, const ClosedSection &cs) { return cs ? os << "[" << cs.l << "," << cs.r << "]" : os << "∅"; }
 };
 std::set<ClosedSection> mp;
public:
 RangeSet() {
  constexpr Int INF= std::numeric_limits<Int>::max() / 2;
  mp.insert({INF, INF}), mp.insert({-INF, -INF});
 }
 ClosedSection covered_by(Int l, Int r) const {
  assert(l <= r);
  if (auto it= std::prev(mp.upper_bound(ClosedSection{l, l})); it->l <= l && r <= it->r) return *it;
  return {1, 0};
 }
 ClosedSection covered_by(Int x) const { return covered_by(x, x); }
 ClosedSection covered_by(const ClosedSection &cs) const { return covered_by(cs.l, cs.r); }
 size_t size() const { return mp.size() - 2; }
 auto begin() const { return std::next(mp.begin()); }
 auto end() const { return std::prev(mp.end()); }
 Int insert(Int l, Int r) {
  assert(l <= r);
  auto it= std::prev(mp.upper_bound(ClosedSection{l, l}));
  Int sum= 0, x= it->l, y= it->r;
  if (x <= l && r <= y) return sum;
  if (x <= l && l <= y + merge) sum+= y - (l= x) + 1, it= mp.erase(it);
  else std::advance(it, 1);
  for (; it->r < r; it= mp.erase(it)) sum+= it->r - it->l + 1;
  if (x= it->l, y= it->r; x - merge <= r && r <= y) sum+= (r= y) - x + 1, mp.erase(it);
  return mp.insert({l, r}), r - l + 1 - sum;
 }
 Int insert(Int x) { return insert(x, x); }
 Int insert(const ClosedSection &cs) { return insert(cs.l, cs.r); }
 Int erase(Int l, Int r) {
  assert(l <= r);
  auto it= std::prev(mp.upper_bound(ClosedSection{l, l}));
  Int sum= 0, x= it->l, y= it->r;
  if (x <= l && r <= y) {
   if (mp.erase(it); x < l) mp.insert({x, l - 1});
   if (r < y) mp.insert({r + 1, y});
   return r - l + 1;
  }
  if (x <= l && l <= y) {
   if (x < l) mp.insert({x, l - 1});
   sum+= y - l + 1, it= mp.erase(it);
  } else std::advance(it, 1);
  for (; it->r <= r; it= mp.erase(it)) sum+= it->r - it->l + 1;
  if (x= it->l, y= it->r; x <= r && r <= y)
   if (sum+= r - x + 1, mp.erase(it); r < y) mp.insert({r + 1, y});
  return sum;
 }
 Int erase(Int x) { return erase(x, x); }
 Int erase(const ClosedSection &cs) { return erase(cs.l, cs.r); }
 Int mex(Int x) const {
  auto cs= covered_by(x);
  return cs ? cs.r + 1 : x;
 }
 friend std::ostream &operator<<(std::ostream &os, const RangeSet &rs) {
  os << "[";
  for (auto it= rs.begin(); it != rs.end(); ++it) os << (it == rs.begin() ? "" : ",") << *it;
  return os << "]";
 }
};
#line 7 "test/atcoder/abc256_d.test.cpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N;
 cin >> N;
 RangeSet<int> rs;
 for (int i= 0; i < N; ++i) {
  int L, R;
  cin >> L >> R;
  rs.insert(L, R - 1);
 }
 for (auto [l, r]: rs) cout << l << " " << r + 1 << '\n';
 return 0;
}
Back to top page