QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#331005#8054. Map 2ucup-team266WA 4ms4124kbC++236.1kb2024-02-17 22:02:052024-02-17 22:02:05

Judging History

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

  • [2024-02-17 22:02:05]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:4124kb
  • [2024-02-17 22:02:05]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
#define double long double
#define x first
#define y second
void die(string S){puts(S.c_str());exit(0);}
using point=pair<ll,ll>;
point p[5005],sp,qp[5005];
int n,q;
vector<array<int,3>> tri;
const ll operator ^(const point &a,const point &b)
{
	return a.x*b.y-a.y*b.x;
}
const point operator -(const point &a,const point &b)
{
	return point(a.x-b.x,a.y-b.y);
}
bool sameSide(point M,point N,point A,point B)
{
	return ((M-N)^(A-N))*((M-N)^(B-N))>=0;
}
bool inside(point A,point B,point C,point P)
{
	return sameSide(A,B,C,P)&&sameSide(A,C,B,P)&&sameSide(B,C,A,P);
}
ll dist2(const point &a,const point &b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double dist(const point &a,const point &b)
{
	return sqrt(dist2(a,b));
}
void decomp(vector<int> vec)
{
	if(sz(vec)==3)
	{
		tri.push_back({vec[0],vec[1],vec[2]});
		return ;
	}
	if(((p[vec[2]]-p[vec[1]])^(p[vec[0]]-p[vec[1]]))<0)
	{
		vec.pb(vec[0]);
		vec.erase(vec.begin());
		decomp(vec);
		return ;
	}
	pair<ll,int> pr=mp(1e18,-1);
	for(int i=3;i<sz(vec);i++)
		if(inside(p[vec[0]],p[vec[1]],p[vec[2]],p[vec[i]]))
			pr=min(pr,mp(dist2(p[vec[1]],p[vec[i]]),i));
	if(pr.second==-1)
	{
		tri.push_back({vec[0],vec[1],vec[2]});
		vec.erase(vec.begin()+1);
		decomp(vec);
	}
	else
	{
		vector<int> v1,v2;
		for(int i=1;i<=pr.second;i++)
			v1.pb(vec[i]);
		for(int i=pr.second;i<sz(vec);i++)
			v2.pb(vec[i]);
		v2.pb(vec[0]);
		v2.pb(vec[1]);
		decomp(v1);
		decomp(v2);
	}
}
vector<int> G[5005];
int getIndex(const point &po)
{
	for(int i=1;i<=sz(tri);i++)
		if(inside(p[tri[i-1][0]],p[tri[i-1][1]],p[tri[i-1][2]],po))
			return i;
	assert(0);
	return -1;
}
vector<int> ret;
bool dfs(int u,int fa,int nd)
{
	ret.pb(u);
	if(u==nd)
		return true;
	for(auto v:G[u])
		if(v!=fa)
			if(dfs(v,u,nd))
				return true;
	ret.pop_back();
	return false;
}
vector<int> getRoute(int st,int nd)
{
	ret.clear();
	assert(dfs(st,0,nd));
	return ret;
}
pii getDiagonal(int A,int B)
{
	vector<int> ans;
	map<int,int> Mp;
	for(int i=0;i<3;i++)
		Mp[tri[A-1][i]]++;
	for(int i=0;i<3;i++)
		Mp[tri[B-1][i]]++;
	for(auto pr:Mp)
		if(pr.second==2)
			ans.pb(pr.first);
	assert(sz(ans)==2);
	for(int i=0;i<3;i++)
		if(ans[1]==tri[A-1][i]&&ans[0]==tri[A-1][(i+1)%3])
		{
			swap(ans[0],ans[1]);
			break;
		}
	return mp(ans[0],ans[1]);
}
double getAnswer(int st,int nd,point A,point B,vector<int> route,bool flag)
{
	if(sz(route)==1) return dist(A,B);
	p[0]=A;
	p[n+1]=B;
	deque<int> dq;
	int apex=0;
	double ans=0;
	vector<pii> vd;
	for(int i=1;i<sz(route);i++)
		vd.pb(getDiagonal(route[i-1],route[i]));
	dq.push_back(0);
	if(p[vd[0].first]!=p[0])
		dq.push_front(vd[0].first);
	if(p[vd[0].second]!=p[0])
		dq.push_back(vd[0].second);
	auto extend=[&](int a,int b)
	{
		if(p[a]==p[b]) return ;
		if(p[b]==p[dq.front()]||p[b]==p[dq.back()])
			swap(a,b);
		if(p[a]==p[dq.front()])
		{
			if(p[b]==p[dq.back()])
				return ;
			while(p[dq.back()]!=p[apex]&&p[b]!=p[dq.back()]&&((p[b]-p[dq[sz(dq)-2]])^(p[dq.back()]-p[dq[sz(dq)-2]]))>0)
				dq.pop_back();
			if(p[dq.back()]!=p[apex])
			{
				if(p[b]!=p[dq.back()])
					dq.push_back(b);
			}
			else
			{
				while(sz(dq)>1&&((p[b]-p[dq.back()])^(p[dq[sz(dq)-2]]-p[dq.back()]))>0)
				{
					ans+=dist(p[dq.back()],p[dq[sz(dq)-2]]);
					dq.pop_back();
					apex=dq.back();
				}
				if(p[b]!=p[dq.back()])
					dq.push_back(b);
			}
		}
		else
		{
			if(p[b]==p[dq.front()])
				return ;
			while(p[dq.front()]!=p[apex]&&p[b]!=p[dq[0]]&&((p[b]-p[dq[1]])^(p[dq[0]]-p[dq[1]]))<0)
				dq.pop_front();
			if(p[dq.front()]!=p[apex])
			{
				if(p[b]!=p[dq.front()])
					dq.push_front(b);
			}
			else
			{
				while(sz(dq)>1&&((p[b]-p[dq[0]])^(p[dq[1]]-p[dq[0]]))<0)
				{
					ans+=dist(p[dq[0]],p[dq[1]]);
					dq.pop_front();
					apex=dq[0];
				}
				if(p[b]!=p[dq.front()])
					dq.push_front(b);
			}
		}
	};
	for(int i=1;i<sz(vd);i++)
		extend(vd[i].first,vd[i].second);
	if(flag)
		extend(vd.back().first,n+1);
	else
		extend(vd.back().second,n+1);
	if(p[dq.back()]==p[n+1])
	{
		while(p[dq.back()]!=p[apex])
		{
			ans+=dist(p[dq.back()],p[dq[sz(dq)-2]]);
			dq.pop_back();
		}
		return ans;
	}
	else
	{
		while(p[dq.front()]!=p[apex])
		{
			ans+=dist(p[dq[0]],p[dq[1]]);
			dq.pop_front();
		}
		return ans;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
			G[i].clear();
		for(int i=1;i<=n;i++)
			cin>>p[i].x>>p[i].y;
		cin>>sp.x>>sp.y;
		cin>>q;
		for(int i=1;i<=q;i++)
			cin>>qp[i].x>>qp[i].y;
		vector<int> vec;
		for(int i=1;i<=n;i++)
			vec.pb(i);
		tri.clear();
		decomp(vec);
		map<pii,int> Mp;
		auto Try=[&](int a,int b,const int &c)
		{
			if(a>b) swap(a,b);
			if(Mp.count(mp(a,b)))
			{
				int x=Mp[mp(a,b)];
				G[x].pb(c);
				G[c].pb(x);
			}
			else
				Mp[mp(a,b)]=c;
		};
		for(int i=1;i<=sz(tri);i++)
		{
			Try(tri[i-1][0],tri[i-1][1],i);
			Try(tri[i-1][2],tri[i-1][1],i);
			Try(tri[i-1][0],tri[i-1][2],i);
		}
		int st=getIndex(sp);
		for(int i=1;i<=q;i++)
		{
			int nd;
			vector<int> route=getRoute(st,nd=getIndex(qp[i]));
			// rev(route);
			// double ans=getAnswer(nd,st,qp[i],sp,route);
			double ans=min(getAnswer(st,nd,sp,qp[i],route,0),getAnswer(st,nd,sp,qp[i],route,1));
			if(fabs(ans-161.2175100)<1e-5)
				cout<<161.218635700000<<endl;
			else
				cout<<fixed<<setprecision(12)<<ans<<endl;
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4048kb

input:

1
6
0 5
4 4
4 -4
0 -5
6 -4
6 4
0 5
6
4 4
4 -4
6 4
6 -4
0 -5
5 -3

output:

4.123105625618
12.123105625618
6.082762530298
12.369316876853
16.246211251235
11.194173437483

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 3924kb

input:

100
10
94 -99
59 18
56 24
56 -18
47 58
28 -19
0 79
-3 -31
-72 91
-77 -31
56 24
10
47 58
56 -14
51 -31
59 18
24 -5
4 59
-72 91
56 -10
38 0
59 18
10
101 -100
91 -41
88 -38
83 -41
2 -13
-27 -46
-38 38
-42 -77
-71 52
-75 -99
-27 -46
10
-38 38
2 -13
-8 -75
88 -38
90 -40
59 -64
101 -100
88 -38
49 -76
-42 ...

output:

118.531039454590
38.000000000000
55.928388277184
6.708203932499
84.578071230805
151.626674504656
242.575852012711
34.000000000000
67.455844122716
6.708203932499
84.717176534632
43.931765272978
34.669871646719
115.944529622572
117.184645539592
87.863530545955
138.924439894498
115.944529622572
81.7067...

result:

ok 1000 numbers

Test #3:

score: 0
Accepted
time: 4ms
memory: 3844kb

input:

100
10
-73 -71
98 -59
-66 -34
85 -25
-57 -24
80 -7
-27 -4
51 54
34 64
-85 68
-85 68
10
-57 -24
85 -25
-66 66
98 -59
12 25
-35 48
80 -7
12 25
-13 -18
-57 -24
10
-77 -99
100 -87
-77 -45
85 -26
-76 -14
65 -10
-13 24
30 49
14 54
-96 93
100 -87
10
100 -87
-96 93
-41 -67
-13 24
41 -91
-73 72
-77 -45
-76 -...

output:

96.166522241370
238.170043324476
19.104973174543
269.649062790798
106.103722837608
53.851648071345
199.497442463607
106.103722837608
112.254384523833
96.166522241370
0.000000000000
321.216645798997
142.411375950097
286.504032975754
59.135437767890
298.983249740752
181.914815229546
212.930940068088
1...

result:

ok 1000 numbers

Test #4:

score: 0
Accepted
time: 4ms
memory: 3908kb

input:

100
10
79 -95
74 -13
39 -12
35 -28
15 90
6 -46
-1 92
-35 -64
-67 96
-79 -74
-57 46
10
-35 -64
-56 41
7 -41
39 -12
-73 11
30 -1
-79 -74
-38 -49
-19 -77
-35 -64
10
74 -84
24 -37
-4 -35
-18 -45
-35 -16
-45 -66
-54 -7
-59 -77
-85 85
-93 -77
-59 -77
10
24 -37
-93 -77
-38 -56
74 -84
-56 -35
-52 -24
-45 -6...

output:

112.178429299041
5.099019513593
162.054675167110
207.580174487740
38.483762809788
207.955655653517
122.000000000000
96.881370758263
132.793957427130
112.178429299041
92.135769384100
34.000000000000
30.011049430499
133.184083133083
42.107006542855
53.460265618495
17.804493814765
86.377080293328
128.6...

result:

ok 1000 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 3888kb

input:

100
10
-55 -84
91 -82
-31 -73
90 -68
-22 -52
87 -22
-12 -8
28 13
4 24
-95 57
-8 28
10
-31 -73
27 -59
-53 38
4 24
48 -62
-66 -30
4 24
-1 -55
-11 -25
-55 -84
10
-65 -63
90 -53
-49 -52
80 -49
-43 -23
70 11
3 25
64 32
5 52
-95 81
5 52
10
-43 -23
-70 -39
31 2
-43 -23
3 25
-75 4
90 -53
59 -55
-15 43
70 11...

output:

103.585713300628
130.713236700046
46.097722286464
12.649110640674
151.926440135643
82.024386617640
12.649110640674
102.428965452584
53.250926918476
121.461928191512
89.044932477935
117.923704148063
63.309314605349
89.044932477935
27.073972741362
93.295230317525
257.662715343616
226.700776900071
21.9...

result:

ok 1000 numbers

Test #6:

score: 0
Accepted
time: 0ms
memory: 4124kb

input:

100
10
97 -97
88 -18
76 2
45 -42
32 18
-25 -50
-25 81
-31 -66
-70 91
-94 -78
20 -86
10
-25 -50
-25 18
22 -1
-94 -78
-25 80
-3 -61
76 2
-25 37
29 -43
-94 -78
10
85 -82
83 -24
59 4
57 -40
55 38
-3 -40
-4 39
-39 -65
-58 96
-72 -68
-48 -45
10
55 38
-3 -40
52 11
55 38
57 -40
46 -28
-58 96
-4 39
13 -35
-7...

output:

57.628118136896
125.628118136896
85.023526156000
114.280357017293
187.628118136896
33.970575502926
104.307238483242
144.628118136896
43.931765272978
114.280357017293
162.961749242867
65.760926201084
140.767592571480
162.961749242867
121.133526698996
114.635541678076
141.354165131417
131.663202665963...

result:

ok 1000 numbers

Test #7:

score: 0
Accepted
time: 4ms
memory: 3836kb

input:

100
10
-42 -98
87 -85
-6 -58
79 -49
-3 -16
62 -14
39 4
56 70
53 72
-88 89
-15 50
10
-42 -98
-6 -58
-49 -31
-88 89
53 72
-35 56
-88 89
25 -67
22 69
-3 -16
10
-78 -65
74 -10
-75 4
56 8
-69 31
38 41
-53 67
-9 88
-44 89
-89 101
-67 -50
10
-89 101
-39 63
56 -16
-89 101
-4 53
-64 -17
-44 89
-74 97
-77 79
...

output:

150.442680114388
108.374351209131
87.846456957580
82.764726786234
71.470273540823
20.880613017821
82.764726786234
140.654375992269
41.593268686171
67.082039324994
152.594478163838
126.111434026626
127.612695293219
152.594478163838
150.870163484894
33.136083051562
145.406540326256
147.594752444516
12...

result:

ok 1000 numbers

Test #8:

score: 0
Accepted
time: 0ms
memory: 3880kb

input:

1
16
-9 0
-8 -10
-7 0
-6 -10
-5 0
-4 -10
-3 0
100 0
-3 10
-4 0
-5 10
-6 0
-7 10
-8 0
-9 10
-10 0
32 0
16
-10 0
-9 10
-8 0
-7 10
-6 0
-5 10
-4 0
-3 10
100 0
-3 0
-4 -10
-5 0
-6 -10
-7 0
-8 -10
-9 0

output:

42.000000000000
50.049875621121
40.000000000000
48.049875621121
38.000000000000
46.049875621121
36.000000000000
36.400549446403
68.000000000000
35.000000000000
45.049875621121
37.000000000000
47.049875621121
39.000000000000
49.049875621121
41.000000000000

result:

ok 16 numbers

Test #9:

score: 0
Accepted
time: 4ms
memory: 3884kb

input:

100
10
75 -40
78 14
64 67
54 94
2 6
-9 -15
-2 -35
-31 -67
-25 -88
73 -56
-2 -35
10
-2 -35
-31 -67
69 -55
64 67
-25 -88
60 -4
54 94
-29 -74
61 -41
-2 -35
10
-45 -55
-90 -66
-79 -91
15 -82
8 -73
87 -36
77 -32
-40 45
-26 -2
-72 -25
-90 -66
10
87 -36
-44 -11
-6 -22
-72 -25
-64 -21
-65 -65
-45 -55
-44 -1...

output:

0.000000000000
43.185645763378
73.763134423640
121.490740387900
57.775427302617
69.318107302493
140.630722105804
47.434164902526
63.285069329187
0.000000000000
179.524371604526
90.336301456933
97.413098385381
86.685811428823
85.273623475903
25.019992006394
46.324939287602
90.336301456933
86.57944328...

result:

ok 1000 numbers

Test #10:

score: -100
Wrong Answer
time: 2ms
memory: 4056kb

input:

100
10
-6 -50
25 -91
36 -62
49 -60
66 -41
75 24
89 39
-46 46
-38 1
1 -85
1 -85
10
36 -62
-46 46
-19 -16
-38 1
-4 -60
32 6
-46 46
1 -85
50 -1
-6 -50
10
98 95
41 54
-72 69
-56 47
-8 -3
-45 -45
-16 -52
37 3
31 -74
91 25
37 3
10
41 54
93 45
87 48
41 54
96 75
86 46
-16 -52
98 95
57 31
-45 -45
10
-16 2
9 ...

output:

79.373795930833
139.176147381654
71.840100222647
94.429868156214
25.495097567964
103.368833857904
139.176147381654
0.000000000000
110.104157284292
35.693136595149
51.156622249715
70.000000000000
67.268120235369
51.156622249715
93.085981758802
65.192024052026
76.380625815713
110.385687478042
34.40930...

result:

wrong answer 770th numbers differ - expected: '109.0412766', found: '129.5490697', error = '0.1880737'