This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub suisen-cp/cp-library-cpp
#include "library/geom/segment_intersections.hpp"
#ifndef SUISEN_SEGMENT_INTERSECTIONS #define SUISEN_SEGMENT_INTERSECTIONS #include <iostream> #include "library/util/coordinate_compressor.hpp" #include "library/datastructure/fenwick_tree/fenwick_tree.hpp" namespace suisen::geometry { template <typename T> long long segment_intersections(std::vector<std::pair<T, std::pair<T, T>>> vertical, std::vector<std::pair<std::pair<T, T>, T>> horizontal) { CoordinateCompressorBuilder<T> bx, by; for (const auto &[x, range_y] : vertical) bx.push(x); for (const auto &[range_x, y] : horizontal) by.push(y); const auto cx = bx.build(); const auto cy = by.build(); const int n = cx.size(), m = cy.size(); std::vector<std::vector<std::pair<int, int>>> queries(n); for (const auto &[x, range_y] : vertical) { auto [yl, yr] = range_y; if (yl > yr) std::swap(yl, yr); queries[cx[x]].emplace_back(cy.min_geq_index(yl), cy.min_gt_index(yr)); } std::vector<std::vector<int>> in(n + 1), out(n + 1); for (const auto &[range_x, y] : horizontal) { auto [xl, xr] = range_x; if (xl > xr) std::swap(xl, xr); in[cx.min_geq_index(xl)].push_back(cy[y]); out[cx.min_gt_index(xr)].push_back(cy[y]); } FenwickTree<int> ft(m); long long ans = 0; for (int x = 0; x < n; ++x) { for (int y : in[x]) ++ft[y]; for (int y : out[x]) --ft[y]; for (auto [yl, yr] : queries[x]) ans += ft(yl, yr); } return ans; } } // namespace suisen::geometry #endif // SUISEN_SEGMENT_INTERSECTIONS
#line 1 "library/geom/segment_intersections.hpp" #include <iostream> #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/fenwick_tree/fenwick_tree.hpp" #line 5 "library/datastructure/fenwick_tree/fenwick_tree.hpp" #include <map> #include <unordered_map> namespace suisen { namespace internal { template <typename T, typename index_t = int, typename Container = std::vector<T>> class FenwickTreeBase { public: FenwickTreeBase() = default; explicit FenwickTreeBase(index_t n) : n(n) {} int size() const { return n; } void add(index_t i, T v) { for (++i; i <= n; i += (i & -i)) data[i - 1] += v; } T sum(index_t l, index_t r) const { return sum(r) - sum(l); } auto operator[](int i) { struct { int i; FenwickTreeBase& ft; operator T() const { return ft.sum(i, i + 1); } auto& operator++() { return *this += 1; } auto& operator--() { return *this -= 1; } auto& operator+=(T val) { ft.add(i, val); return *this; } auto& operator-=(T val) { ft.add(i, -val); return *this; } auto& operator*=(T val) { T cur = ft.sum(i, i + 1); ft.add(i, cur * val - cur); return *this; } auto& operator/=(T val) { T cur = ft.sum(i, i + 1); ft.add(i, cur / val - cur); return *this; } auto& operator%=(T val) { T cur = ft.sum(i, i + 1); ft.add(i, cur % val - cur); return *this; } auto& operator =(T val) { T cur = ft.sum(i, i + 1); ft.add(i, val - cur); return *this; } } obj{ i, *this }; return obj; } T operator()(int l, int r) const { return sum(l, r); } Container& get_internal_container() { return data; } protected: index_t n; Container data; template <typename ...Args> FenwickTreeBase(index_t n, Args &&...args) : n(n), data(std::forward<Args>(args)...) {} private: T sum(int r) const { T s{}; for (; r; r -= r & -r) s += data[r - 1]; return s; } }; template <typename Key, typename Value, bool unordered> using cond_map_t = std::conditional_t<unordered, std::unordered_map<Key, Value>, std::map<Key, Value>>; } // namespace internal template <typename T> struct FenwickTree : public internal::FenwickTreeBase<T> { FenwickTree() : FenwickTree(0) {} explicit FenwickTree(int n) : internal::FenwickTreeBase<T>::FenwickTreeBase(n, n, T{}) {} explicit FenwickTree(std::vector<T>&& a) : internal::FenwickTreeBase<T>::FenwickTreeBase(a.size(), std::move(a)) { for (int i = 1; i <= this->n; ++i) { int p = i + (i & -i); if (p <= this->n) this->data[p - 1] += this->data[i - 1]; } } explicit FenwickTree(const std::vector<T>& a) : FenwickTree(std::vector<T>(a)) {} }; template <typename T, typename index_t, bool use_unordered_map = false> using MapFenwickTree = internal::FenwickTreeBase<T, index_t, internal::cond_map_t<index_t, T, use_unordered_map>>; } // namespace suisen #line 8 "library/geom/segment_intersections.hpp" namespace suisen::geometry { template <typename T> long long segment_intersections(std::vector<std::pair<T, std::pair<T, T>>> vertical, std::vector<std::pair<std::pair<T, T>, T>> horizontal) { CoordinateCompressorBuilder<T> bx, by; for (const auto &[x, range_y] : vertical) bx.push(x); for (const auto &[range_x, y] : horizontal) by.push(y); const auto cx = bx.build(); const auto cy = by.build(); const int n = cx.size(), m = cy.size(); std::vector<std::vector<std::pair<int, int>>> queries(n); for (const auto &[x, range_y] : vertical) { auto [yl, yr] = range_y; if (yl > yr) std::swap(yl, yr); queries[cx[x]].emplace_back(cy.min_geq_index(yl), cy.min_gt_index(yr)); } std::vector<std::vector<int>> in(n + 1), out(n + 1); for (const auto &[range_x, y] : horizontal) { auto [xl, xr] = range_x; if (xl > xr) std::swap(xl, xr); in[cx.min_geq_index(xl)].push_back(cy[y]); out[cx.min_gt_index(xr)].push_back(cy[y]); } FenwickTree<int> ft(m); long long ans = 0; for (int x = 0; x < n; ++x) { for (int y : in[x]) ++ft[y]; for (int y : out[x]) --ft[y]; for (auto [yl, yr] : queries[x]) ans += ft(yl, yr); } return ans; } } // namespace suisen::geometry