This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub suisen-cp/cp-library-cpp
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DSL_4_A" #include <iostream> #include <tuple> #include "library/util/coordinate_compressor.hpp" #include "library/datastructure/util/range_set.hpp" using suisen::CoordinateCompressorBuilder; using suisen::RangeSet; constexpr int L = 31; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; CoordinateCompressorBuilder<long long> builder; std::vector<std::tuple<long long, long long, long long, long long>> rectangles(n); for (int i = 0; i < n; ++i) { int xl, yl, xr, yr; std::cin >> xl >> yl >> xr >> yr; rectangles[i] = { xl, yl, xr, yr }; builder.push(yl); builder.push(yr); } auto comp_y = builder.build(); int m = comp_y.size(); std::vector<RangeSet<long long>> sets(m); for (const auto &[xl, yl, xr, yr] : rectangles) { int cyl = comp_y[yl], cyr = comp_y[yr]; for (int i = cyl; i < cyr; ++i) { sets[i].insert(xl, xr - 1); } } long long ans = 0; for (int i = 0; i < m - 1; ++i) { long long hight = comp_y.decomp(i + 1) -comp_y.decomp(i); ans += hight * sets[i].size(); } std::cout << ans << std::endl; return 0; }
#line 1 "test/src/datastructure/util/range_set/DSL_4_A.test.cpp" #define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DSL_4_A" #include <iostream> #include <tuple> #line 1 "library/util/coordinate_compressor.hpp" #include <algorithm> #include <cassert> #include <vector> #line 1 "library/type_traits/type_traits.hpp" #include <limits> #line 6 "library/type_traits/type_traits.hpp" #include <type_traits> namespace suisen { template <typename ...Constraints> using constraints_t = std::enable_if_t<std::conjunction_v<Constraints...>, std::nullptr_t>; template <typename T, typename = std::nullptr_t> struct bitnum { static constexpr int value = 0; }; template <typename T> struct bitnum<T, constraints_t<std::is_integral<T>>> { static constexpr int value = std::numeric_limits<std::make_unsigned_t<T>>::digits; }; template <typename T> static constexpr int bitnum_v = bitnum<T>::value; template <typename T, size_t n> struct is_nbit { static constexpr bool value = bitnum_v<T> == n; }; template <typename T, size_t n> static constexpr bool is_nbit_v = is_nbit<T, n>::value; template <typename T, typename = std::nullptr_t> struct safely_multipliable { using type = T; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 32>>> { using type = long long; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 64>>> { using type = __int128_t; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 32>>> { using type = unsigned long long; }; template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 64>>> { using type = __uint128_t; }; template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type; template <typename T, typename = void> struct rec_value_type { using type = T; }; template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> { using type = typename rec_value_type<typename T::value_type>::type; }; template <typename T> using rec_value_type_t = typename rec_value_type<T>::type; template <typename T> class is_iterable { template <typename T_> static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_iterable_v = is_iterable<T>::value; template <typename T> class is_writable { template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::ostream&>() << e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_writable_v = is_writable<T>::value; template <typename T> class is_readable { template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::istream&>() >> e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval<T>()))::value; }; template <typename T> static constexpr bool is_readable_v = is_readable<T>::value; } // namespace suisen #line 9 "library/util/coordinate_compressor.hpp" namespace suisen { template <typename T> class CoordinateCompressorBuilder { public: struct Compressor { public: static constexpr int absent = -1; // default constructor Compressor() : _xs(std::vector<T>{}) {} // Construct from strictly sorted vector Compressor(const std::vector<T> &xs) : _xs(xs) { assert(is_strictly_sorted(xs)); } // Return the number of distinct keys. int size() const { return _xs.size(); } // Check if the element is registered. bool has_key(const T &e) const { return std::binary_search(_xs.begin(), _xs.end(), e); } // Compress the element. if not registered, returns `default_value`. (default: Compressor::absent) int comp(const T &e, int default_value = absent) const { const int res = min_geq_index(e); return res != size() and _xs[res] == e ? res : default_value; } // Restore the element from the index. T decomp(const int compressed_index) const { return _xs[compressed_index]; } // Compress the element. Equivalent to call `comp(e)` int operator[](const T &e) const { return comp(e); } // Return the minimum registered value greater than `e`. if not exists, return `default_value`. T min_gt(const T &e, const T &default_value) const { auto it = std::upper_bound(_xs.begin(), _xs.end(), e); return it == _xs.end() ? default_value : *it; } // Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`. T min_geq(const T &e, const T &default_value) const { auto it = std::lower_bound(_xs.begin(), _xs.end(), e); return it == _xs.end() ? default_value : *it; } // Return the maximum registered value less than `e`. if not exists, return `default_value` T max_lt(const T &e, const T &default_value) const { auto it = std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>()); return it == _xs.rend() ? default_value : *it; } // Return the maximum registered value less than or equal to `e`. if not exists, return `default_value` T max_leq(const T &e, const T &default_value) const { auto it = std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>()); return it == _xs.rend() ? default_value : *it; } // Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`. int min_gt_index(const T &e) const { return std::upper_bound(_xs.begin(), _xs.end(), e) - _xs.begin(); } // Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`. int min_geq_index(const T &e) const { return std::lower_bound(_xs.begin(), _xs.end(), e) - _xs.begin(); } // Return the compressed index of the maximum registered value less than `e`. if not exists, return -1. int max_lt_index(const T &e) const { return int(_xs.rend() - std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1; } // Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1. int max_leq_index(const T &e) const { return int(_xs.rend() - std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1; } private: std::vector<T> _xs; static bool is_strictly_sorted(const std::vector<T> &v) { return std::adjacent_find(v.begin(), v.end(), std::greater_equal<T>()) == v.end(); } }; CoordinateCompressorBuilder() : _xs(std::vector<T>{}) {} explicit CoordinateCompressorBuilder(const std::vector<T> &xs) : _xs(xs) {} explicit CoordinateCompressorBuilder(std::vector<T> &&xs) : _xs(std::move(xs)) {} template <typename Gen, constraints_t<std::is_invocable_r<T, Gen, int>> = nullptr> CoordinateCompressorBuilder(const int n, Gen generator) { reserve(n); for (int i = 0; i < n; ++i) push(generator(i)); } // Attempt to preallocate enough memory for specified number of elements. void reserve(int n) { _xs.reserve(n); } // Add data. void push(const T &first) { _xs.push_back(first); } // Add data. void push(T &&first) { _xs.push_back(std::move(first)); } // Add data in the range of [first, last). template <typename Iterator> auto push(const Iterator &first, const Iterator &last) -> decltype(std::vector<T>{}.push_back(*first), void()) { for (auto it = first; it != last; ++it) _xs.push_back(*it); } // Add all data in the container. Equivalent to `push(iterable.begin(), iterable.end())`. template <typename Iterable> auto push(const Iterable &iterable) -> decltype(std::vector<T>{}.push_back(*iterable.begin()), void()) { push(iterable.begin(), iterable.end()); } // Add data. template <typename ...Args> void emplace(Args &&...args) { _xs.emplace_back(std::forward<Args>(args)...); } // Build compressor. auto build() { std::sort(_xs.begin(), _xs.end()), _xs.erase(std::unique(_xs.begin(), _xs.end()), _xs.end()); return Compressor {_xs}; } // Build compressor from vector. static auto build(const std::vector<T> &xs) { return CoordinateCompressorBuilder(xs).build(); } // Build compressor from vector. static auto build(std::vector<T> &&xs) { return CoordinateCompressorBuilder(std::move(xs)).build(); } // Build compressor from generator. template <typename Gen, constraints_t<std::is_invocable_r<T, Gen, int>> = nullptr> static auto build(const int n, Gen generator) { return CoordinateCompressorBuilder<T>(n, generator).build(); } private: std::vector<T> _xs; }; } // namespace suisen #line 1 "library/datastructure/util/range_set.hpp" #include <map> namespace suisen { template <typename T, bool merge_adjacent_segment = true> struct RangeSet : public std::map<T, T> { public: RangeSet() : _size(0) {} // returns the number of intergers in this set (not the number of ranges). O(1) T size() const { return number_of_elements(); } // returns the number of intergers in this set (not the number of ranges). O(1) T number_of_elements() const { return _size; } // returns the number of ranges in this set (not the number of integers). O(1) int number_of_ranges() const { return std::map<T, T>::size(); } // returns whether the given integer is in this set or not. O(log N) bool contains(T x) const { auto it = this->upper_bound(x); return it != this->begin() and x <= std::prev(it)->second; } /** * returns the iterator pointing to the range [l, r] in this set s.t. l <= x <= r. * if such a range does not exist, returns `end()`. * O(log N) */ auto find_range(T x) const { auto it = this->upper_bound(x); return it != this->begin() and x <= (--it)->second ? it : this->end(); } // returns whether `x` and `y` is in this set and in the same range. O(log N) bool in_the_same_range(T x, T y) const { auto it = get_containing_range(x); return it != this->end() and it->first <= y and y <= it->second; } // inserts the range [x, x] and returns the number of integers inserted to this set. O(log N) T insert(T x) { return insert(x, x); } // inserts the range [l, r] and returns the number of integers inserted to this set. amortized O(log N) T insert(T l, T r) { if (l > r) return 0; auto it = this->upper_bound(l); if (it != this->begin() and is_mergeable(std::prev(it)->second, l)) { it = std::prev(it); l = std::min(l, it->first); } T inserted = 0; for (; it != this->end() and is_mergeable(r, it->first); it = std::map<T, T>::erase(it)) { auto [cl, cr] = *it; r = std::max(r, cr); inserted -= cr - cl + 1; } inserted += r - l + 1; (*this)[l] = r; _size += inserted; return inserted; } // erases the range [x, x] and returns the number of intergers erased from this set. O(log N) T erase(T x) { return erase(x, x); } // erases the range [l, r] and returns the number of intergers erased from this set. amortized O(log N) T erase(T l, T r) { if (l > r) return 0; T tl = l, tr = r; auto it = this->upper_bound(l); if (it != this->begin() and l <= std::prev(it)->second) { it = std::prev(it); tl = it->first; } T erased = 0; for (; it != this->end() and it->first <= r; it = std::map<T, T>::erase(it)) { auto [cl, cr] = *it; tr = cr; erased += cr - cl + 1; } if (tl < l) { (*this)[tl] = l - 1; erased -= l - tl; } if (r < tr) { (*this)[r + 1] = tr; erased -= tr - r; } _size -= erased; return erased; } // returns minimum integer x s.t. x >= lower and x is NOT in this set T minimum_excluded(T lower = 0) const { static_assert(merge_adjacent_segment); auto it = find_range(lower); return it == this->end() ? lower : it->second + 1; } // returns maximum integer x s.t. x <= upper and x is NOT in this set T maximum_excluded(T upper) const { static_assert(merge_adjacent_segment); auto it = find_range(upper); return it == this->end() ? upper : it->first - 1; } private: T _size; bool is_mergeable(T cur_r, T next_l) { return next_l <= cur_r + merge_adjacent_segment; } }; } // namespace suisen #line 8 "test/src/datastructure/util/range_set/DSL_4_A.test.cpp" using suisen::CoordinateCompressorBuilder; using suisen::RangeSet; constexpr int L = 31; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; CoordinateCompressorBuilder<long long> builder; std::vector<std::tuple<long long, long long, long long, long long>> rectangles(n); for (int i = 0; i < n; ++i) { int xl, yl, xr, yr; std::cin >> xl >> yl >> xr >> yr; rectangles[i] = { xl, yl, xr, yr }; builder.push(yl); builder.push(yr); } auto comp_y = builder.build(); int m = comp_y.size(); std::vector<RangeSet<long long>> sets(m); for (const auto &[xl, yl, xr, yr] : rectangles) { int cyl = comp_y[yl], cyr = comp_y[yr]; for (int i = cyl; i < cyr; ++i) { sets[i].insert(xl, xr - 1); } } long long ans = 0; for (int i = 0; i < m - 1; ++i) { long long hight = comp_y.decomp(i + 1) -comp_y.decomp(i); ans += hight * sets[i].size(); } std::cout << ans << std::endl; return 0; }