#line 2 "library/other/template.hpp" #include #line 2 "library/template/macros.hpp" #line 4 "library/template/macros.hpp" #ifndef __COUNTER__ #define __COUNTER__ __LINE__ #endif #define OVERLOAD5(a, b, c, d, e, ...) e #define REP1_0(b, c) REP1_1(b, c) #define REP1_1(b, c) \ for (ll REP_COUNTER_##c = 0; REP_COUNTER_##c < (ll)(b); ++REP_COUNTER_##c) #define REP1(b) REP1_0(b, __COUNTER__) #define REP2(i, b) for (ll i = 0; i < (ll)(b); ++i) #define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c)) #define rep(...) OVERLOAD5(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__) #define RREP2(i, a) for (ll i = (ll)(a)-1; i >= 0; --i) #define RREP3(i, a, b) for (ll i = (ll)(a)-1; i >= (ll)(b); --i) #define RREP4(i, a, b, c) for (ll i = (ll)(a)-1; i >= (ll)(b); i -= (ll)(c)) #define rrep(...) OVERLOAD5(__VA_ARGS__, RREP4, RREP3, RREP2)(__VA_ARGS__) #define REPS2(i, b) for (ll i = 1; i <= (ll)(b); ++i) #define REPS3(i, a, b) for (ll i = (ll)(a) + 1; i <= (ll)(b); ++i) #define REPS4(i, a, b, c) for (ll i = (ll)(a) + 1; i <= (ll)(b); i += (ll)(c)) #define reps(...) OVERLOAD5(__VA_ARGS__, REPS4, REPS3, REPS2)(__VA_ARGS__) #define RREPS2(i, a) for (ll i = (ll)(a); i > 0; --i) #define RREPS3(i, a, b) for (ll i = (ll)(a); i > (ll)(b); --i) #define RREPS4(i, a, b, c) for (ll i = (ll)(a); i > (ll)(b); i -= (ll)(c)) #define rreps(...) OVERLOAD5(__VA_ARGS__, RREPS4, RREPS3, RREPS2)(__VA_ARGS__) #define each_for(...) for (auto&& __VA_ARGS__) #define each_const(...) for (const auto& __VA_ARGS__) #define all(v) std::begin(v), std::end(v) #if __cplusplus >= 201402L #define rall(v) std::rbegin(v), std::rend(v) #else #define rall(v) v.rbegin(), v.rend() #endif #if __cpp_constexpr >= 201304L #define CONSTEXPR constexpr #else #define CONSTEXPR #endif #if __cpp_if_constexpr >= 201606L #define IF_CONSTEXPR constexpr #else #define IF_CONSTEXPR #endif #define IO_BUFFER_SIZE 2048 #line 2 "library/template/alias.hpp" #line 4 "library/template/alias.hpp" using ll = long long; using uint = unsigned int; using ull = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using ld = long double; using PLL = std::pair; template using prique = std::priority_queue, std::greater>; template struct infinity { static constexpr T value = std::numeric_limits::max() / 2; static constexpr T mvalue = std::numeric_limits::lowest() / 2; static constexpr T max = std::numeric_limits::max(); static constexpr T min = std::numeric_limits::lowest(); }; #if __cplusplus <= 201402L template constexpr T infinity::value; template constexpr T infinity::mvalue; template constexpr T infinity::max; template constexpr T infinity::min; #endif #if __cpp_variable_templates >= 201304L template constexpr T INF = infinity::value; #endif constexpr ll inf = infinity::value; constexpr ld EPS = 1e-8; constexpr ld PI = 3.1415926535897932384626; #line 2 "library/template/type_traits.hpp" #line 5 "library/template/type_traits.hpp" template struct function_traits_impl { using result_type = T; template using argument_type = typename std::tuple_element>::type; using argument_tuple = std::tuple; static constexpr std::size_t arg_size() { return sizeof...(Args); } }; template struct function_traits_helper; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; #if __cpp_noexcept_function_type >= 201510L template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; #endif template using function_traits = typename function_traits_helper< decltype(&std::remove_reference::type::operator())>::type; template using function_result_type = typename function_traits::result_type; template using function_argument_type = typename function_traits::template argument_type; template using function_argument_tuple = typename function_traits::argument_tuple; template using is_signed_int = std::integral_constant::value && std::is_signed::value) || std::is_same::value>; template using is_unsigned_int = std::integral_constant::value && std::is_unsigned::value) || std::is_same::value>; template using is_int = std::integral_constant::value || is_unsigned_int::value>; template using make_signed_int = typename std::conditional< std::is_same::value || std::is_same::value, std::common_type, std::make_signed>::type; template using make_unsigned_int = typename std::conditional< std::is_same::value || std::is_same::value, std::common_type, std::make_unsigned>::type; template struct is_range : std::false_type {}; template struct is_range< T, decltype(all(std::declval::type>()), (void)0)> : std::true_type {}; template::value> struct range_rank : std::integral_constant {}; template struct range_rank : std::integral_constant::value + 1> {}; template struct int_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::int_least8_t, typename std::conditional< size <= 16, std::int_least16_t, typename std::conditional< size <= 32, std::int_least32_t, typename std::conditional::type>::type>::type>::type; }; template using int_least_t = typename int_least::type; template struct uint_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::uint_least8_t, typename std::conditional< size <= 16, std::uint_least16_t, typename std::conditional< size <= 32, std::uint_least32_t, typename std::conditional::type>::type>::type>::type; }; template using uint_least_t = typename uint_least::type; template using double_size_int = int_least::digits * 2 + 1>; template using double_size_int_t = typename double_size_int::type; template using double_size_uint = uint_least::digits * 2>; template using double_size_uint_t = typename double_size_uint::type; template using double_size = typename std::conditional::value, double_size_int, double_size_uint>::type; template using double_size_t = typename double_size::type; #line 2 "library/template/in.hpp" #line 4 "library/template/in.hpp" #include #line 8 "library/template/in.hpp" template class Reader { private: int fd, idx, sz; bool state; std::array buffer; inline void read_buf() { sz = read(fd, buffer.begin(), buf_size); idx = 0; if (sz < 0) throw std::runtime_error("input failed"); } public: static constexpr int get_buf_size() { return buf_size; } Reader() noexcept : fd(0), idx(0), sz(0), state(true) {} Reader(int fd) noexcept : fd(fd), idx(0), sz(0), state(true) {} Reader(FILE* fp) noexcept : fd(fileno(fp)), idx(0), sz(0), state(true) {} class iterator { private: Reader* reader; public: using difference_type = void; using value_type = void; using pointer = void; using reference = void; using iterator_category = std::input_iterator_tag; iterator() : reader(nullptr) {} explicit iterator(Reader& reader) : reader(&reader) {} explicit iterator(Reader* reader) : reader(reader) {} iterator& operator++() { if (reader->idx == reader->sz) reader->read_buf(); ++reader->idx; return *this; } iterator operator++(int) { iterator res = *this; ++(*this); return res; } char operator*() const { if (reader->idx == reader->sz) reader->read_buf(); if (reader->idx < reader->sz) return reader->buffer[reader->idx]; reader->state = false; return '\0'; } bool rdstate() const { return reader->state; } void setstate(bool state) { reader->state = state; } }; iterator begin() noexcept { return iterator(this); } }; Reader<> reader(0); template class Scanner { public: using iterator_type = Iterator; private: template struct has_scan : std::false_type {}; template struct has_scan< T, decltype(std::declval().scan(std::declval()), (void)0)> : std::true_type {}; Iterator itr; public: Scanner() = default; Scanner(const Iterator& itr) : itr(itr) {} char scan_char() { char c = *itr; ++itr; return c; } Scanner ignore(int n = 1) { rep (n) ++itr; return *this; } inline void discard_space() { while (('\t' <= *itr && *itr <= '\r') || *itr == ' ') ++itr; } void scan(char& a) { discard_space(); a = *itr; ++itr; } void scan(bool& a) { discard_space(); a = *itr != '0'; ++itr; } void scan(std::string& a) { discard_space(); a.clear(); while ((*itr < '\t' || '\r' < *itr) && *itr != ' ' && *itr != '\0') { a += *itr; ++itr; } } template void scan(std::bitset& a) { discard_space(); rrep (i, len) { a[i] = *itr != '0'; ++itr; } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); if (*itr == '-') { ++itr; a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 - (*itr - '0'); ++itr; } } else { a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 + (*itr - '0'); ++itr; } } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 + *itr - '0'; ++itr; } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); bool sgn = false; if (*itr == '-') { sgn = true; ++itr; } a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 + *itr - '0'; ++itr; } if (*itr == '.') { ++itr; T n = 0, d = 1; for (int i = 0; '0' <= *itr && *itr <= '9' && i < (int)decimal_precision; ++i) { n = n * 10 + *itr - '0'; d *= 10; ++itr; } while ('0' <= *itr && *itr <= '9') ++itr; a += n / d; } if (sgn) a = -a; } private: template void scan(std::tuple& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { scan(std::get(a)); scan(a); } } public: template void scan(std::tuple& a) { scan<0, Args...>(a); } template void scan(std::pair& a) { scan(a.first); scan(a.second); } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { each_for (i : a) scan(i); } template::value>::type* = nullptr> void scan(T& a) { a.scan(*this); } void operator()() {} template void operator()(Head& head, Args&... args) { scan(head); operator()(args...); } template Scanner& operator>>(T& a) { scan(a); return *this; } explicit operator bool() const { return itr.rdstate(); } friend Scanner& getline(Scanner& scan, std::string& a) { a.erase(); char c; if ((c = scan.scan_char()) == '\n' || c == '\0') return scan; a += c; while ((c = scan.scan_char()) != '\n' && c != '\0') a += c; scan.itr.setstate(true); return scan; } }; Scanner::iterator> scan(reader.begin()); #line 2 "library/template/out.hpp" #line 8 "library/template/out.hpp" template class Writer { private: int fd, idx; std::array buffer; inline void write_buf() { int num = write(fd, buffer.begin(), idx); idx = 0; if (num < 0) throw std::runtime_error("output failed"); } public: Writer() noexcept : fd(1), idx(0) {} Writer(int fd) noexcept : fd(fd), idx(0) {} Writer(FILE* fp) noexcept : fd(fileno(fp)), idx(0) {} ~Writer() { write_buf(); } class iterator { private: Writer* writer; public: using difference_type = void; using value_type = void; using pointer = void; using reference = void; using iterator_category = std::output_iterator_tag; iterator() noexcept : writer(nullptr) {} explicit iterator(Writer& writer) noexcept : writer(&writer) {} explicit iterator(Writer* writer) noexcept : writer(writer) {} iterator& operator++() { ++writer->idx; if (writer->idx == buf_size) writer->write_buf(); return *this; } iterator operator++(int) { iterator res = *this; ++(*this); return res; } char& operator*() const { return writer->buffer[writer->idx]; } void flush() const { writer->write_buf(); } }; iterator begin() noexcept { return iterator(this); } }; Writer<> writer(1), ewriter(2); template class Printer { public: using iterator_type = Iterator; private: template struct has_print : std::false_type {}; template struct has_print().print(std::declval()), (void)0)> : std::true_type {}; template struct has_print().debug(std::declval()), (void)0)> : std::true_type {}; Iterator itr; std::size_t decimal_precision; public: void print_char(char c) { *itr = c; ++itr; } void flush() { itr.flush(); } Printer() noexcept = default; explicit Printer(const Iterator& itr) noexcept : itr(itr), decimal_precision(16) {} void set_decimal_precision(std::size_t decimal_precision) { this->decimal_precision = decimal_precision; } void print(char c) { if IF_CONSTEXPR (debug) print_char('\''); print_char(c); if IF_CONSTEXPR (debug) print_char('\''); } void print(bool b) { print_char((char)(b + '0')); } void print(const char* a) { if IF_CONSTEXPR (debug) print_char('"'); for (; *a != '\0'; ++a) print_char(*a); if IF_CONSTEXPR (debug) print_char('"'); } template void print(const char (&a)[len]) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } void print(const std::string& a) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } template void print(const std::bitset& a) { rrep (i, len) print_char((char)(a[i] + '0')); } template::value && !has_print::value>::type* = nullptr> void print(T a) { if (!a) { print_char('0'); return; } if IF_CONSTEXPR (is_signed_int::value) { if (a < 0) { print_char('-'); using U = typename make_unsigned_int::type; print(static_cast(-static_cast(a))); return; } } std::string s; while (a) { s += (char)(a % 10 + '0'); a /= 10; } for (auto i = s.rbegin(); i != s.rend(); ++i) print_char(*i); } template::value && !has_print::value>::type* = nullptr> void print(T a) { if (a == std::numeric_limits::infinity()) { print("inf"); return; } if (a == -std::numeric_limits::infinity()) { print("-inf"); return; } if (std::isnan(a)) { print("nan"); return; } if (a < 0) { print_char('-'); a = -a; } T b = a; if (b < 1) { print_char('0'); } else { std::string s; while (b >= 1) { s += (char)('0' + (int)std::fmod(b, 10.0)); b /= 10; } for (auto i = s.rbegin(); i != s.rend(); ++i) print_char(*i); } print_char('.'); rep (decimal_precision) { a *= 10; print_char((char)('0' + (int)std::fmod(a, 10.0))); } } private: template void print(const std::tuple& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(std::get(a)); print(a); } } public: template void print(const std::tuple& a) { if IF_CONSTEXPR (debug) print_char('('); if IF_CONSTEXPR (sizeof...(Args) != 0) print(std::get<0>(a)); print<1, Args...>(a); if IF_CONSTEXPR (debug) print_char(')'); } template void print(const std::pair& a) { if IF_CONSTEXPR (debug) print_char('('); print(a.first); if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(a.second); if IF_CONSTEXPR (debug) print_char(')'); } template::value && !has_print::value>::type* = nullptr> void print(const T& a) { if IF_CONSTEXPR (debug) print_char('{'); for (auto i = std::begin(a); i != std::end(a); ++i) { if (i != std::begin(a)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); } print(*i); } if IF_CONSTEXPR (debug) print_char('}'); } template::value && !debug>::type* = nullptr> void print(const T& a) { a.print(*this); } template::value && debug>::type* = nullptr> void print(const T& a) { a.debug(*this); } void operator()() {} template void operator()(const Head& head, const Args&... args) { print(head); operator()(args...); } template Printer& operator<<(const T& a) { print(a); return *this; } Printer& operator<<(Printer& (*pf)(Printer&)) { return pf(*this); } }; template Printer& endl(Printer& pr) { pr.print_char('\n'); pr.flush(); return pr; } template Printer& flush(Printer& pr) { pr.flush(); return pr; } struct SetPrec { int n; template void print(Pr& pr) const { pr.set_decimal_precision(n); } template void debug(Pr& pr) const { pr.set_decimal_precision(n); } }; SetPrec setprec(int n) { return SetPrec{n}; }; Printer::iterator> print(writer.begin()), eprint(ewriter.begin()); void prints() { print.print_char('\n'); } template auto prints(const T& v) -> decltype(print << v, (void)0) { print << v; print.print_char('\n'); } template auto prints(const Head& head, const Tail&... tail) -> decltype(print << head, (void)0) { print << head; print.print_char(' '); prints(tail...); } #ifdef SHIO_LOCAL Printer::iterator, true> debug(writer.begin()), edebug(ewriter.begin()); #else char debug_iterator_character; class DebugIterator { public: DebugIterator() noexcept = default; DebugIterator& operator++() { return *this; } DebugIterator& operator++(int) { return *this; } char& operator*() const { return debug_iterator_character; } void flush() const {} }; Printer debug, edebug; #endif void debugs() { debug.print_char('\n'); } template auto debugs(const T& v) -> decltype(debug << v, (void)0) { debug << v; debug.print_char('\n'); } template auto debugs(const Head& head, const Tail&... tail) -> decltype(debug << head, (void)0) { debug << head; debug.print_char(' '); debugs(tail...); } #line 2 "library/template/bitop.hpp" #line 6 "library/template/bitop.hpp" namespace bitop { #define KTH_BIT(b, k) (((b) >> (k)) & 1) #define POW2(k) (1ull << (k)) inline ull next_combination(int n, ull x) { if (n == 0) return 1; ull a = x & -x; ull b = x + a; return (x & ~b) / a >> 1 | b; } #define rep_comb(i, n, k) \ for (ull i = (1ull << (k)) - 1; i < (1ull << (n)); \ i = bitop::next_combination((n), i)) inline CONSTEXPR int msb(ull x) { int res = x ? 0 : -1; if (x & 0xFFFFFFFF00000000) x &= 0xFFFFFFFF00000000, res += 32; if (x & 0xFFFF0000FFFF0000) x &= 0xFFFF0000FFFF0000, res += 16; if (x & 0xFF00FF00FF00FF00) x &= 0xFF00FF00FF00FF00, res += 8; if (x & 0xF0F0F0F0F0F0F0F0) x &= 0xF0F0F0F0F0F0F0F0, res += 4; if (x & 0xCCCCCCCCCCCCCCCC) x &= 0xCCCCCCCCCCCCCCCC, res += 2; return res + ((x & 0xAAAAAAAAAAAAAAAA) ? 1 : 0); } inline CONSTEXPR int ceil_log2(ull x) { return x ? msb(x - 1) + 1 : 0; } inline CONSTEXPR ull reverse(ull x) { x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) | ((x & 0x5555555555555555) << 1); x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) | ((x & 0x3333333333333333) << 2); x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) | ((x & 0x0F0F0F0F0F0F0F0F) << 4); x = ((x & 0xFF00FF00FF00FF00) >> 8) | ((x & 0x00FF00FF00FF00FF) << 8); x = ((x & 0xFFFF0000FFFF0000) >> 16) | ((x & 0x0000FFFF0000FFFF) << 16); return (x >> 32) | (x << 32); } inline CONSTEXPR ull reverse(ull x, int n) { return reverse(x) >> (64 - n); } } // namespace bitop inline CONSTEXPR int popcnt(ull x) noexcept { #if __cplusplus >= 202002L return std::popcount(x); #endif x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555); x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f0f0f0f0f); x = (x & 0x00ff00ff00ff00ff) + ((x >> 8) & 0x00ff00ff00ff00ff); x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff); return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff); } #line 2 "library/template/func.hpp" #line 6 "library/template/func.hpp" template> inline constexpr bool chmin(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(b, a))) { return cmp(b, a) ? a = b, true : false; } template> inline constexpr bool chmax(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(a, b))) { return cmp(a, b) ? a = b, true : false; } inline CONSTEXPR ll gcd(ll a, ll b) { if (a < 0) a = -a; if (b < 0) b = -b; while (b) { const ll c = a; a = b; b = c % b; } return a; } inline CONSTEXPR ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } inline CONSTEXPR bool is_prime(ll N) { if (N <= 1) return false; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } inline std::vector prime_factor(ll N) { std::vector res; for (ll i = 2; i * i <= N; ++i) { while (N % i == 0) { res.push_back(i); N /= i; } } if (N != 1) res.push_back(N); return res; } inline CONSTEXPR ll my_pow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res *= a; b >>= 1; a *= a; } return res; } inline CONSTEXPR ll mod_pow(ll a, ll b, ll mod) { assert(mod > 0); if (mod == 1) return 0; a %= mod; ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; b >>= 1; (a *= a) %= mod; } return res; } inline PLL extGCD(ll a, ll b) { const ll n = a, m = b; ll x = 1, y = 0, u = 0, v = 1; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); std::swap(y -= t * v, v); } if (x < 0) { x += m; y -= n; } return {x, y}; } inline ll mod_inv(ll a, ll mod) { ll b = mod; ll x = 1, u = 0; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); } if (x < 0) x += mod; assert(a == 1); return x; } #line 2 "library/template/util.hpp" #line 6 "library/template/util.hpp" template class RecLambda { private: F f; public: explicit constexpr RecLambda(F&& f_) : f(std::forward(f_)) {} template constexpr auto operator()(Args&&... args) -> decltype(f(*this, std::forward(args)...)) { return f(*this, std::forward(args)...); } }; template inline constexpr RecLambda rec_lambda(F&& f) { return RecLambda(std::forward(f)); } template struct multi_dim_vector { using type = std::vector::type>; }; template struct multi_dim_vector { using type = T; }; template constexpr std::vector make_vec(int n, Arg&& arg) { return std::vector(n, std::forward(arg)); } template constexpr typename multi_dim_vector::type make_vec(int n, Args&&... args) { return typename multi_dim_vector::type( n, make_vec(std::forward(args)...)); } template> class compressor { private: std::vector dat; Comp cmp; bool sorted = false; public: compressor() : compressor(Comp()) {} compressor(const Comp& cmp) : cmp(cmp) {} compressor(const std::vector& vec, bool f = false, const Comp& cmp = Comp()) : dat(vec), cmp(cmp) { if (f) build(); } compressor(std::vector&& vec, bool f = false, const Comp& cmp = Comp()) : dat(std::move(vec)), cmp(cmp) { if (f) build(); } compressor(std::initializer_list il, bool f = false, const Comp& cmp = Comp()) : dat(all(il)), cmp(cmp) { if (f) build(); } void reserve(int n) { assert(!sorted); dat.reserve(n); } void push_back(const T& v) { assert(!sorted); dat.push_back(v); } void push_back(T&& v) { assert(!sorted); dat.push_back(std::move(v)); } template void emplace_back(Args&&... args) { assert(!sorted); dat.emplace_back(std::forward(args)...); } void push(const std::vector& vec) { assert(!sorted); const int n = dat.size(); dat.resize(n + vec.size()); rep (i, vec.size()) dat[n + i] = vec[i]; } int build() { assert(!sorted); sorted = true; std::sort(all(dat), cmp); dat.erase(std::unique(all(dat), [&](const T& a, const T& b) -> bool { return !cmp(a, b) && !cmp(b, a); }), dat.end()); return dat.size(); } const T& operator[](int k) const& { assert(sorted); assert(0 <= k && k < (int)dat.size()); return dat[k]; } int get(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); assert(itr != dat.end() && !cmp(val, *itr)); return itr - dat.begin(); } int lower_bound(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); return itr - dat.begin(); } int upper_bound(const T& val) const { assert(sorted); auto itr = std::upper_bound(all(dat), val, cmp); return itr - dat.begin(); } bool contains(const T& val) const { assert(sorted); return std::binary_search(all(dat), val, cmp); } std::vector pressed(const std::vector& vec) const { assert(sorted); std::vector res(vec.size()); rep (i, vec.size()) res[i] = get(vec[i]); return res; } void press(std::vector& vec) const { assert(sorted); each_for (i : vec) i = get(i); } int size() const { assert(sorted); return dat.size(); } }; #line 2 "main.cpp" #define GEOMETRY_REAL_TYPE ll #line 2 "library/geometry/All.hpp" #line 2 "library/geometry/template.hpp" #line 4 "library/geometry/template.hpp" #ifdef GEOMETRY_EPS constexpr ld geom_eps = GEOMETRY_EPS; #else constexpr ld geom_eps = EPS; #endif #ifdef GEOMETRY_REAL_TYPE using Real = GEOMETRY_REAL_TYPE; // a <=> b : cmp(a, b) <=> 0 inline int cmp(Real a, Real b) { if (a > b) return 1; if (a < b) return -1; return 0; } #else using Real = ld; // a <=> b : cmp(a, b) <=> 0 inline int cmp(ld a, ld b) { if (a > b + geom_eps) return 1; if (a < b - geom_eps) return -1; return 0; } #endif #ifdef GEOMETRY_ANGLE_TYPE using angle_t = GEOMETRY_ANGLE_TYPE; #else using angle_t = ld; #endif #line 2 "library/geometry/Point.hpp" #line 4 "library/geometry/Point.hpp" class Point { public: Real x, y; Point() : x(0), y(0) {} Point(Real x, Real y) : x(x), y(y) {} Point& operator+=(const Point& p) { x += p.x; y += p.y; return *this; } Point& operator-=(const Point& p) { x -= p.x; y -= p.y; return *this; } Point& operator*=(Real a) { x *= a; y *= a; return *this; } Point& operator/=(Real a) { x /= a; y /= a; return *this; } Point operator+() const { return *this; } Point operator-() const { return Point(-x, -y); } friend Point operator+(const Point& p1, const Point& p2) { return Point(p1) += p2; } friend Point operator-(const Point& p1, const Point& p2) { return Point(p1) -= p2; } friend Point operator*(const Point& p, Real a) { return Point(p) *= a; } friend Point operator*(Real a, const Point& p) { return Point(p) *= a; } friend Point operator/(const Point& p, Real a) { return Point(p) /= a; } friend bool operator==(const Point& p1, const Point& p2) { return cmp(p1.x, p2.x) == 0 && cmp(p1.y, p2.y) == 0; } friend bool operator!=(const Point& p1, const Point& p2) { return !(p1 == p2); } friend bool operator<(const Point& p1, const Point& p2) { return cmp(p1.x, p2.x) < 0 || (cmp(p1.x, p2.x) == 0 && cmp(p1.y, p2.y) < 0); } friend bool operator>(const Point& p1, const Point& p2) { return p2 < p1; } friend bool operator<=(const Point& p1, const Point& p2) { return !(p2 < p1); } friend bool operator>=(const Point& p1, const Point& p2) { return !(p1 < p2); } friend bool comp_arg(const Point& p1, const Point& p2) { // -pi < theta <= pi int a1 = p1.y < 0 ? 0 : p1.y > 0 ? 2 : p1.x >= 0 ? 1 : 3; int a2 = p2.y < 0 ? 0 : p2.y > 0 ? 2 : p2.x >= 0 ? 1 : 3; if (a1 != a2) return a1 < a2; return cross(p1, p2) > 0; } Real norm() const { return x * x + y * y; } friend Real norm(const Point& p) { return p.norm(); } Real abs() const { return sqrt(norm()); } friend Real abs(const Point& p) { return p.abs(); } inline angle_t arg() const { return atan2((ld)y, (ld)x); } friend angle_t arg(const Point& p) { return p.arg(); } Point& rotate(angle_t theta) { Real c = cos(theta), s = sin(theta); Real nx = x * c - y * s, ny = x * s + y * c; x = nx; y = ny; return *this; } friend Point rotate(const Point& p, angle_t theta) { return Point(p).rotate(theta); } Point& rotate90() { Real nx = -y, ny = x; x = nx; y = ny; return *this; } friend Point rotate90(const Point& p) { return Point(p).rotate90(); } // inner product(内積), p1 * p2 = |p1| * |p2| * cos(theta) friend Real dot(const Point& p1, const Point& p2) { return p1.x * p2.x + p1.y * p2.y; } // outer product(外積), p1 ^ p2 = |p1| * |p2| * sin(theta) friend Real cross(const Point& p1, const Point& p2) { return p1.x * p2.y - p1.y * p2.x; } template void scan(Sc& scan) { scan >> x >> y; } template void print(Pr& print) const { print << x << ' ' << y; } template void debug(Pr& print) const { print.print_char('('); print << x; print.print_char(','); print << y; print.print_char(')'); } }; Real distance(const Point& p1, const Point& p2) { return abs(p1 - p2); } enum class CCW { COUNTER_CLOCKWISE = 1, CLOCKWISE = -1, ONLINE_BACK = 2, ONLINE_FRONT = -2, ON_SEGMENT = 0, }; CCW ccw(const Point& p0, const Point& p1, const Point& p2) { Point a = p1 - p0, b = p2 - p0; if (cmp(cross(a, b), 0) > 0) return CCW::COUNTER_CLOCKWISE; if (cmp(cross(a, b), 0) < 0) return CCW::CLOCKWISE; if (cmp(dot(a, b), 0) < 0) return CCW::ONLINE_BACK; if (a.norm() < b.norm()) return CCW::ONLINE_FRONT; return CCW::ON_SEGMENT; } #line 2 "library/geometry/Line.hpp" #line 5 "library/geometry/Line.hpp" class Line { public: Real a, b, c; // ax + by + c = 0 Line() : a(0), b(1), c(0) {} Line(Real a, Real b, Real c) : a(a), b(b), c(c) {} Line(const Point& p1, const Point& p2) { a = p2.y - p1.y; b = p1.x - p2.x; c = p2.x * p1.y - p1.x * p2.y; } friend bool operator==(const Line& l1, const Line& l2) { return cmp(l1.a * l2.b, l2.a * l1.b) == 0 && cmp(l1.b * l2.c, l2.b * l1.c) == 0; } friend bool operator!=(const Line& l1, const Line& l2) { return !(l1 == l2); } friend bool operator<(const Line& l1, const Line& l2) { return cmp(l1.a * l2.b, l2.a * l1.b) < 0 || (cmp(l1.a * l2.b, l2.a * l1.b) == 0 && cmp(l1.b * l2.c, l2.b * l1.c) < 0); } friend bool operator>(const Line& l1, const Line& l2) { return l2 < l1; } friend bool operator<=(const Line& l1, const Line& l2) { return !(l2 < l1); } friend bool operator>=(const Line& l1, const Line& l2) { return !(l1 < l2); } bool is_on(const Point& p) const { return cmp(a * p.x + b * p.y + c, 0) == 0; } template void debug(Pr& print) const { print << a; print.print_char('x'); print.print_char('+'); print << b; print.print_char('y'); print.print_char('+'); print << c; print.print_char('='); print.print_char('0'); } }; Real distance(const Point& p, const Line& l) { return std::abs(l.a * p.x + l.b * p.y + l.c) / std::sqrt(l.a * l.a + l.b * l.b); } Real distance(const Line& l, const Point& p) { return distance(p, l); } // 垂直二等分線 Line perpendicular_bisector(const Point& p1, const Point& p2) { return Line((p1 + p2) / 2, (p1 + p2) / 2 + (p2 - p1).rotate90()); } // 平行判定 bool is_parallel(const Line& l1, const Line& l2) { return cmp(l1.a * l2.b, l2.a * l1.b) == 0; } // 直交判定 bool is_orthogonal(const Line& l1, const Line& l2) { return cmp(l1.a * l2.a + l1.b * l2.b, 0) == 0; } // 平行線 Line parallel(const Line& l, const Point& p) { return Line(l.a, l.b, -l.a * p.x - l.b * p.y); } // 垂直線 Line perpendicular(const Line& l, const Point& p) { return Line(l.b, -l.a, -l.b * p.x + l.a * p.y); } // 交叉判定 bool is_intersect(const Line& l1, const Line& l2) { return l1 == l2 || !is_parallel(l1, l2); } // 交点 Point intersection(const Line& l1, const Line& l2) { assert(!is_parallel(l1, l2)); Real d = l1.a * l2.b - l2.a * l1.b; return Point((l1.b * l2.c - l2.b * l1.c) / d, (l1.c * l2.a - l2.c * l1.a) / d); } // 射影 Point projection(const Line& l, const Point& p) { return intersection(l, perpendicular(l, p)); } // 反射 Point reflection(const Line& l, const Point& p) { return projection(l, p) * 2 - p; } #line 2 "library/geometry/Segment.hpp" #line 6 "library/geometry/Segment.hpp" class Segment { public: Point p1, p2; Segment() = default; Segment(const Point& p1, const Point& p2) : p1(p1), p2(p2) {} friend bool operator==(const Segment& s1, const Segment& s2) { return s1.p1 == s2.p1 && s1.p2 == s2.p2; } friend bool operator!=(const Segment& s1, const Segment& s2) { return !(s1 == s2); } friend bool operator<(const Segment& s1, const Segment& s2) { return s1.p1 < s2.p1 || (s1.p1 == s2.p1 && s1.p2 < s2.p2); } friend bool operator>(const Segment& s1, const Segment& s2) { return s2 < s1; } friend bool operator<=(const Segment& s1, const Segment& s2) { return !(s2 < s1); } friend bool operator>=(const Segment& s1, const Segment& s2) { return !(s1 < s2); } bool is_on(const Point& p) const { return p == p1 || p == p2 || ccw(p1, p2, p) == CCW::ON_SEGMENT; } explicit operator Line() const { return Line(p1, p2); } template void debug(Pr& print) const { print << p1; print.print_char('-'); print.print_char('>'); print << p2; } template void scan(Sc& scan) { scan >> p1 >> p2; } }; bool is_parallel(const Segment& s1, const Segment& s2) { return is_parallel(Line(s1), Line(s2)); } bool is_orthogonal(const Segment& s1, const Segment& s2) { return is_orthogonal(Line(s1), Line(s2)); } Line perpendicular_bisector(const Segment& s) { return perpendicular_bisector(s.p1, s.p2); } bool is_intersect(const Segment& s1, const Segment& s2) { if (is_parallel(s1, s2)) { return s1.is_on(s2.p1) || s1.is_on(s2.p2) || s2.is_on(s1.p1) || s2.is_on(s1.p2); } Point p = intersection(Line(s1), Line(s2)); return s1.is_on(p) && s2.is_on(p); } bool is_intersect(const Segment& s1, const Line& l) { if (!is_intersect(Line(s1), l)) return false; Point p = intersection(Line(s1), l); return s1.is_on(p); } bool is_intersect(const Line& l, const Segment& s1) { return is_intersect(s1, l); } Real distance(const Point& p, const Segment& s) { if (s.p1 == s.p2) return distance(p, s.p1); if (dot(s.p2 - s.p1, p - s.p1) < 0) return distance(p, s.p1); if (dot(s.p1 - s.p2, p - s.p2) < 0) return distance(p, s.p2); return distance(p, Line(s)); } Real distance(const Segment& s, const Point& p) { return distance(p, s); } Real distance(const Segment& s1, const Segment& s2) { if (is_intersect(s1, s2)) return 0; return std::min({distance(s1.p1, s2), distance(s1.p2, s2), distance(s2.p1, s1), distance(s2.p2, s1)}); } Real distance(const Segment& s, const Line& l) { if (is_intersect(s, l)) return 0; return std::min(distance(s.p1, l), distance(s.p2, l)); } Real distance(const Line& l, const Segment& s) { return distance(s, l); } #line 2 "library/geometry/Polygon.hpp" #line 6 "library/geometry/Polygon.hpp" class Polygon : public std::vector { public: using std::vector::vector; explicit Polygon(const std::vector& v) : std::vector(v) {} explicit Polygon(std::vector&& v) : std::vector(std::move(v)) {} }; Real area(const Polygon& p) { const int n = p.size(); Real res = 0; rep (i, n) { res += cross(p[i], p[(i + 1) % n]); } return res / 2; } bool is_convex(const Polygon& p, bool allow_straight = false) { const int n = p.size(); rep (i, n) { CCW c = ccw(p[(i + 1) % n], p[i], p[(i + 2) % n]); if (c == CCW::COUNTER_CLOCKWISE || (!allow_straight && c == CCW::ONLINE_BACK)) { return false; } } return true; } bool contains(const Polygon& p, const Point& q, bool true_when_on_edge = true) { const int n = p.size(); rep (i, n) { if (p[i] == q) return true_when_on_edge; Point a = p[i] - q; Point b = p[(i + 1) % n] - q; if (cmp(cross(a, b), 0) == 0 && cmp(dot(a, b), 0) <= 0) { return true_when_on_edge; } } bool res = false; rep (i, n) { Point a = p[i] - q; Point b = p[(i + 1) % n] - q; if (cmp(a.y, b.y) > 0) std::swap(a, b); if (cmp(a.y, 0) <= 0 && cmp(b.y, 0) > 0 && cmp(cross(a, b), 0) < 0) { res = !res; } } return res; } Polygon convex_hull(std::vector A, bool allow_straight = false) { const int n = A.size(); if (n <= 2) return Polygon{A}; std::sort(A.begin(), A.end(), [](const Point& a, const Point& b) { return cmp(a.x, b.x) != 0 ? cmp(a.x, b.x) < 0 : cmp(a.y, b.y) < 0; }); Polygon res; rep (i, n) { while ((int)res.size() >= 2) { CCW c = ccw(res[res.size() - 2], res.back(), A[i]); if (c == CCW::CLOCKWISE || (!allow_straight && c == CCW::ONLINE_FRONT)) { res.pop_back(); } else break; } res.push_back(A[i]); } int t = res.size(); rrep (i, n - 1) { while ((int)res.size() >= t + 1) { CCW c = ccw(res[res.size() - 2], res.back(), A[i]); if (c == CCW::CLOCKWISE || (!allow_straight && c == CCW::ONLINE_FRONT)) { res.pop_back(); } else break; } res.push_back(A[i]); } res.pop_back(); return res; } std::pair diameter(const Polygon& p) { const int n = p.size(); int i = 0, j = 0; rep (k, n) { if (cmp(p[k].x, p[i].x) > 0) i = k; if (cmp(p[k].x, p[j].x) < 0) j = k; } Real res = abs(p[i] - p[j]); int ri = i, rj = j; int si = i, sj = j; do { if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) < 0) { i = (i + 1) % n; } else { j = (j + 1) % n; } if (chmax(res, abs(p[i] - p[j]), [](const Real& a, const Real& b) { return cmp(a, b) < 0; })) { ri = i; rj = j; } } while (i != si || j != sj); return {p[ri], p[rj]}; } std::pair farthest_pair(const std::vector& p) { auto poly = convex_hull(p); return diameter(poly); } std::pair closest_pair(std::vector p) { assert(p.size() >= 2); const int n = p.size(); std::sort(all(p)); Real res = infinity::value; Point a, b; rec_lambda([&](auto&& self, int l, int r) -> void { const int m = (l + r) / 2; if (r - l <= 1) return; const Real x = p[m].x; self(l, m); self(m, r); std::inplace_merge( p.begin() + l, p.begin() + m, p.begin() + r, [](const Point& a, const Point& b) { return cmp(a.y, b.y) < 0; }); std::vector B; rep (i, l, r) { if (cmp(std::abs(p[i].x - x), res) >= 0) continue; rrep (j, B.size()) { if (cmp(p[i].y - p[B[j]].y, res) >= 0) break; if (chmin(res, distance(p[i], p[B[j]]), [](const Real& a, const Real& b) { return cmp(a, b) < 0; })) { a = p[i]; b = p[B[j]]; } } B.push_back(i); } })(0, n); return {a, b}; } // cut with line p0-p1 and return left side Polygon polygon_cut(const Polygon& p, const Point& p0, const Point& p1) { const int n = p.size(); Polygon res; rep (i, n) { Point a = p[i], b = p[(i + 1) % n]; Real ca = cross(p0 - a, p1 - a); Real cb = cross(p0 - b, p1 - b); if (cmp(ca, 0) >= 0) res.push_back(a); if (cmp(ca, 0) * cmp(cb, 0) < 0) { res.push_back(intersection(Line(a, b), Line(p0, p1))); } } return res; } #line 2 "library/geometry/Triangle.hpp" #line 6 "library/geometry/Triangle.hpp" class Triangle { public: Point p1, p2, p3; Triangle() = default; Triangle(const Point& p1, const Point& p2, const Point& p3) : p1(p1), p2(p2), p3(p3) {} Real area() const { return std::abs(cross(p2 - p1, p3 - p1)) / 2; } Point centroid() const { return (p1 + p2 + p3) / 3; } Point circumcenter() const { Line l1 = perpendicular_bisector(p1, p2); Line l2 = perpendicular_bisector(p2, p3); return intersection(l1, l2); } Real circumradius() const { return distance(p1, circumcenter()); } Point incenter() const { Real a = distance(p2, p3); Real b = distance(p3, p1); Real c = distance(p1, p2); return (a * p1 + b * p2 + c * p3) / (a + b + c); } Real inradius() const { return 2 * area() / (distance(p1, p2) + distance(p2, p3) + distance(p3, p1)); } Point orthocenter() const { return intersection(perpendicular(Line(p1, p2), p3), perpendicular(Line(p2, p3), p1)); } std::array excenter() const { Real a = distance(p2, p3); Real b = distance(p3, p1); Real c = distance(p1, p2); return {(-a * p1 + b * p2 + c * p3) / (-a + b + c), (a * p1 - b * p2 + c * p3) / (a - b + c), (a * p1 + b * p2 - c * p3) / (a + b - c)}; } std::array exradius() const { auto a = excenter(); Line l(p1, p2); return {distance(a[0], l), distance(a[1], l), distance(a[2], l)}; } Point nine_point_center() const { return (orthocenter() + circumcenter()) / 2; } Real nine_point_radius() const { return circumradius() / 2; } template void scan(Sc& scan) { scan >> p1 >> p2 >> p3; } template void debug(Pr& print) const { print.print_char('{'); print << p1; print.print_char(' '); print << p2; print.print_char(' '); print << p3; print.print_char('}'); } }; #line 2 "library/geometry/Circle.hpp" #line 6 "library/geometry/Circle.hpp" class Circle { public: Point c; Real r; Circle() : c(Point()), r(0) {} Circle(Point c, Real r) : c(c), r(r) {} friend bool operator==(const Circle& c1, const Circle& c2) { return c1.c == c2.c && cmp(c1.r, c2.r) == 0; } friend bool operator!=(const Circle& c1, const Circle& c2) { return !(c1 == c2); } friend bool operator<(const Circle& c1, const Circle& c2) { return c1.c < c2.c || (c1.c == c2.c && cmp(c1.r, c2.r) < 0); } friend bool operator>(const Circle& c1, const Circle& c2) { return c2 < c1; } friend bool operator<=(const Circle& c1, const Circle& c2) { return !(c2 < c1); } friend bool operator>=(const Circle& c1, const Circle& c2) { return !(c1 < c2); } template void scan(Sc& scan) { scan >> c >> r; } template void print(Pr& print) { print << c << ' ' << r; } template void debug(Pr& print) { print.print_char('{'); print << c; print.print_char(':'); print << r; print.print_char('}'); } }; enum class circle_relation { IN = 0, // 内包 INSCRIBE = 1, // 内接 INTERSECT = 2, // 交わる CIRCUMSCRIBE = 3, // 外接 SEPARATE = 4, // 離れている SAME = 5, // 等しい }; circle_relation relation(const Circle& c1, const Circle& c2) { if (c1 == c2) return circle_relation::SAME; const Real d = norm(c1.c - c2.c); const Real r1 = c1.r + c2.r, r2 = c1.r - c2.r; if (cmp(d, r1 * r1) > 0) return circle_relation::SEPARATE; if (cmp(d, r1 * r1) == 0) return circle_relation::CIRCUMSCRIBE; if (cmp(d, r2 * r2) > 0) return circle_relation::INTERSECT; if (cmp(d, r2 * r2) == 0) return circle_relation::INSCRIBE; return circle_relation::IN; } std::vector intersections(const Circle& c, const Line& l) { const Point h = projection(l, c.c); const Real d = norm(h - c.c); if (cmp(d, c.r * c.r) > 0) return {}; if (cmp(d, c.r * c.r) == 0) return {h}; const Point v = Point(l.b, -l.a) * std::sqrt(std::max((c.r * c.r - d) / (l.a * l.a + l.b * l.b), 0)); return {h - v, h + v}; } Line radical_axis(const Circle& c1, const Circle& c2) { const Real a = c1.c.x, b = c1.c.y, r = c1.r; const Real c = c2.c.x, d = c2.c.y, s = c2.r; const Real p = -2 * a + 2 * c, q = -2 * b + 2 * d; const Real r2 = a * a + b * b - c * c - d * d - r * r + s * s; return Line(p, q, r2); } std::vector intersections(const Circle& c1, const Circle& c2) { const Line l = radical_axis(c1, c2); return intersections(c1, l); } Line tangent_at_point(const Circle& c, const Point& p) { assert(cmp(norm(c.c - p), c.r * c.r) == 0); const Real a = c.c.x, b = c.c.y; const Real px = p.x, py = p.y; return Line(px - a, py - b, (a - px) * a + (b - py) * b - c.r * c.r); } std::vector tangent_points(const Circle& c, const Point& p) { const Real d = norm(c.c - p); const Real r2 = c.r * c.r; if (cmp(d, r2) < 0) return {}; if (cmp(d, r2) == 0) return {p}; const Circle c2(p, std::sqrt(std::max(d - r2, 0))); return intersections(c, c2); } std::vector common_tangents(const Circle& c1, const Circle& c2) { assert(c1 != c2); const Real d = norm(c1.c - c2.c); const Real r1 = c1.r, r2 = c2.r; std::vector res; if (cmp(d, (r1 - r2) * (r1 - r2)) == 0) { const Point v = (c2.c - c1.c) * (r1 / std::sqrt(d)); res.push_back(c1.c + (cmp(r1, r2) < 0 ? -v : v)); } else if (cmp(d, (r1 - r2) * (r1 - r2)) > 0) { if (cmp(r1, r2) == 0) { const Point v = (c2.c - c1.c).rotate90() * (r1 / std::sqrt(d)); res.push_back(c1.c + v); res.push_back(c1.c - v); } else { const Point v = (c1.c * r2 - c2.c * r1) / (-r1 + r2); auto ps = tangent_points(c1, v); std::copy(all(ps), std::back_inserter(res)); } if (cmp(d, (r1 + r2) * (r1 + r2)) == 0) { const Point v = (c2.c - c1.c) * (r1 / std::sqrt(d)); res.push_back(c1.c + v); } else if (cmp(d, (r1 + r2) * (r1 + r2)) > 0) { const Point v = (c1.c * r2 + c2.c * r1) / (r1 + r2); auto ps = tangent_points(c1, v); std::copy(all(ps), std::back_inserter(res)); } } return res; } #line 4 "main.cpp" using namespace std; int main() { int q; scan >> q; rep (q) { Point a, b, c, d; scan >> a >> b >> c >> d; auto cc = ccw({0, 0}, a - b, c - d); if ((cc == CCW::ON_SEGMENT || cc == CCW::ONLINE_FRONT) && ((a == c && b == d) || (a - b).norm() > (c - d).norm())) prints("Yes"); else prints("No"); } }