QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290987#6133. Mirrorucup-team087#WA 3344ms4092kbC++2023.3kb2023-12-26 00:55:512023-12-26 00:55:51

Judging History

你现在查看的是最新测评结果

  • [2023-12-26 00:55:51]
  • 评测
  • 测评结果:WA
  • 用时:3344ms
  • 内存:4092kb
  • [2023-12-26 00:55:51]
  • 提交

answer

#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

pi readpi(int off=0){
	int a,b;cin>>a>>b;
	return pi(a+off,b+off);
}

template<class t>
void print_single(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
	print_single(p.a,2);
	print_single(p.b,suc);
}

template<class T>
void print_single(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print_single(v[i],i==int(v.size())-1?suc:2);
}

template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
	rep(i,v.size())
		print_single(v[i]+off,i==int(v.size())-1?suc:2);
}

template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
	rep(i,N)
		print_single(v[i],i==int(N)-1?suc:2);
}

template<class T>
void print(const T&t){
	print_single(t);
}

template<class T,class ...Args>
void print(const T&t,const Args&...args){
	print_single(t,2);
	print(args...);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

void YES(bool ex=true){
	cout<<"YES\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void NO(bool ex=true){
	cout<<"NO\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void Yes(bool ex=true){
	cout<<"Yes\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void No(bool ex=true){
	cout<<"No\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}*/
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
int popcount(ull t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}
ull umask(int i){
	return (ull(1)<<i)-1;
}
ll minp2(ll n){
	if(n<=1)return 1;
	else return ll(1)<<(topbit(n-1)+1);
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	//#ifdef LOCAL
	static mt19937_64 gen;
	/*#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif*/
	return uniform_int_distribution<ll>(l, r)(gen);
}

ll rand_int(ll k){ //[0,k)
	return rand_int(0,k-1);
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}
template<class t>
bool bis(const vc<t>&v,const t&a){
	return binary_search(all(v),a);
}

vvc<int> readGraph(int n,int m){
	vvc<int> g(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		//sc.read(a,b);
		a--;b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	return g;
}

vvc<int> readTree(int n){
	return readGraph(n,n-1);
}

vc<ll> presum(const vi&a){
	vc<ll> s(si(a)+1);
	rep(i,si(a))s[i+1]=s[i]+a[i];
	return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
	gnr(i,1,si(a))a[i]-=a[i-1];
	return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
	int n=si(a),m=si(a[0]);
	vvc<ll> b(n+1,vc<ll>(m+1));
	rep(i,n)rep(j,m)
		b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
	return b;
}

//verify してないや
void transvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[j][i]=a[i][j];
	}
	a.swap(b);
	transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[m-1-j][i]=a[i][j];
	}
	a.swap(b);
	rotvvc(n,m,args...);
}

//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
	int n=si(a);
	vi idx(n);iota(all(idx),0);
	sort(all(idx),[&](int i,int j){return a[i]<a[j];});
	return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
	int n=si(a);
	assert(si(idx)==n);
	vc<t> vs(n);
	rep(i,n)vs[i]=a[idx[i]];
	return vs;
}
//CF850C
vi invperm(const vi&p){
	int n=si(p);
	vi q(n);
	rep(i,n)q[p[i]]=i;
	return q;
}

template<class t,class s=t>
s SUM(const vc<t>&a){
	return accumulate(all(a),s(0));
}

template<class t>
t MAX(const vc<t>&a){
	return *max_element(all(a));
}

template<class t>
t MIN(const vc<t>&a){
	return *min_element(all(a));
}

template<class t,class u>
pair<t,u> operator+(const pair<t,u>&a,const pair<t,u>&b){
	return mp(a.a+b.a,a.b+b.b);
}

vi vid(int n){
	vi res(n);iota(all(res),0);
	return res;
}

//デバッグ実行でオーバーフローするとコアダンプしがち

using int128=__int128_t;
using uint128=unsigned __int128_t;

istream& operator>>(istream&is,int128&res){
	res=0;
	string s;is>>s;
	int head=0;
	int128 w=1;
	if(s[0]=='-'){
		w=-1;
		head++;
	}
	while(head<int(s.size())){
		res=res*10+s[head++]-'0';
	}
	res*=w;
	return is;
}
ostream& operator<<(ostream&os,int128 i){
	if(i==0)
		return os<<0;
	static char buf[100];
	if(i<0){
		os<<"-";
		i=-i;
	}
	int p=0;
	while(i){
		buf[p++]='0'+i%10;
		i/=10;
	}
	reverse(buf,buf+p);
	buf[p]=0;
	return os<<buf;
}
ostream& operator<<(ostream&os,uint128 i){
	if(i==0)
		return os<<0;
	static char buf[100];
	int p=0;
	while(i){
		buf[p++]='0'+i%10;
		i/=10;
	}
	reverse(buf,buf+p);
	buf[p]=0;
	return os<<buf;
}
int128 abs128(int128 a){
	return a<0?-a:a;
}
int botbit(int128 a){
	const int128 m=(int128(1)<<64)-1;
	if(a&m)return __builtin_ctzll(ll(a));
	else return __builtin_ctzll(ll(a>>64))+64;
}
int128 gcd(int128 a,int128 b){
	if(a<0)a=-a;
	if(b<0)b=-b;
	if(a==0)return b;
	if(b==0)return a;
	int128 s=botbit(a|b);
	a>>=botbit(a);
	do{
		b>>=botbit(b);
		if(a>b)swap(a,b);
		b-=a;
	}while(b);
	return a<<s;
}
const int128 inf128=int128(1)<<122;

//ARC055D
//比較関数のあたりはverifyされてない?多分大丈夫だと思うけど
//多分verifyした
template<class t>
struct frac{
	t a,b;
	frac(t aa=0,t bb=1):a(aa),b(bb){g();}
	frac&g(){
		if(a==0){
			b=1;
			return *this;
		}
		t x=gcd(a,b);
		a/=x;b/=x;
		if(b<0){a=-a;b=-b;}
		return *this;
	}
	t quo()const{return a/b-((a^b)<0&&a%b);}
	t rem()const{return a-quo()*b;}
	frac inv()const{return frac(b,a);}
	frac operator-()const{return frac(-a,b);}
	frac&operator+=(frac r){a=a*r.b+r.a*b;b*=r.b;return g();}
	frac&operator-=(frac r){a=a*r.b-r.a*b;b*=r.b;return g();}
	frac&operator*=(frac r){a*=r.a;b*=r.b;return g();}
	frac&operator/=(frac r){a*=r.b;b*=r.a;return g();}
	frac operator+(frac r)const{return frac(*this)+=r;}
	frac operator-(frac r)const{return frac(*this)-=r;}
	frac operator*(frac r)const{return frac(*this)*=r;}
	frac operator/(frac r)const{return frac(*this)/=r;}
	explicit operator bool()const{
		return a;
	}
	bool operator<(frac r)const{
		t c=quo(),d=r.quo();
		if(c!=d)return c<d;
		else return rem()*r.b<r.rem()*b;
	}
	bool operator<=(frac r)const{
		t c=quo(),d=r.quo();
		if(c!=d)return c<=d;
		else return rem()*r.b<=r.rem()*b;
	}
	bool operator>(frac r)const{
		t c=quo(),d=r.quo();
		if(c!=d)return c>d;
		else return rem()*r.b>r.rem()*b;
	}
	bool operator>=(frac r)const{
		t c=quo(),d=r.quo();
		if(c!=d)return c>=d;
		else return rem()*r.b>=r.rem()*b;
	}
	bool operator==(frac r)const{
		return quo()==r.quo()&&rem()*r.b==r.rem()*b;
	}
	bool operator!=(frac r)const{
		return !operator==(r);
	}
	frac pow(int n)const{
		frac res(1);
		rep(_,n)res*=*this;
		return res;
	}
};
template<class t>
ostream&operator<<(ostream&os,frac<t> a){
	return os<<a.a<<"/"<<a.b;
}
template<class t>
int sgn(frac<t> a){return a.a<0?-1:(a.a>0?1:0);}
template<class t>
int cmpfrac(frac<t> a,frac<t> b){
	t p=a.a*b.b-b.a*a.b;
	return p<0?-1:(p==0?0:1);
}
template<class t>
frac<t> abs(frac<t> a){return a.a<0?-a:a;}

//copied from yosupo's library
//ptARTLY VERIFIED

//USACO 2022 January ptlatinum C

//#define GEOF

#ifdef GEOF
using ld=long double;
//using ld=double;
const ld PI=acos(ld(-1));
#else
using ld=frac<int128>;
#endif
const ld eps=0;
int sgn(ld a){return a<-eps?-1:(a>eps?1:0);}
int sgn(ld a,ld b){return sgn(a-b);}
/*
using pt=complex<ld>;
#define x real()
#define y imag()
*/
struct pt {
    ld x,y;
    //pt(ld _x = ld(0), ld _y = ld(0)) : x(_x), y(_y) {}
    pt():x(0),y(0){}
    pt(ld xx,ld yy):x(xx),y(yy){}
    pt operator+(const pt& r) const { return {x + r.x, y + r.y}; }
    pt operator-(const pt& r) const { return {x - r.x, y - r.y}; }
    pt operator*(const pt& r) const {
        return {x * r.x - y * r.y, x * r.y + y * r.x};
    }

    pt operator*(const ld& r) const { return {x * r, y * r}; }
    pt operator/(const ld& r) const { return {x / r, y / r}; }

    pt& operator+=(const pt& r) { return *this = *this + r; }
    pt& operator-=(const pt& r) { return *this = *this - r; }
    pt& operator*=(const pt& r) { return *this = *this * r; }
    pt& operator*=(const ld& r) { return *this = *this * r; }
    pt& operator/=(const ld& r) { return *this = *this / r; }

    pt operator-() const { return {-x, -y}; }

    bool operator<(const pt& r) const {
        return 2 * sgn(x, r.x) + sgn(y, r.y) < 0;
    }
    bool operator==(const pt& r) const { return sgn((*this - r).rabs()) == 0; }
    bool operator!=(const pt& r) const { return !(*this == r); }

    ld norm() const { return x * x + y * y; }
    ld rabs() const { return max(abs(x), abs(y)); }  // robust abs
    pair<ld, ld> to_pair() const { return {x, y}; }
    #ifdef GEOF
    ld abs() const { return sqrt(norm()); }
    ld arg() const { return atan2(y, x); }
    static pt polar(ld le, ld th) { return {le * cos(th), le * sin(th)}; }
	#endif
};
/*istream& operator>>(istream& is, pt& p){
	return is>>p.x>>p.y;
}*/
ostream& operator<<(ostream& os, const pt& p) {
    return os << "pt(" << p.x << ", " << p.y << ")";
}
ld norm(const pt&a){
	return a.norm();
}
#ifdef GEOF
ld abs(const pt&a){
	return sqrt(norm(a));
}
//XXII Opencup Gpt of Ural K
pt normalized(const pt&a){
	return a/abs(a);
}
ld arg(const pt&a){return a.arg();}
//normalize to [-PI,PI)
//Contest 2: ptKU Contest 1, ptTZ Summer 2022 Day 4
ld normarg(ld a){
	ld res=fmod(a+PI,2*PI);
	if(res<0)res+=PI;
	else res-=PI;
	return res;
}
//AOJ1183
//arg between ab
//assume given lengths are valid
ld arg(ld a,ld b,ld c){
	return acos(min(max((a*a+b*b-c*c)/(2*a*b),ld(-1)),ld(1)));
}
#endif
ld norm(const pt&a,const pt&b){
	return (a-b).norm();
}
ld dot(const pt&a,const pt&b){return a.x*b.x+a.y*b.y;}
ld crs(const pt& a,const pt& b){return a.x*b.y-a.y*b.x;}
ld crs(const pt& a,const pt& b,const pt& c){return crs(b-a,c-a);}
int ccw(const pt& a,const pt& b){return sgn(crs(a,b));}
int ccw(const pt& a,const pt& b,const pt& c){return ccw(b-a,c-a);}
//(-pi,0](0,pi]
int argtype(const pt&a){
	if(sgn(a.y)==0)return a.x<0?1:0;
	return a.y<0?0:1;
}
int argcmp(const pt&a,const pt&b){
	int at=argtype(a),bt=argtype(b);
	if(at!=bt)return at<bt?-1:1;
	return -ccw(a,b);
};
bool argless(const pt&a,const pt&b){return argcmp(a,b)<0;}
//(-2)[a,-1](0)[b,1](2)
int bet(pt a,pt b,pt c){
	pt d=b-a;
	ld e=dot(d,c-a);
	if(sgn(e)<=0)return sgn(e)-1;
	return sgn(e-norm(d))+1;
}
//AOJ0153
//三角形 abc に d が含まれるか?0-no,1-edge,2-in
int cont(pt a,pt b,pt c,pt d){
	if(ccw(a,b,c)==-1) swap(b,c);
	return min({ccw(a,b,d),ccw(b,c,d),ccw(c,a,d)})+1;
}
//(a,b) を結ぶ直線を考え,x 座標との交点の y 座標を求める
//(分子,分母)を返す
pair<ld,ld> xcut(const pt&a,const pt&b,ld x){
	return mp(a.y*(b.x-x)-b.y*(a.x-x),b.x-a.x);
}
//XXII Opencup Gpt of Ural K
pt rot90(pt a){
	return pt(-a.y,a.x);
}
#ifdef GEOF
ld xcutval(const pt&a,const pt&b,ld x){
	auto [p,q]=xcut(a,b,x);
	return p/q;
}
//AOJ1183
//Xmas2010 E
//-+ の 順で返す
//a の符号により,small/large が決まる
int qeq(ld a,ld b,ld c,ld&d,ld&e){
	if(sgn(a)==0){
		if(sgn(b)==0)return 0;
		d=-c/b;
		return 1;
	}
	ld f=b*b-4*a*c;
	if(sgn(f)<0)return 0;
	ld g=sqrt(max(f,ld(0)));
	d=(-b-g)/(2*a);
	e=(-b+g)/(2*a);
	return sgn(f)+1;
}
#else
#endif

using ln=pair<pt,pt>;
pt dir(ln a){return a.b-a.a;}
pt eval(ln a,ld b){return a.a+dir(a)*b;}
ld crs(ln a,pt b){return crs(a.a,a.b,b);}
int ccw(ln a,pt b){return ccw(a.a,a.b,b);}
int bet(ln a,pt b){return bet(a.a,a.b,b);}
ld projt(ln a,pt b){
	pt c=dir(a);
	return dot(c,b-a.a)/norm(c);
}
pt proj(ln a,pt b){
	pt c=dir(a);
	return a.a+c*dot(c,b-a.a)/norm(c);
}
pt refl(ln a,pt b){
	return proj(a,b)*2-b;
}
//AOJ1157
//0-no,1-yes(endpoint),2-yes(innner),3-overelap
//if the two line touch like this
// x----x----x
//it returns 1
int iss(ln a,ln b){
	int c1=ccw(a.a,a.b,b.a),c2=ccw(a.a,a.b,b.b);
	int d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);
	if(c1||c2||d1||d2)return 1-max(c1*c2,d1*d2);
	int f=bet(a.a,a.b,b.a),g=bet(a.a,a.b,b.b);
	if(max(f,g)==-2||min(f,g)==2)return 0;
	if(max(f,g)==-1||min(f,g)==1)return 1;
	return 3;
}
//segment a intersects line b?
//endpoints inclusive
bool isl(ln a,ln b){
	int d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);
	return d1*d2<=0;
}
bool ill(ln a,ln b){
	return bool(crs(dir(a),dir(b)));
}
ld cllt(ln a,ln b){
	return crs(b.a,b.b,a.a)/crs(dir(a),dir(b));
}
//ICPC Yokohama 2022 J
pair<ld,ld> clltf(ln a,ln b){
	return mp(crs(b.a,b.b,a.a),crs(dir(a),dir(b)));
}
//AOJ1033
pt cll(ln a,ln b){
	return eval(a,crs(b.a,b.b,a.a)/crs(dir(a),dir(b)));
}
#ifdef GEOF
//AOJ2201
ld dlp(ln a,pt b){
	return abs(crs(a,b)/abs(dir(a)));
}
//AOJ0153
ld dsp(ln a,pt b){
	pt c=proj(a,b);
	if(abs(bet(a.a,a.b,c))<=1)return abs(b-c);
	return min(abs(b-a.a),abs(b-a.b));
}
//AOJ1157
ld dss(ln a,ln b){
	if(iss(a,b))return 0;
	return min({dsp(a,b.a),dsp(a,b.b),dsp(b,a.a),dsp(b,a.b)});
}
//AOJ2160
//反時計回り方向に伸びる垂直二等分線
ln vbis(pt a,pt b){
	pt c=(a+b)*ld(0.5),d=b-a;
	return ln(c,pt(c.x-d.y,c.y+d.x));
}
ld cutareat(ln z,ld l,ld r){
	pt a=eval(z,l);
	pt b=eval(z,r);
	return -(b.x-a.x)*(a.y+b.y)/2;
}
#endif
//ABC286H
//simple polygon と線分が交わるか
//接している場合も true
/*bool icl(const vc<pt>&ps,ln z){
	int n=si(ps);
	rep(i,n){
		pt p=ps[i],q=ps[(i+1)%n];
		if(iss(z,ln(p,q)))return true;
	}
	return cont(ps,z.a);
}*/

pt readpos(){
	int a,b;cin>>a>>b;
	return pt(a,b);
}

using V=long double;
const V none=inf;
V dist(pt a,pt b){
	pt dif=a-b;
	frac<int128> z=sq(dif.x)+sq(dif.y);
	return sqrtl(z.a)/sqrtl(z.b);
}

//open な区間をまとめる
/*void unite(vc<pair<ld,ld>>&ls){
	vc<pair<ld,int>> qs;
	for(auto [l,r]:ls){
		qs.eb(l,1);
		qs.eb(r,-1);
	}
	sort(all(qs));
	vc<pair<ld,ld>> res;
	int cur=0;
	for(auto [x,add]:qs){
		if(cur==0){
			res.eb(x,0);
		}
		cur+=add;
		if(cur==0){
			res.back().b=x;
		}
		assert(cur>=0);
	}
	ls.swap(res);
}*/

void slv(){
	int numstone;cin>>numstone;
	pt start=readpos();
	pt goal=readpos();
	ln mir;
	mir.a=readpos();
	mir.b=readpos();
	vc<pt> tri(3);
	rep(i,3)tri[i]=readpos();
	if(ccw(tri[0],tri[1],tri[2])<0)swap(tri[1],tri[2]);
	
	vc<pt> ls{mir.a,mir.b};
	auto add=[&](pt v){
		ls.pb(v);
		ls.pb(refl(mir,v));
	};
	add(start);
	add(goal);
	rep(i,3)add(tri[i]);
	mkuni(ls);
	int sid=lwb(ls,start);
	int gid=lwb(ls,goal);
	{
		vc<pt> tmp=ls;
		rep(i,si(ls))if(i!=sid&&i!=gid)rep(j,si(ls))if(j!=sid&&j!=gid){
			if(ill(ln(start,ls[i]),ln(goal,ls[j]))){
				pt v=cll(ln(start,ls[i]),ln(goal,ls[j]));
				if(cont(tri[0],tri[1],tri[2],v)==0){
					bool bad=false;
					if(ccw(mir,v)==0)bad=true;
					rep(k,3)if(ccw(tri[k],tri[(k+1)%3],v)==0)bad=true;
					if(!bad)tmp.pb(v);
				}
			}
		}
		ls=tmp;
	}
	cerr<<si(ls)<<endl;
	mkuni(ls);
	sid=lwb(ls,start);
	gid=lwb(ls,goal);
	
	auto check2=[&](pt a,pt b,pt from)->bool{
		ln z(a,b);
		//見えない範囲を管理します
		int ini=0;
		vc<pair<ld,int>> tot;
		
		int off=0;
		vc<pair<ld,int>> qs;
		//w から見て左側を駄目にする
		auto cut=[&](ln w){
			int c=ccw(dir(z),dir(w));
			if(c==0){
				if(ccw(z,w.a)*dot(dir(z),dir(w)).a<=0){
					//all ok
				}else{
					//all bad
					off++;
				}
			}else if(c==1){
				ld t=cllt(z,w);
				//dmp2(z,w,t);
				qs.eb(t,-1);//[inf,t)は駄目
				off++;
			}else{
				ld t=cllt(z,w);
				qs.eb(t,1);//(t,inf] は駄目
			}
		};
		
		{
			off=0;
			qs.clear();
			auto edge=[&](pt c,pt d){
				if(ccw(from,c,d)<0)swap(c,d);
				if(ccw(from,c,d)==0)return;
				if(ill(ln(from,c),z)&&ill(ln(from,d),z)){
					if(cllt(ln(from,c),z)<=1&&cllt(ln(from,d),z)<=1){
						return;
					}
				}
				int cnt=0;
				if(ill(ln(from,c),z)&&cllt(ln(from,c),z)>=1){
					cnt++;
					cut(ln(from,c));
				}
				if(ill(ln(from,d),z)&&cllt(ln(from,d),z)>=1){
					cnt++;
					cut(ln(d,from));
				}
				if(cnt==2)off--;
			};
			edge(mir.a,mir.b);
			rep(i,3)edge(tri[i],tri[(i+1)%3]);
			int cur=off;
			//dmp(off);
			//dmp(qs);
			sort(all(qs));
			for(auto [x,val]:qs){
				if(cur==0&&cur+val>0)tot.eb(x,1);
				if(cur>0&&cur+val==0)tot.eb(x,-1);
				cur+=val;
			}
			if(off)ini++;
			//dmp(ini);
			//dmp(tot);
		}
		if(ccw(mir,from)<0){
			off=0;
			qs.clear();
			pt unko=refl(mir,from);
			//dmp(unko);
			auto edge=[&](pt c,pt d){
				if(ccw(unko,c,d)<0)swap(c,d);
				if(ccw(unko,c,d)==0)return;
				if(ill(ln(unko,c),z)&&ill(ln(unko,d),z)){
					if(cllt(ln(unko,c),z)<=1&&cllt(ln(unko,d),z)<=1){
						return;
					}
				}
				int cnt=0;
				if(ill(ln(unko,c),z)&&cllt(ln(unko,c),z)>=1){
					cnt++;
					cut(ln(unko,c));
				}
				if(ill(ln(unko,d),z)&&cllt(ln(unko,d),z)>=1){
					cnt++;
					cut(ln(d,unko));
				}
				if(cnt==2)off--;
			};
			cut(ln(mir.a,unko));
			//dmp2(off,qs);
			cut(ln(unko,mir.b));
			//dmp2(off,qs);
			cut(mir);
			//dmp2(off,qs);
			rep(i,3)edge(tri[i],tri[(i+1)%3]);
			//dmp2(off,qs);
			rep(i,3){
				pt u=tri[i],v=tri[(i+1)%3];
				if(ccw(mir,u)<0||ccw(mir,v)<0){
					edge(refl(mir,u),refl(mir,v));
				}
			}
			int cur=off;
			sort(all(qs));
			//dmp(off);
			//dmp(qs);
			for(auto [x,val]:qs){
				if(cur==0&&cur+val>0)tot.eb(x,1);
				if(cur>0&&cur+val==0)tot.eb(x,-1);
				cur+=val;
			}
			if(off)ini++;
		}else{
			ini++;
		}
		//[0,1] の値が 1 以下なら OK っす
		int cur=ini;
		sort(all(tot));
		ld pre=-1;
		for(auto [x,val]:tot){
			if(cur==2){
				if(pre<ld(1)&&ld(0)<x)return false;
			}
			cur+=val;
			if(cur==2){
				pre=x;
			}
		}
		if(cur==2){
			if(pre<ld(1))return false;
		}
		return true;
	};
	
	auto check=[&](pt a,pt b,bool needs,bool needg)->bool{
		{
			if(iss(ln(a,b),mir)==2)return false;
			rep(k,3)if(iss(ln(a,b),ln(tri[k],tri[(k+1)%3]))==2)return false;
		}
		if(needs&&!check2(a,b,start))return false;
		if(needg&&!check2(a,b,goal))return false;
		return true;
	};
	
	//dmp(check2(start,tri[0],start));
	//dmp(check2(start,tri[0],goal));
	//check2(start,tri[0],goal);
	//return;
	
	auto calc=[&](bool needs,bool needg){
		int n=si(ls);
		vvc<V> g(n,vc<V>(n,none));
		rep(i,n)g[i][i]=0;
		rep(i,n)rng(j,i+1,n){
			if(check(ls[i],ls[j],needs,needg)){
				g[i][j]=g[j][i]=dist(ls[i],ls[j]);
			}
		}
		rep(k,n)rep(i,n)rep(j,n)chmin(g[i][j],g[i][k]+g[k][j]);
		//return g[sid][gid];
		vc<V> dist(n,none);
		dist[sid]=0;
		vc<bool> done(n,false);
		rep(_,n){
			int f=-1;
			rep(i,n)if(!done[i]&&(f==-1||dist[f]>dist[i]))
				f=i;
			done[f]=true;
			rep(j,n)chmin(dist[j],dist[f]+g[f][j]);
		}
		return dist[gid];
	};
	if(numstone==1){
		V ans=calc(0,0);
		if(ans>=none)print(-1);
		else print(ans);
	}else{
		V a=calc(1,0);
		V b=calc(0,1);
		V c=calc(1,1);
		V ans=a+b+c*(2*numstone-3);
		if(ans>=none)print(-1);
		else print(ans);
	}
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	
	int t;cin>>t;rep(_,t)
	slv();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 588ms
memory: 4092kb

input:

2
2
-2 0 2 0
-3 3 3 3
0 1
-3 -2
3 -2
2
-2 0 2 0
-3 3 -1 3
0 1
-3 -2
3 -2

output:

13.41640786499873817805
-1

result:

ok 2 numbers

Test #2:

score: -100
Wrong Answer
time: 3344ms
memory: 4052kb

input:

25
1
-2 0 2 0
-3 3 3 3
0 1
-3 -2
3 -2
2
-2 0 2 0
-3 3 3 3
0 1
-3 -2
3 -2
100000
-2 0 2 0
-3 3 3 3
0 1
-3 -2
3 -2
2
-2 0 2 0
-3 3 -1 3
0 1
-3 -2
3 -2
2
2 0 -2 0
3 3 -3 3
0 1
-3 -2
3 -2
1
2 0 -2 0
3 3 -3 3
0 1
-3 -2
3 -2
2
2 2 -2 2
3 3 -3 3
0 1
-3 -2
3 -2
2
0 0 4 0
0 2 4 2
1 1
3 1
2 -1
1
0 0 4 0
0 2 4...

output:

4.47213595499957939283
13.41640786499873817805
894422.71886396087899129270
-1
-1
4.47213595499957939283
12.00000000000000000000
14.48528137423857029258
4.47213595499957939283
24.14213562373095048677
6.47213595499957939240
-1
8.26129717376116416185
24.78389152128349248554
41.30648586880582080749
7.49...

result:

wrong answer 14th numbers differ - expected: '24.9104078', found: '24.7838915', error = '0.0050789'