#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B"
#include"../../geometry/linear.hpp"#include"../../utilities/fast_io.hpp"intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;usingnamespacem1une::geometry;intq;fast_input>>q;while(q--){Segment<longlong>first;Segment<longlong>second;fast_input>>first.a.x>>first.a.y>>first.b.x>>first.b.y;fast_input>>second.a.x>>second.a.y>>second.b.x>>second.b.y;fast_output<<intersects(first,second)<<'\n';}}
#line 1 "verify/geometry/segment_intersection.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B"
#line 1 "geometry/linear.hpp"
#include<algorithm>
#include<cassert>
#include<cmath>#line 1 "geometry/point.hpp"
#line 5 "geometry/point.hpp"
#include<concepts>
#line 7 "geometry/point.hpp"
#include<type_traits>#line 1 "geometry/detail/floating_predicate.hpp"
namespacem1une{namespacegeometry{namespacepredicate_detail{template<typenameT>constexprTabsolute(Tvalue){returnvalue<T(0)?-value:value;}template<typenameT>constexprTmax_value(Tfirst,Tsecond){returnfirst<second?second:first;}template<typenameT>constexprTvector_scale(Tx,Ty){returnmax_value(absolute(x),absolute(y));}template<boolExact,typenameT>constexprintscaled_sign(Tvalue,Tscale,longdoubleeps){ifconstexpr(Exact){return(value>T(0))-(value<T(0));}else{constTtolerance=T(eps)*scale;return(value>tolerance)-(value<-tolerance);}}template<boolExact,typenameT>constexprTdeterminant_scale(Tax,Tay,Tbx,Tby){ifconstexpr(Exact){returnT(0);}else{returnvector_scale(ax,ay)*vector_scale(bx,by);}}template<boolExact,typenameT>constexprintdeterminant_sign(Tax,Tay,Tbx,Tby,longdoubleeps){constTdeterminant=ax*by-ay*bx;returnscaled_sign<Exact>(determinant,determinant_scale<Exact>(ax,ay,bx,by),eps);}template<boolExact,typenameT>constexprintorientation_sign(Tdirection_x,Tdirection_y,Toffset_x,Toffset_y,longdoubleeps){constTdeterminant=direction_x*offset_y-direction_y*offset_x;Tscale=T(0);ifconstexpr(!Exact){constTdirection_scale=vector_scale(direction_x,direction_y);scale=direction_scale*max_value(direction_scale,vector_scale(offset_x,offset_y));}returnscaled_sign<Exact>(determinant,scale,eps);}template<boolExact,typenameT>constexprintdot_sign(Tax,Tay,Tbx,Tby,longdoubleeps){constTvalue=ax*bx+ay*by;Tscale=T(0);ifconstexpr(!Exact){scale=vector_scale(ax,ay)*vector_scale(bx,by);}returnscaled_sign<Exact>(value,scale,eps);}}// namespace predicate_detail}// namespace geometry}// namespace m1une#line 10 "geometry/point.hpp"
namespacem1une{namespacegeometry{template<typenameT>conceptCoordinate=!std::same_as<std::remove_cv_t<T>,bool>&&(std::is_arithmetic_v<T>||(std::copyable<T>&&std::totally_ordered<T>&&requires(Ta,Tb){T(0);T(1);static_cast<longdouble>(a);{+a}->std::same_as<T>;{-a}->std::same_as<T>;{a+b}->std::same_as<T>;{a-b}->std::same_as<T>;{a*b}->std::same_as<T>;{a/b}->std::same_as<T>;{a+=b}->std::same_as<T&>;{a-=b}->std::same_as<T&>;}));// Custom coordinate types keep their own exact arithmetic.template<typenameT>conceptExactCoordinate=Coordinate<T>&&!std::floating_point<T>;template<CoordinateT>usingwide_type=std::conditional_t<std::integral<T>,__int128_t,std::conditional_t<std::floating_point<T>,longdouble,T>>;template<CoordinateT>structPoint{Tx;Ty;constexprPoint():x(0),y(0){}constexprPoint(Tx_value,Ty_value):x(x_value),y(y_value){}template<CoordinateU>explicitconstexprPoint(constPoint<U>&other):x(static_cast<T>(other.x)),y(static_cast<T>(other.y)){}constexprPoint&operator+=(constPoint&other){x+=other.x;y+=other.y;return*this;}constexprPoint&operator-=(constPoint&other){x-=other.x;y-=other.y;return*this;}constexprPointoperator+()const{return*this;}constexprPointoperator-()const{returnPoint(-x,-y);}friendconstexprPointoperator+(Pointleft,constPoint&right){returnleft+=right;}friendconstexprPointoperator-(Pointleft,constPoint&right){returnleft-=right;}friendconstexprbooloperator==(constPoint&,constPoint&)=default;friendconstexprbooloperator<(constPoint&left,constPoint&right){if(left.x!=right.x)returnleft.x<right.x;returnleft.y<right.y;}};template<CoordinateT>constexprPoint<longdouble>centroid(constPoint<T>&point){returnPoint<longdouble>(point);}template<CoordinateT,typenameScalar>requires(std::is_arithmetic_v<Scalar>||Coordinate<Scalar>)constexprautooperator*(constPoint<T>&point,Scalarscalar){usingResult=std::common_type_t<T,Scalar>;returnPoint<Result>(Result(point.x)*Result(scalar),Result(point.y)*Result(scalar));}template<typenameScalar,CoordinateT>requires(std::is_arithmetic_v<Scalar>||Coordinate<Scalar>)constexprautooperator*(Scalarscalar,constPoint<T>&point){returnpoint*scalar;}template<CoordinateT,typenameScalar>requires(std::is_arithmetic_v<Scalar>||Coordinate<Scalar>)constexprautooperator/(constPoint<T>&point,Scalarscalar){usingResult=std::common_type_t<T,Scalar>;returnPoint<Result>(Result(point.x)/Result(scalar),Result(point.y)/Result(scalar));}template<CoordinateT>constexprwide_type<T>dot(constPoint<T>&a,constPoint<T>&b){usingW=wide_type<T>;returnW(a.x)*W(b.x)+W(a.y)*W(b.y);}template<CoordinateT>constexprwide_type<T>cross(constPoint<T>&a,constPoint<T>&b){usingW=wide_type<T>;returnW(a.x)*W(b.y)-W(a.y)*W(b.x);}template<CoordinateT>constexprwide_type<T>cross(constPoint<T>&origin,constPoint<T>&a,constPoint<T>&b){usingW=wide_type<T>;Wax=W(a.x)-W(origin.x);Way=W(a.y)-W(origin.y);Wbx=W(b.x)-W(origin.x);Wby=W(b.y)-W(origin.y);returnax*by-ay*bx;}template<CoordinateT>constexprwide_type<T>norm2(constPoint<T>&point){returndot(point,point);}template<CoordinateT>constexprwide_type<T>distance2(constPoint<T>&a,constPoint<T>&b){usingW=wide_type<T>;Wdx=W(a.x)-W(b.x);Wdy=W(a.y)-W(b.y);returndx*dx+dy*dy;}template<CoordinateT>longdoublenorm(constPoint<T>&point){returnstd::hypot(static_cast<longdouble>(point.x),static_cast<longdouble>(point.y));}template<CoordinateT>longdoubledistance(constPoint<T>&a,constPoint<T>&b){returnstd::hypot(static_cast<longdouble>(a.x)-static_cast<longdouble>(b.x),static_cast<longdouble>(a.y)-static_cast<longdouble>(b.y));}template<CoordinateT,typenameM,typenameN>requires(std::is_arithmetic_v<M>||Coordinate<M>)&&(std::is_arithmetic_v<N>||Coordinate<N>)constexprPoint<longdouble>internal_division_point(constPoint<T>&a,constPoint<T>&b,Mm,Nn){longdoublefirst_ratio=static_cast<longdouble>(m);longdoublesecond_ratio=static_cast<longdouble>(n);longdoubledenominator=first_ratio+second_ratio;assert(denominator!=0);Point<longdouble>first(a);Point<longdouble>direction=Point<longdouble>(b)-first;returnfirst+direction*(first_ratio/denominator);}template<CoordinateT,typenameM,typenameN>requires(std::is_arithmetic_v<M>||Coordinate<M>)&&(std::is_arithmetic_v<N>||Coordinate<N>)constexprPoint<longdouble>external_division_point(constPoint<T>&a,constPoint<T>&b,Mm,Nn){longdoublefirst_ratio=static_cast<longdouble>(m);longdoublesecond_ratio=static_cast<longdouble>(n);longdoubledenominator=first_ratio-second_ratio;assert(denominator!=0);Point<longdouble>first(a);Point<longdouble>direction=Point<longdouble>(b)-first;returnfirst+direction*(first_ratio/denominator);}template<CoordinateT>constexprintsign(wide_type<T>value,longdoubleeps=1e-12L){returnpredicate_detail::scaled_sign<ExactCoordinate<T>>(value,wide_type<T>(1),eps);}template<CoordinateT>constexprintorientation(constPoint<T>&a,constPoint<T>&b,constPoint<T>&c,longdoubleeps=1e-12L){usingW=wide_type<T>;constWfirst_x=W(b.x)-W(a.x);constWfirst_y=W(b.y)-W(a.y);constWsecond_x=W(c.x)-W(a.x);constWsecond_y=W(c.y)-W(a.y);returnpredicate_detail::orientation_sign<ExactCoordinate<T>>(first_x,first_y,second_x,second_y,eps);}template<CoordinateT>constexprboolcollinear(constPoint<T>&a,constPoint<T>&b,constPoint<T>&c,longdoubleeps=1e-12L){returnorientation(a,b,c,eps)==0;}template<CoordinateT>Point<longdouble>rotate(constPoint<T>&point,longdoubleangle){longdoublecosine=std::cos(angle);longdoublesine=std::sin(angle);returnPoint<longdouble>(static_cast<longdouble>(point.x)*cosine-static_cast<longdouble>(point.y)*sine,static_cast<longdouble>(point.x)*sine+static_cast<longdouble>(point.y)*cosine);}template<CoordinateT>Point<longdouble>normalized(constPoint<T>&point){longdoublelength=norm(point);assert(length!=0);returnPoint<longdouble>(static_cast<longdouble>(point.x)/length,static_cast<longdouble>(point.y)/length);}}// namespace geometry}// namespace m1une#line 9 "geometry/linear.hpp"
namespacem1une{namespacegeometry{template<CoordinateT>structLine{Point<T>a;Point<T>b;};template<CoordinateT>structSegment{Point<T>a;Point<T>b;};template<CoordinateT>structRay{Point<T>origin;Point<T>through;};enumclassLinearIntersectionKind{Empty,Point,Segment,Ray,Line,};structLinearIntersection{LinearIntersectionKindkind;Point<longdouble>first;Point<longdouble>second;};structClosestPoints{Point<longdouble>first;Point<longdouble>second;};namespacelinear_intersection_detail{inlineLinearIntersectionmake_empty(){constPoint<longdouble>zero;returnLinearIntersection{LinearIntersectionKind::Empty,zero,zero,};}template<CoordinateT>LinearIntersectionmake_point(constPoint<T>&point){constPoint<longdouble>converted(point);returnLinearIntersection{LinearIntersectionKind::Point,converted,converted,};}template<CoordinateT>LinearIntersectionmake_object(LinearIntersectionKindkind,constPoint<T>&first,constPoint<T>&second){returnLinearIntersection{kind,Point<longdouble>(first),Point<longdouble>(second),};}}// namespace linear_intersection_detailtemplate<CoordinateT>constexprPoint<longdouble>centroid(constSegment<T>&segment){returnPoint<longdouble>((static_cast<longdouble>(segment.a.x)+static_cast<longdouble>(segment.b.x))/2,(static_cast<longdouble>(segment.a.y)+static_cast<longdouble>(segment.b.y))/2);}template<CoordinateT>boolon_line(constLine<T>&line,constPoint<T>&point,longdoubleeps=1e-12L){assert(line.a!=line.b);returnorientation(line.a,line.b,point,eps)==0;}template<CoordinateT>boolparallel(constLine<T>&first,constLine<T>&second,longdoubleeps=1e-12L){usingW=wide_type<T>;Wfirst_x=W(first.b.x)-W(first.a.x);Wfirst_y=W(first.b.y)-W(first.a.y);Wsecond_x=W(second.b.x)-W(second.a.x);Wsecond_y=W(second.b.y)-W(second.a.y);returnpredicate_detail::determinant_sign<ExactCoordinate<T>>(first_x,first_y,second_x,second_y,eps)==0;}template<CoordinateT>boolorthogonal(constLine<T>&first,constLine<T>&second,longdoubleeps=1e-12L){usingW=wide_type<T>;Wfirst_x=W(first.b.x)-W(first.a.x);Wfirst_y=W(first.b.y)-W(first.a.y);Wsecond_x=W(second.b.x)-W(second.a.x);Wsecond_y=W(second.b.y)-W(second.a.y);returnpredicate_detail::dot_sign<ExactCoordinate<T>>(first_x,first_y,second_x,second_y,eps)==0;}template<CoordinateT>Point<longdouble>projection(constLine<T>&line,constPoint<T>&point){assert(line.a!=line.b);Point<longdouble>a(line.a);Point<longdouble>direction(static_cast<longdouble>(line.b.x)-static_cast<longdouble>(line.a.x),static_cast<longdouble>(line.b.y)-static_cast<longdouble>(line.a.y));Point<longdouble>offset(static_cast<longdouble>(point.x)-a.x,static_cast<longdouble>(point.y)-a.y);longdoubleratio=dot(offset,direction)/dot(direction,direction);returna+direction*ratio;}template<CoordinateT>Point<longdouble>reflection(constLine<T>&line,constPoint<T>&point){Point<longdouble>projected=projection(line,point);returnprojected*2.0L-Point<longdouble>(point);}template<CoordinateT>boolintersects(constLine<T>&first,constLine<T>&second,longdoubleeps=1e-12L){return!parallel(first,second,eps)||on_line(first,second.a,eps);}template<CoordinateT>boolon_segment(constSegment<T>&segment,constPoint<T>&point,longdoubleeps=1e-12L){if(orientation(segment.a,segment.b,point,eps)!=0)returnfalse;usingW=wide_type<T>;constWdirection_x=W(segment.b.x)-W(segment.a.x);constWdirection_y=W(segment.b.y)-W(segment.a.y);if(direction_x==W(0)&&direction_y==W(0)){ifconstexpr(ExactCoordinate<T>){returnpoint==segment.a;}else{returnpredicate_detail::absolute(W(point.x)-W(segment.a.x))<=eps&&predicate_detail::absolute(W(point.y)-W(segment.a.y))<=eps;}}constWoffset_x=W(point.x)-W(segment.a.x);constWoffset_y=W(point.y)-W(segment.a.y);constWprojection=offset_x*direction_x+offset_y*direction_y;constWlength_squared=direction_x*direction_x+direction_y*direction_y;returnpredicate_detail::scaled_sign<ExactCoordinate<T>>(projection,length_squared,eps)>=0&&predicate_detail::scaled_sign<ExactCoordinate<T>>(projection-length_squared,length_squared,eps)<=0;}template<CoordinateT>Point<longdouble>projection(constSegment<T>&segment,constPoint<T>&point){constPoint<longdouble>first(segment.a);constPoint<longdouble>direction=Point<longdouble>(segment.b)-first;constlongdoublelength_squared=dot(direction,direction);if(length_squared==0)returnfirst;constlongdoubleratio=std::clamp(dot(Point<longdouble>(point)-first,direction)/length_squared,0.0L,1.0L);returnfirst+direction*ratio;}template<CoordinateT>boolintersects(constSegment<T>&first,constSegment<T>&second,longdoubleeps=1e-12L){intabc=orientation(first.a,first.b,second.a,eps);intabd=orientation(first.a,first.b,second.b,eps);intcda=orientation(second.a,second.b,first.a,eps);intcdb=orientation(second.a,second.b,first.b,eps);if(abc==0&&on_segment(first,second.a,eps))returntrue;if(abd==0&&on_segment(first,second.b,eps))returntrue;if(cda==0&&on_segment(second,first.a,eps))returntrue;if(cdb==0&&on_segment(second,first.b,eps))returntrue;returnabc*abd<0&&cda*cdb<0;}template<CoordinateT>boolintersects(constLine<T>&line,constSegment<T>&segment,longdoubleeps=1e-12L){intfirst_side=orientation(line.a,line.b,segment.a,eps);intsecond_side=orientation(line.a,line.b,segment.b,eps);returnfirst_side==0||second_side==0||first_side!=second_side;}template<CoordinateT>boolintersects(constSegment<T>&segment,constLine<T>&line,longdoubleeps=1e-12L){returnintersects(line,segment,eps);}namespacelinear_parameter_detail{template<CoordinateT>structParameters{wide_type<T>denominator;wide_type<T>denominator_scale;wide_type<T>first_numerator;wide_type<T>second_numerator;};template<CoordinateT>Parameters<T>parameters(constPoint<T>&first_origin,constPoint<T>&first_through,constPoint<T>&second_origin,constPoint<T>&second_through){usingW=wide_type<T>;Wfirst_x=W(first_through.x)-W(first_origin.x);Wfirst_y=W(first_through.y)-W(first_origin.y);Wsecond_x=W(second_through.x)-W(second_origin.x);Wsecond_y=W(second_through.y)-W(second_origin.y);Woffset_x=W(second_origin.x)-W(first_origin.x);Woffset_y=W(second_origin.y)-W(first_origin.y);returnParameters<T>{first_x*second_y-first_y*second_x,predicate_detail::determinant_scale<ExactCoordinate<T>>(first_x,first_y,second_x,second_y),offset_x*second_y-offset_y*second_x,offset_x*first_y-offset_y*first_x};}template<CoordinateT>intdenominator_sign(constParameters<T>&values,longdoubleeps){returnpredicate_detail::scaled_sign<ExactCoordinate<T>>(values.denominator,values.denominator_scale,eps);}template<CoordinateT>boolratio_nonnegative(wide_type<T>numerator,wide_type<T>denominator,longdoubleeps){constintnumerator_sign=predicate_detail::scaled_sign<ExactCoordinate<T>>(numerator,predicate_detail::absolute(denominator),eps);constintdenominator_direction=(denominator>0)-(denominator<0);returnnumerator_sign==0||numerator_sign==denominator_direction;}template<CoordinateT>boolratio_in_unit_interval(wide_type<T>numerator,wide_type<T>denominator,longdoubleeps){constautoscale=predicate_detail::absolute(denominator);constintstart_sign=predicate_detail::scaled_sign<ExactCoordinate<T>>(numerator,scale,eps);constintfinish_sign=predicate_detail::scaled_sign<ExactCoordinate<T>>(numerator-denominator,scale,eps);if(denominator>0){returnstart_sign>=0&&finish_sign<=0;}returnstart_sign<=0&&finish_sign>=0;}}// namespace linear_parameter_detailtemplate<CoordinateT>boolon_ray(constRay<T>&ray,constPoint<T>&point,longdoubleeps=1e-12L){assert(ray.origin!=ray.through);if(orientation(ray.origin,ray.through,point,eps)!=0)returnfalse;usingW=wide_type<T>;Wdirection_x=W(ray.through.x)-W(ray.origin.x);Wdirection_y=W(ray.through.y)-W(ray.origin.y);Woffset_x=W(point.x)-W(ray.origin.x);Woffset_y=W(point.y)-W(ray.origin.y);constWprojection=direction_x*offset_x+direction_y*offset_y;constWlength_squared=direction_x*direction_x+direction_y*direction_y;returnpredicate_detail::scaled_sign<ExactCoordinate<T>>(projection,length_squared,eps)>=0;}template<CoordinateT>Point<longdouble>projection(constRay<T>&ray,constPoint<T>&point){assert(ray.origin!=ray.through);Point<longdouble>origin(ray.origin);Point<longdouble>direction=Point<longdouble>(ray.through)-origin;Point<longdouble>offset=Point<longdouble>(point)-origin;longdoubleratio=dot(offset,direction)/dot(direction,direction);if(ratio<0)ratio=0;returnorigin+direction*ratio;}template<CoordinateT>Ray<longdouble>reflection(constLine<T>&line,constRay<T>&ray){assert(ray.origin!=ray.through);returnRay<longdouble>{reflection(line,ray.origin),reflection(line,ray.through)};}template<CoordinateT>Ray<longdouble>reflected_ray(constRay<T>&incoming,constPoint<T>&hit,constLine<T>&mirror,longdoubleeps=1e-12L){assert(incoming.origin!=incoming.through);assert(on_line(mirror,hit,eps));Point<T>translated=hit+(incoming.through-incoming.origin);returnRay<longdouble>{Point<longdouble>(hit),reflection(mirror,translated)};}template<CoordinateT>boolintersects(constRay<T>&ray,constLine<T>&line,longdoubleeps=1e-12L){assert(ray.origin!=ray.through);assert(line.a!=line.b);linear_parameter_detail::Parameters<T>values=linear_parameter_detail::parameters(ray.origin,ray.through,line.a,line.b);if(linear_parameter_detail::denominator_sign(values,eps)==0){returnon_line(line,ray.origin,eps);}returnlinear_parameter_detail::ratio_nonnegative<T>(values.first_numerator,values.denominator,eps);}template<CoordinateT>boolintersects(constLine<T>&line,constRay<T>&ray,longdoubleeps=1e-12L){returnintersects(ray,line,eps);}template<CoordinateT>boolintersects(constRay<T>&ray,constSegment<T>&segment,longdoubleeps=1e-12L){assert(ray.origin!=ray.through);if(segment.a==segment.b)returnon_ray(ray,segment.a,eps);linear_parameter_detail::Parameters<T>values=linear_parameter_detail::parameters(ray.origin,ray.through,segment.a,segment.b);if(linear_parameter_detail::denominator_sign(values,eps)==0){if(orientation(ray.origin,ray.through,segment.a,eps)!=0){returnfalse;}returnon_ray(ray,segment.a,eps)||on_ray(ray,segment.b,eps)||on_segment(segment,ray.origin,eps);}returnlinear_parameter_detail::ratio_nonnegative<T>(values.first_numerator,values.denominator,eps)&&linear_parameter_detail::ratio_in_unit_interval<T>(values.second_numerator,values.denominator,eps);}template<CoordinateT>boolintersects(constSegment<T>&segment,constRay<T>&ray,longdoubleeps=1e-12L){returnintersects(ray,segment,eps);}template<CoordinateT>boolintersects(constRay<T>&first,constRay<T>&second,longdoubleeps=1e-12L){assert(first.origin!=first.through);assert(second.origin!=second.through);linear_parameter_detail::Parameters<T>values=linear_parameter_detail::parameters(first.origin,first.through,second.origin,second.through);if(linear_parameter_detail::denominator_sign(values,eps)==0){if(orientation(first.origin,first.through,second.origin,eps)!=0){returnfalse;}returnon_ray(first,second.origin,eps)||on_ray(second,first.origin,eps);}returnlinear_parameter_detail::ratio_nonnegative<T>(values.first_numerator,values.denominator,eps)&&linear_parameter_detail::ratio_nonnegative<T>(values.second_numerator,values.denominator,eps);}namespacelinear_intersection_detail{enumclassDomain{Line,Segment,Ray,};template<CoordinateT>structParametricObject{Point<T>origin;Point<T>through;Domaindomain;};template<CoordinateT>ParametricObject<T>parametric_object(constLine<T>&line){assert(line.a!=line.b);returnParametricObject<T>{line.a,line.b,Domain::Line};}template<CoordinateT>ParametricObject<T>parametric_object(constSegment<T>&segment){returnParametricObject<T>{segment.a,segment.b,Domain::Segment};}template<CoordinateT>ParametricObject<T>parametric_object(constRay<T>&ray){assert(ray.origin!=ray.through);returnParametricObject<T>{ray.origin,ray.through,Domain::Ray};}template<CoordinateT>boolcontains(constParametricObject<T>&object,constPoint<T>&point,longdoubleeps){if(object.domain==Domain::Line){returnon_line(Line<T>{object.origin,object.through},point,eps);}if(object.domain==Domain::Segment){returnon_segment(Segment<T>{object.origin,object.through},point,eps);}returnon_ray(Ray<T>{object.origin,object.through},point,eps);}template<CoordinateT>boolaccepts_parameter(Domaindomain,wide_type<T>numerator,wide_type<T>denominator,longdoubleeps){if(domain==Domain::Line)returntrue;if(domain==Domain::Ray){returnlinear_parameter_detail::ratio_nonnegative<T>(numerator,denominator,eps);}returnlinear_parameter_detail::ratio_in_unit_interval<T>(numerator,denominator,eps);}template<CoordinateT>Point<longdouble>point_at_ratio(constParametricObject<T>&object,wide_type<T>numerator,wide_type<T>denominator){constlongdoubleratio=static_cast<longdouble>(numerator)/static_cast<longdouble>(denominator);constPoint<longdouble>origin(object.origin);constPoint<longdouble>direction=Point<longdouble>(object.through)-origin;returnorigin+direction*ratio;}template<CoordinateT>structAxisProjection{booluse_x;boolnegate;wide_type<T>operator()(constPoint<T>&point)const{constwide_type<T>value=use_x?wide_type<T>(point.x):wide_type<T>(point.y);returnnegate?-value:value;}};template<CoordinateT>AxisProjection<T>axis_projection(constParametricObject<T>&object){usingW=wide_type<T>;constWdirection_x=W(object.through.x)-W(object.origin.x);constWdirection_y=W(object.through.y)-W(object.origin.y);constbooluse_x=predicate_detail::absolute(direction_x)>=predicate_detail::absolute(direction_y);constWcomponent=use_x?direction_x:direction_y;assert(component!=W(0));returnAxisProjection<T>{use_x,component<W(0)};}template<CoordinateT>structParameterInterval{boolhas_lower;boolhas_upper;wide_type<T>lower;wide_type<T>upper;};template<CoordinateT>ParameterInterval<T>parameter_interval(constParametricObject<T>&object,constAxisProjection<T>&projection){usingW=wide_type<T>;constWorigin=projection(object.origin);constWthrough=projection(object.through);if(object.domain==Domain::Line){returnParameterInterval<T>{false,false,W(0),W(0)};}if(object.domain==Domain::Segment){returnParameterInterval<T>{true,true,std::min(origin,through),std::max(origin,through),};}if(origin<through){returnParameterInterval<T>{true,false,origin,W(0)};}returnParameterInterval<T>{false,true,W(0),origin};}template<CoordinateT>ParameterInterval<T>intersect_intervals(ParameterInterval<T>first,constParameterInterval<T>&second){if(second.has_lower&&(!first.has_lower||first.lower<second.lower)){first.has_lower=true;first.lower=second.lower;}if(second.has_upper&&(!first.has_upper||second.upper<first.upper)){first.has_upper=true;first.upper=second.upper;}returnfirst;}template<CoordinateT>Point<longdouble>point_at_projection(constParametricObject<T>&object,constAxisProjection<T>&projection,longdoubletarget){constlongdoubleorigin=static_cast<longdouble>(projection(object.origin));constlongdoublethrough=static_cast<longdouble>(projection(object.through));constlongdoubleratio=(target-origin)/(through-origin);constPoint<longdouble>point(object.origin);constPoint<longdouble>direction=Point<longdouble>(object.through)-point;returnpoint+direction*ratio;}template<CoordinateT>LinearIntersectioncollinear_intersection(constParametricObject<T>&first,constParametricObject<T>&second,longdoubleeps){usingW=wide_type<T>;constAxisProjection<T>projection=axis_projection(first);constParameterInterval<T>first_interval=parameter_interval(first,projection);constParameterInterval<T>second_interval=parameter_interval(second,projection);constParameterInterval<T>common=intersect_intervals(first_interval,second_interval);Wscale=predicate_detail::absolute(projection(first.through)-projection(first.origin));scale=std::max(scale,predicate_detail::absolute(projection(second.through)-projection(second.origin)));if(common.has_lower&&common.has_upper){constintorder=predicate_detail::scaled_sign<ExactCoordinate<T>>(common.lower-common.upper,scale,eps);if(order>0)returnmake_empty();if(order==0){constlongdoublecoordinate=(static_cast<longdouble>(common.lower)+static_cast<longdouble>(common.upper))/2.0L;returnmake_point(point_at_projection(first,projection,coordinate));}returnmake_object(LinearIntersectionKind::Segment,point_at_projection(first,projection,static_cast<longdouble>(common.lower)),point_at_projection(first,projection,static_cast<longdouble>(common.upper)));}constPoint<longdouble>direction=Point<longdouble>(first.through)-Point<longdouble>(first.origin);if(common.has_lower){constPoint<longdouble>origin=point_at_projection(first,projection,static_cast<longdouble>(common.lower));returnmake_object(LinearIntersectionKind::Ray,origin,origin+direction);}if(common.has_upper){constPoint<longdouble>origin=point_at_projection(first,projection,static_cast<longdouble>(common.upper));returnmake_object(LinearIntersectionKind::Ray,origin,origin-direction);}returnmake_object(LinearIntersectionKind::Line,first.origin,first.through);}template<CoordinateT>LinearIntersectionintersect(constParametricObject<T>&first,constParametricObject<T>&second,longdoubleeps){constboolfirst_degenerate=first.origin==first.through;constboolsecond_degenerate=second.origin==second.through;if(first_degenerate){assert(first.domain==Domain::Segment);if(contains(second,first.origin,eps)){returnmake_point(first.origin);}returnmake_empty();}if(second_degenerate){assert(second.domain==Domain::Segment);if(contains(first,second.origin,eps)){returnmake_point(second.origin);}returnmake_empty();}constlinear_parameter_detail::Parameters<T>values=linear_parameter_detail::parameters(first.origin,first.through,second.origin,second.through);if(linear_parameter_detail::denominator_sign(values,eps)!=0){if(!accepts_parameter<T>(first.domain,values.first_numerator,values.denominator,eps)||!accepts_parameter<T>(second.domain,values.second_numerator,values.denominator,eps)){returnmake_empty();}returnmake_point(point_at_ratio(first,values.first_numerator,values.denominator));}if(orientation(first.origin,first.through,second.origin,eps)!=0){returnmake_empty();}returncollinear_intersection(first,second,eps);}}// namespace linear_intersection_detailtemplate<CoordinateT>LinearIntersectionlinear_intersection(constLine<T>&first,constLine<T>&second,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(first),linear_intersection_detail::parametric_object(second),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constLine<T>&line,constSegment<T>&segment,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(line),linear_intersection_detail::parametric_object(segment),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constSegment<T>&segment,constLine<T>&line,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(segment),linear_intersection_detail::parametric_object(line),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constSegment<T>&first,constSegment<T>&second,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(first),linear_intersection_detail::parametric_object(second),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constRay<T>&ray,constLine<T>&line,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(ray),linear_intersection_detail::parametric_object(line),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constLine<T>&line,constRay<T>&ray,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(line),linear_intersection_detail::parametric_object(ray),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constRay<T>&ray,constSegment<T>&segment,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(ray),linear_intersection_detail::parametric_object(segment),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constSegment<T>&segment,constRay<T>&ray,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(segment),linear_intersection_detail::parametric_object(ray),eps);}template<CoordinateT>LinearIntersectionlinear_intersection(constRay<T>&first,constRay<T>&second,longdoubleeps=1e-12L){returnlinear_intersection_detail::intersect(linear_intersection_detail::parametric_object(first),linear_intersection_detail::parametric_object(second),eps);}namespaceclosest_points_detail{inlineClosestPointsreversed(constClosestPoints&result){returnClosestPoints{result.second,result.first};}inlineboolpoint_less(constPoint<longdouble>&first,constPoint<longdouble>&second){if(first.x!=second.x)returnfirst.x<second.x;returnfirst.y<second.y;}inlineClosestPointscommon_point(constLinearIntersection&intersection){assert(intersection.kind!=LinearIntersectionKind::Empty);Point<longdouble>point=intersection.first;if(intersection.kind==LinearIntersectionKind::Segment){if(point_less(intersection.second,point)){point=intersection.second;}}elseif(intersection.kind==LinearIntersectionKind::Line){constLine<longdouble>line{intersection.first,intersection.second};point=projection(line,Point<longdouble>(0,0));}returnClosestPoints{point,point};}inlinelongdoubleseparation2(constClosestPoints&result){returndistance2(result.first,result.second);}inlineboolcanonical_less(constClosestPoints&first,constClosestPoints&second){Point<longdouble>first_start=first.first;Point<longdouble>first_finish=first.second;if(point_less(first_finish,first_start)){std::swap(first_start,first_finish);}Point<longdouble>second_start=second.first;Point<longdouble>second_finish=second.second;if(point_less(second_finish,second_start)){std::swap(second_start,second_finish);}if(point_less(first_start,second_start))returntrue;if(point_less(second_start,first_start))returnfalse;returnpoint_less(first_finish,second_finish);}inlinevoidconsider(ClosestPoints&best,constClosestPoints&candidate){constlongdoublebest_distance=separation2(best);constlongdoublecandidate_distance=separation2(candidate);if(candidate_distance<best_distance||(candidate_distance==best_distance&&canonical_less(candidate,best))){best=candidate;}}}// namespace closest_points_detailtemplate<CoordinateT>ClosestPointsclosest_points(constPoint<T>&first,constPoint<T>&second){returnClosestPoints{Point<longdouble>(first),Point<longdouble>(second),};}template<CoordinateT>ClosestPointsclosest_points(constLine<T>&line,constPoint<T>&point){returnClosestPoints{projection(line,point),Point<longdouble>(point),};}template<CoordinateT>ClosestPointsclosest_points(constPoint<T>&point,constLine<T>&line){returnclosest_points_detail::reversed(closest_points(line,point));}template<CoordinateT>ClosestPointsclosest_points(constSegment<T>&segment,constPoint<T>&point){returnClosestPoints{projection(segment,point),Point<longdouble>(point),};}template<CoordinateT>ClosestPointsclosest_points(constPoint<T>&point,constSegment<T>&segment){returnclosest_points_detail::reversed(closest_points(segment,point));}template<CoordinateT>ClosestPointsclosest_points(constRay<T>&ray,constPoint<T>&point){returnClosestPoints{projection(ray,point),Point<longdouble>(point),};}template<CoordinateT>ClosestPointsclosest_points(constPoint<T>&point,constRay<T>&ray){returnclosest_points_detail::reversed(closest_points(ray,point));}template<CoordinateT>ClosestPointsclosest_points(constLine<T>&first,constLine<T>&second,longdoubleeps=1e-12L){constLinearIntersectionintersection=linear_intersection(first,second,eps);if(intersection.kind!=LinearIntersectionKind::Empty){returnclosest_points_detail::common_point(intersection);}ClosestPointsresult=closest_points(first,second.a);closest_points_detail::consider(result,closest_points(first.a,second));returnresult;}template<CoordinateT>ClosestPointsclosest_points(constLine<T>&line,constSegment<T>&segment,longdoubleeps=1e-12L){constLinearIntersectionintersection=linear_intersection(line,segment,eps);if(intersection.kind!=LinearIntersectionKind::Empty){returnclosest_points_detail::common_point(intersection);}ClosestPointsresult=closest_points(line,segment.a);closest_points_detail::consider(result,closest_points(line,segment.b));returnresult;}template<CoordinateT>ClosestPointsclosest_points(constSegment<T>&segment,constLine<T>&line,longdoubleeps=1e-12L){returnclosest_points_detail::reversed(closest_points(line,segment,eps));}template<CoordinateT>ClosestPointsclosest_points(constSegment<T>&first,constSegment<T>&second,longdoubleeps=1e-12L){constLinearIntersectionintersection=linear_intersection(first,second,eps);if(intersection.kind!=LinearIntersectionKind::Empty){returnclosest_points_detail::common_point(intersection);}ClosestPointsresult=closest_points(first,second.a);closest_points_detail::consider(result,closest_points(first,second.b));closest_points_detail::consider(result,closest_points(first.a,second));closest_points_detail::consider(result,closest_points(first.b,second));returnresult;}template<CoordinateT>ClosestPointsclosest_points(constLine<T>&line,constRay<T>&ray,longdoubleeps=1e-12L){constLinearIntersectionintersection=linear_intersection(line,ray,eps);if(intersection.kind!=LinearIntersectionKind::Empty){returnclosest_points_detail::common_point(intersection);}returnclosest_points(line,ray.origin);}template<CoordinateT>ClosestPointsclosest_points(constRay<T>&ray,constLine<T>&line,longdoubleeps=1e-12L){returnclosest_points_detail::reversed(closest_points(line,ray,eps));}template<CoordinateT>ClosestPointsclosest_points(constRay<T>&ray,constSegment<T>&segment,longdoubleeps=1e-12L){constLinearIntersectionintersection=linear_intersection(ray,segment,eps);if(intersection.kind!=LinearIntersectionKind::Empty){returnclosest_points_detail::common_point(intersection);}ClosestPointsresult=closest_points(ray,segment.a);closest_points_detail::consider(result,closest_points(ray,segment.b));closest_points_detail::consider(result,closest_points(ray.origin,segment));returnresult;}template<CoordinateT>ClosestPointsclosest_points(constSegment<T>&segment,constRay<T>&ray,longdoubleeps=1e-12L){returnclosest_points_detail::reversed(closest_points(ray,segment,eps));}template<CoordinateT>ClosestPointsclosest_points(constRay<T>&first,constRay<T>&second,longdoubleeps=1e-12L){constLinearIntersectionintersection=linear_intersection(first,second,eps);if(intersection.kind!=LinearIntersectionKind::Empty){returnclosest_points_detail::common_point(intersection);}ClosestPointsresult=closest_points(first,second.origin);closest_points_detail::consider(result,closest_points(first.origin,second));returnresult;}template<CoordinateT>longdoubledistance(constLine<T>&line,constPoint<T>&point){constClosestPointsresult=closest_points(line,point);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constPoint<T>&point,constLine<T>&line){returndistance(line,point);}template<CoordinateT>longdoubledistance(constSegment<T>&segment,constPoint<T>&point){constClosestPointsresult=closest_points(segment,point);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constPoint<T>&point,constSegment<T>&segment){returndistance(segment,point);}template<CoordinateT>longdoubledistance(constRay<T>&ray,constPoint<T>&point){constClosestPointsresult=closest_points(ray,point);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constPoint<T>&point,constRay<T>&ray){returndistance(ray,point);}template<CoordinateT>longdoubledistance(constLine<T>&first,constLine<T>&second){constClosestPointsresult=closest_points(first,second);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constLine<T>&line,constSegment<T>&segment){constClosestPointsresult=closest_points(line,segment);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constSegment<T>&segment,constLine<T>&line){returndistance(line,segment);}template<CoordinateT>longdoubledistance(constSegment<T>&first,constSegment<T>&second){constClosestPointsresult=closest_points(first,second);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constLine<T>&line,constRay<T>&ray){constClosestPointsresult=closest_points(line,ray);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constRay<T>&ray,constLine<T>&line){returndistance(line,ray);}template<CoordinateT>longdoubledistance(constRay<T>&ray,constSegment<T>&segment){constClosestPointsresult=closest_points(ray,segment);returngeometry::distance(result.first,result.second);}template<CoordinateT>longdoubledistance(constSegment<T>&segment,constRay<T>&ray){returndistance(ray,segment);}template<CoordinateT>longdoubledistance(constRay<T>&first,constRay<T>&second){constClosestPointsresult=closest_points(first,second);returngeometry::distance(result.first,result.second);}}// namespace geometry}// namespace m1une#line 4 "verify/geometry/segment_intersection.test.cpp"
#line 1 "utilities/fast_io.hpp"
#line 5 "utilities/fast_io.hpp"
#include<array>
#include<cerrno>
#include<charconv>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstdint>
#include<cstring>
#include<iterator>
#include<string>
#include<sys/stat.h>
#line 17 "utilities/fast_io.hpp"
#include<utility>
#include<unistd.h>
#include<vector>namespacem1une{namespaceutilities{structFastOutput;namespaceinternal{// Shared with the convenience helpers in template.hpp.inlineFastOutput*standard_output_instance=nullptr;// Detect std::begin(x), std::end(x).template<classT,class=void>structis_range:std::false_type{};template<classT>structis_range<T,std::void_t<decltype(std::begin(std::declval<T&>())),decltype(std::end(std::declval<T&>()))>>:std::true_type{};template<classT>inlineconstexprboolis_range_v=is_range<T>::value;template<classT>usingrange_reference_t=decltype(*std::begin(std::declval<T&>()));template<classT>usingrange_value_t=std::remove_cv_t<std::remove_reference_t<range_reference_t<T>>>;template<classT,class=void>structrange_stored_value{usingtype=range_value_t<T>;};template<classT>structrange_stored_value<T,std::void_t<typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type>>{usingtype=typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type;};template<classT>usingrange_stored_value_t=typenamerange_stored_value<T>::type;// Treat strings and C strings as scalar output objects, not as ranges.template<classT>structis_char_array:std::false_type{};template<classT,std::size_tN>structis_char_array<T[N]>:std::bool_constant<std::is_same_v<std::remove_cv_t<T>,char>>{};template<classT>structis_string_like:std::bool_constant<std::is_same_v<std::decay_t<T>,std::string>||std::is_same_v<std::decay_t<T>,constchar*>||std::is_same_v<std::decay_t<T>,char*>||is_char_array<std::remove_reference_t<T>>::value>{};template<classT>inlineconstexprboolis_string_like_v=is_string_like<T>::value;// ModInt-like type: x.val() is printable, and x can be assigned from long long.template<classT,class=void>structhas_val_method:std::false_type{};template<classT>structhas_val_method<T,std::void_t<decltype(std::declval<constT&>().val())>>:std::true_type{};template<classT>inlineconstexprboolhas_val_method_v=has_val_method<T>::value;template<classT,class=void>structhas_static_mod_raw:std::false_type{};template<classT>structhas_static_mod_raw<T,std::void_t<decltype(T::mod()),decltype(T::raw(std::declval<uint32_t>()))>>:std::true_type{};template<classT>inlineconstexprboolhas_static_mod_raw_v=has_static_mod_raw<T>::value;// libstdc++ before GCC 16 does not classify __int128 as an integral type in// strict ISO modes such as -std=c++23. Keep the fast-I/O interface independent// of that implementation detail.template<classT>inlineconstexprboolis_integral_v=std::is_integral_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>||std::is_same_v<std::remove_cv_t<T>,__uint128_t>;template<classT>inlineconstexprboolis_signed_v=std::is_signed_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>;template<classT>structmake_unsigned{usingtype=std::make_unsigned_t<T>;};template<>structmake_unsigned<__int128_t>{usingtype=__uint128_t;};template<>structmake_unsigned<__uint128_t>{usingtype=__uint128_t;};template<classT>usingmake_unsigned_t=typenamemake_unsigned<std::remove_cv_t<T>>::type;}// namespace internalstructFastInput{staticconstexprintbuffer_size=1<<20;private:std::FILE*_stream;char_buffer[buffer_size];int_position;int_length;int_file_descriptor;bool_streaming;boolrefill(){_position=0;if(_streaming){ssize_tlength;do{length=::read(_file_descriptor,_buffer,buffer_size);}while(length<0&&errno==EINTR);if(length<=0){_length=0;returnfalse;}_length=int(length);}else{_length=int(std::fread(_buffer,1,buffer_size,_stream));}return_length!=0;}template<classT>boolread_integer_from_stream(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'){negative=true;c=read_char_raw();}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){result=negative?result*10-(c-'0'):result*10+(c-'0');c=read_char_raw();}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){result=result*10+T(c-'0');c=read_char_raw();}value=negative?T(0)-result:result;}returntrue;}boolprepare_number(){if(_length-_position>=64)returntrue;constintremaining=_length-_position;if(remaining>0)std::memmove(_buffer,_buffer+_position,remaining);constintadded=int(std::fread(_buffer+remaining,1,buffer_size-remaining,_stream));_position=0;_length=remaining+added;if(_length<buffer_size)_buffer[_length]='\0';return_length!=0;}public:explicitFastInput(std::FILE*stream=stdin):_stream(stream),_position(0),_length(0),_file_descriptor(::fileno(stream)),_streaming([&]{structstatstatus;return_file_descriptor>=0&&::fstat(_file_descriptor,&status)==0&&!S_ISREG(status.st_mode);}()){}FastInput(constFastInput&)=delete;FastInput&operator=(constFastInput&)=delete;intread_char_raw(){if(_position==_length&&!refill())returnEOF;return_buffer[_position++];}boolskip_spaces(){intc=read_char_raw();while(c!=EOF&&c<=' ')c=read_char_raw();if(c==EOF)returnfalse;--_position;returntrue;}boolread(char&value){if(!skip_spaces())returnfalse;value=char(read_char_raw());returntrue;}boolread(std::string&value){if(!skip_spaces())returnfalse;value.clear();while(true){constintbegin=_position;while(_position<_length&&static_cast<unsignedchar>(_buffer[_position])>' '){++_position;}value.append(_buffer+begin,_position-begin);if(_position<_length){++_position;returntrue;}if(!refill())returntrue;}}boolread(bool&value){intx;if(!read(x))returnfalse;value=x!=0;returntrue;}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>,bool>read(T&value){if(_streaming)returnread_integer_from_stream(value);if(!prepare_number())returnfalse;intc=static_cast<unsignedchar>(_buffer[_position++]);while(c<=' ')c=static_cast<unsignedchar>(_buffer[_position++]);boolnegative=false;if(c=='-'){negative=true;c=static_cast<unsignedchar>(_buffer[_position++]);}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){constintfirst=c-'0';constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=negative?result*100-(first*10+second):result*100+(first*10+second);++_position;}else{result=negative?result*10-first:result*10+first;}c=static_cast<unsignedchar>(_buffer[_position++]);}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){constunsignedfirst=unsigned(c-'0');constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=result*100+T(first*10+unsigned(second));++_position;}else{result=result*10+T(first);}c=static_cast<unsignedchar>(_buffer[_position++]);}value=negative?T(0)-result:result;}if(_position>_length)_position=_length;returntrue;}template<classT>std::enable_if_t<std::is_floating_point_v<T>,bool>read(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'||c=='+'){negative=c=='-';c=read_char_raw();}longdoubleresult=0;while('0'<=c&&c<='9'){result=result*10+(c-'0');c=read_char_raw();}if(c=='.'){longdoubleplace=0.1L;c=read_char_raw();while('0'<=c&&c<='9'){result+=(c-'0')*place;place*=0.1L;c=read_char_raw();}}if(c=='e'||c=='E'){c=read_char_raw();boolexponent_negative=false;if(c=='-'||c=='+'){exponent_negative=c=='-';c=read_char_raw();}intexponent=0;while('0'<=c&&c<='9'){exponent=exponent*10+(c-'0');c=read_char_raw();}longdoublescale=1;longdoublepower=10;while(exponent>0){if(exponent&1)scale*=power;power*=power;exponent>>=1;}result=exponent_negative?result/scale:result*scale;}value=static_cast<T>(negative?-result:result);returntrue;}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>,bool>read(T&value){longlongx;if(!read(x))returnfalse;ifconstexpr(internal::has_static_mod_raw_v<T>){if(x>=0&&uint64_t(x)<uint64_t(T::mod())){value=T::raw(uint32_t(x));}else{value=T(x);}}else{value=T(x);}returntrue;}template<classFirst,classSecond>boolread(std::pair<First,Second>&value){if(!read(value.first))returnfalse;returnread(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>,bool>read(Range&range){usingStoredValue=internal::range_stored_value_t<Range>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;for(auto&&value:range){ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){boolx;if(!read(x))returnfalse;value=x;}else{if(!read(value))returnfalse;}}returntrue;}template<classFirst,classSecond,class...Rest>boolread(First&first,Second&second,Rest&...rest){if(!read(first))returnfalse;returnread(second,rest...);}template<classT>FastInput&operator>>(T&value){if(!read(value))std::abort();return*this;}};structFastOutput{staticconstexprintbuffer_size=1<<20;private:inlinestaticconstautodigit_quads=[]{std::array<char,40000>result{};for(inti=0;i<10000;i++){intvalue=i;for(intj=3;j>=0;j--){result[4*i+j]=char('0'+value%10);value/=10;}}returnresult;}();std::FILE*_stream;char_buffer[buffer_size];int_position;int_precision;std::chars_format_float_format;char_range_separator;std::string*_capture=nullptr;template<classT>std::stringformat_cell(constT&value){std::stringresult;structCaptureGuard{std::string*⌖std::string*previous;~CaptureGuard(){target=previous;}}guard{_capture,_capture};_capture=&result;write(value);returnresult;}template<classMatrix>voidwrite_aligned_matrix(constMatrix&matrix){std::vector<std::vector<std::string>>rows;std::vector<std::size_t>widths;for(constauto&row:matrix){auto&cells=rows.emplace_back();std::size_tcolumn=0;for(constauto&value:row){cells.push_back(format_cell(value));if(column==widths.size())widths.push_back(0);widths[column]=std::max(widths[column],cells.back().size());++column;}}boolfirst=true;for(constauto&row:rows){if(!first)write_char('\n');first=false;for(std::size_tcolumn=0;column<row.size();++column){if(column!=0)write_char(_range_separator);for(std::size_tpadding=row[column].size();padding<widths[column];++padding){write_char(' ');}write(row[column]);}}}public:explicitFastOutput(std::FILE*stream=stdout):_stream(stream),_position(0),_precision(6),_float_format(std::chars_format::general),_range_separator(' '){if(_stream==stdout&&internal::standard_output_instance==nullptr){internal::standard_output_instance=this;}}FastOutput(constFastOutput&)=delete;FastOutput&operator=(constFastOutput&)=delete;~FastOutput(){flush();if(internal::standard_output_instance==this){internal::standard_output_instance=nullptr;}}voidflush(){if(_position!=0){std::fwrite(_buffer,1,_position,_stream);_position=0;}std::fflush(_stream);}voidwrite_char(charc){if(_capture!=nullptr){_capture->push_back(c);return;}if(_position==buffer_size)flush();_buffer[_position++]=c;}voidwrite(constchar*s){while(*s!='\0')write_char(*s++);}voidwrite(conststd::string&s){if(_capture!=nullptr){_capture->append(s);return;}std::size_tposition=0;while(position<s.size()){if(_position==buffer_size)flush();conststd::size_tcopied=std::min<std::size_t>(buffer_size-_position,s.size()-position);std::memcpy(_buffer+_position,s.data()+position,copied);_position+=int(copied);position+=copied;}}voidwrite(charc){write_char(c);}voidwrite(boolvalue){write_char(value?'1':'0');}template<classT>std::enable_if_t<std::is_floating_point_v<T>>write(Tvalue){chardigits[128];auto[end,error]=std::to_chars(digits,digits+sizeof(digits),value,_float_format,_precision);if(error!=std::errc())std::abort();for(constchar*pointer=digits;pointer!=end;pointer++){write_char(*pointer);}}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>>write(Tvalue){usingRaw=std::remove_cv_t<T>;usingUnsigned=internal::make_unsigned_t<Raw>;Unsignedmagnitude;ifconstexpr(internal::is_signed_v<Raw>){if(value<0){write_char('-');magnitude=Unsigned(0)-Unsigned(value);}else{magnitude=Unsigned(value);}}else{magnitude=value;}if(magnitude==0){write_char('0');return;}unsignedchunks[16];intcount=0;while(magnitude>=10000){constUnsignedquotient=magnitude/10000;chunks[count++]=unsigned(magnitude-quotient*10000);magnitude=quotient;}if(_capture==nullptr&&_position>buffer_size-64)flush();charcaptured[64];char*constbegin=_capture!=nullptr?captured:_buffer+_position;char*destination=begin;constunsignedleading=unsigned(magnitude);constchar*first=digit_quads.data()+4*leading;intskip=leading<10?3:leading<100?2:leading<1000?1:0;for(;skip<4;skip++)*destination++=first[skip];while(count--){constchar*digits=digit_quads.data()+4*chunks[count];std::memcpy(destination,digits,4);destination+=4;}if(_capture!=nullptr){_capture->append(begin,destination-begin);}else{_position+=int(destination-begin);}}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>>write(constT&value){write(value.val());}template<classFirst,classSecond>voidwrite(conststd::pair<First,Second>&value){write(value.first);write_char(' ');write(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>>write(constRange&range){usingStoredValue=internal::range_stored_value_t<constRange>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;boolfirst=true;for(constauto&value:range){if(!first)write_char(nested?'\n':_range_separator);first=false;ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){write(static_cast<bool>(value));}else{write(value);}}}template<classFirst,class...Rest>voidprint(constFirst&first,constRest&...rest){write(first);((write_char(' '),write(rest)),...);}voidprintln(){write_char('\n');}voidset_precision(intprecision){_precision=precision;}voidset_fixed(intprecision=6){_float_format=std::chars_format::fixed;_precision=precision;}voidset_general(intprecision=6){_float_format=std::chars_format::general;_precision=precision;}voidset_range_separator(charseparator){_range_separator=separator;}template<classMatrix>voidwrite_aligned(constMatrix&matrix){usingRow=internal::range_stored_value_t<constMatrix>;usingCell=internal::range_stored_value_t<constRow>;static_assert(internal::is_range_v<Row>&&!internal::is_string_like_v<Row>,"write_aligned requires a two-dimensional range");static_assert(!internal::is_range_v<Cell>||internal::is_string_like_v<Cell>,"write_aligned requires scalar cells");write_aligned_matrix(matrix);}template<classMatrix>voidprintln_aligned(constMatrix&matrix){write_aligned(matrix);write_char('\n');}template<class...Args>voidprintln(constArgs&...args){print(args...);write_char('\n');}template<classT>FastOutput&operator<<(constT&value){write(value);return*this;}};}// namespace utilities}// namespace m1une#line 6 "verify/geometry/segment_intersection.test.cpp"
intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;usingnamespacem1une::geometry;intq;fast_input>>q;while(q--){Segment<longlong>first;Segment<longlong>second;fast_input>>first.a.x>>first.a.y>>first.b.x>>first.b.y;fast_input>>second.a.x>>second.a.y>>second.b.x>>second.b.y;fast_output<<intersects(first,second)<<'\n';}}