QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#319072#8082. Minimum Euclidean DistancePetroTarnavskyi#AC ✓318ms4212kbC++203.5kb2024-02-01 18:53:232024-02-01 18:53:23

Judging History

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

  • [2024-02-01 18:53:23]
  • 评测
  • 测评结果:AC
  • 用时:318ms
  • 内存:4212kb
  • [2024-02-01 18:53:23]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db; 

const db PI = acos(-1.0);
const db EPS = 1e-9;
const db INF = 1e47;

void updMin(db& a, db b)
{
	a = min(a, b);
}

struct Pt
{
	db x, y;
	Pt operator+(const Pt& p) const
	{
		return {x + p.x, y + p.y};
	}
	Pt operator-(const Pt& p) const
	{
		return {x - p.x, y - p.y};
	}
	Pt operator*(db d) const
	{
		return {x * d, y * d};
	}
	Pt operator/(db d) const
	{
		return {x / d, y / d};
	}
};

db sq(const Pt& p)
{
	return p.x * p.x + p.y * p.y;
}

db abs(const Pt& p)
{
	return sqrt(sq(p));
}

int sgn(db x)
{
	return (EPS < x) - (x < -EPS);
}

Pt perp(const Pt& p)
{
	return {-p.y, p.x};
}

db dot(const Pt& p, const Pt& q)
{
	return p.x * q.x + p.y * q.y;
}

db cross(const Pt& p, const Pt& q)
{
	return p.x * q.y - p.y * q.x;
}

db orient(const Pt& p, const Pt& q, const Pt& r)
{
	return cross(q - p, r - p) / abs(q - p);
}

struct Line
{
	Pt n;
	db c;
	Line(const Pt& p, const Pt& q):
		n(perp(q - p)), c(-dot(n, p)) {}
	db side(const Pt& p) const
	{
		return dot(n, p) + c;
	}
	db dist(const Pt& p) const
	{
		return abs(side(p)) / abs(n);
	}
	bool cmpProj(const Pt& p, const Pt& q) const
	{
		return sgn(cross(p, n) - cross(q, n)) < 0;
	}
	/*Pt proj(const Pt& p) const
	{
		return p - n * side(p) / sq(n);
	}*/
};

/*bool inDisk(const Pt& a, const Pt& b, const Pt& p)
{
	return sgn(dot(a - p, b - p)) <= 0;
}

bool onSegment(const Pt& a, const Pt& b, const Pt& p)
{
	return sgn(orient(a, b, p)) == 0 && inDisk(a, b, p);`
}*/

db segPt(const Pt& a, const Pt& b, const Line& l, const Pt& p)
{
	if (l.cmpProj(a, p) && l.cmpProj(p, b))
		return l.dist(p);
	return min(abs(p - a), abs(p - b));
}

bool inConvexPolygon(const vector<Pt>& v, const Pt& a)
{
	assert(SZ(v) >= 3);
	if (sgn(orient(v.back(), v[0], a)) < 0
		|| sgn(orient(v[0], v[1], a)) < 0)
		return false;
	int i = lower_bound(v.begin() + 2, v.end(),
		a, [&](const Pt& p, const Pt& q)
	{
		return sgn(orient(v[0], p, q)) > 0;
	}) - v.begin();
	return sgn(orient(v[i - 1], v[i], a)) >= 0;
}

inline db f(db r, db d, db x)
{
	return sqrt(r * r - x * x) * (d - x) * (d - x);
}

db g(db r, db d)
{
	const int N = 47444;
	db a = -r, b = r, h = (b - a) / N, r2 = r * r;
	auto f = [&](db x)
	{
		return sqrt(r2 - x * x) * (d - x) * (d - x);
	};
	db res = f(a) + f(b);
	FOR(i, 1, N)
	{
		res += (i & 1 ? 4 : 2) * f(a + i * h);
	}
	return res * h / 3;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int n, m;
	cin >> n >> m;
	vector<Pt> v(n);
	for (Pt& p : v)
		cin >> p.x >> p.y;
	vector<Line> lines;
	FOR(i, 0, n)
		lines.PB({v[i], v[(i + 1) % n]});
	const db HALF_SQRT2 = sqrt(2) / 2;
	while (m--)
	{
		Pt p1, p2;
		cin >> p1.x >> p1.y >> p2.x >> p2.y;
		Pt p = (p1 + p2) / 2;
		db r = abs(p2 - p1) / 2;
		db d = 1e47;
		if (inConvexPolygon(v, p))
			d = 0;
		else
		{
			FOR(i, 0, n)
			{
				int j = i + 1 == n ? 0 : i + 1;
				d = min(d, segPt(v[i], v[j], lines[i], p));
			}
		}
		cout << 4 * g(r, HALF_SQRT2 * d) / (PI * r * r) << "\n";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4008kb

input:

4 3
0 0
1 0
1 1
0 1
0 0 1 1
1 1 2 2
1 1 2 3

output:

0.2499999600
0.7499999400
1.8749998500

result:

ok Your answer is acceptable!^ ^

Test #2:

score: 0
Accepted
time: 1ms
memory: 3768kb

input:

48 10
-30 0
-29 -4
-28 -7
-27 -9
-25 -12
-22 -16
-21 -17
-17 -20
-14 -22
-12 -23
-9 -24
-5 -25
-4 -25
0 -24
3 -23
5 -22
8 -20
12 -17
13 -16
16 -12
18 -9
19 -7
20 -4
21 0
21 1
20 5
19 8
18 10
16 13
13 17
12 18
8 21
5 23
3 24
0 25
-4 26
-5 26
-9 25
-12 24
-14 23
-17 21
-21 18
-22 17
-25 13
-27 10
-28 ...

output:

589.4999120159
51.4705859359
1051.2498317539
66.6249893371
174.1249866564
562.6749596472
272.3942182352
287.3849605788
689.6248896297
436.2499301809

result:

ok Your answer is acceptable!^ ^

Test #3:

score: 0
Accepted
time: 283ms
memory: 4112kb

input:

5000 5000
-50000000 0
-49999885 -49450
-49999770 -85675
-49999604 -122394
-49999391 -157604
-49999130 -192731
-49998803 -229143
-49998399 -267196
-49997956 -303872
-49997469 -339362
-49996891 -377221
-49996257 -414903
-49995577 -451819
-49994843 -488600
-49994059 -524941
-49993173 -563137
-49992252 ...

output:

2214785280946219.2500000000
1632644992030682.2500000000
3954739403567929.5000000000
5405105240246253.0000000000
817274591297097.5000000000
902260761373196.8750000000
3194362650210357.0000000000
1619744187094202.2500000000
363457427252680.2500000000
4776425129440740.0000000000
8267595107780599.000000...

result:

ok Your answer is acceptable!^ ^

Test #4:

score: 0
Accepted
time: 262ms
memory: 3996kb

input:

2224 5000
-500000 0
-499999 -30
-499998 -59
-499997 -87
-499996 -114
-499995 -140
-499994 -165
-499993 -189
-499992 -212
-499991 -234
-499990 -255
-499989 -275
-499988 -294
-499987 -312
-499986 -329
-499985 -345
-499984 -360
-499982 -389
-499981 -403
-499979 -430
-499978 -443
-499976 -468
-499975 -4...

output:

931340745585.1246337891
410570408785.1343994141
225774954308.8088073730
686588068996.9923095703
803635092294.7229003906
440321773164.5983276367
781364816087.1220703125
303496593309.1243896484
146653870961.5962829590
1361017605563.3295898438
409648985516.5971069336
417747394597.7048950195
46509177909...

result:

ok Your answer is acceptable!^ ^

Test #5:

score: 0
Accepted
time: 236ms
memory: 4080kb

input:

4672 5000
-300 0
-299 -43
-298 -85
-297 -126
-296 -166
-295 -205
-294 -243
-293 -280
-292 -316
-291 -351
-290 -385
-289 -418
-288 -450
-287 -481
-286 -511
-285 -540
-284 -568
-283 -595
-282 -621
-281 -646
-280 -670
-279 -693
-278 -715
-276 -758
-275 -779
-273 -820
-272 -840
-270 -879
-269 -898
-267 ...

output:

356616.4429257130
121018.4806317299
0.2499999600
189.6249696517
103099.6084995362
83253.1116758470
131701.2289220215
58352.4906610396
355863.0680462854
197638.8289603533
605772.3592417414
2062.4455760548
113763.2317928884
134694.4784429701
74679.6487957371
114481.2316779769
60577.2403049821
7456.248...

result:

ok Your answer is acceptable!^ ^

Test #6:

score: 0
Accepted
time: 212ms
memory: 4052kb

input:

576 5000
-300 0
-299 -15
-298 -29
-297 -42
-296 -54
-295 -65
-294 -75
-293 -84
-292 -92
-290 -107
-289 -114
-287 -127
-286 -133
-284 -144
-283 -149
-280 -163
-278 -172
-275 -185
-274 -189
-270 -204
-267 -215
-265 -222
-262 -232
-258 -245
-257 -248
-252 -262
-248 -273
-245 -281
-240 -294
-238 -299
-2...

output:

189295.2197044544
377943.2564451918
299472.9520711803
243821.9038679263
559270.9696913881
100367.5882060937
472743.0493403571
374450.5650714700
77260.6126349100
106891.2167826024
193578.0940190070
98895.0549773552
124019.9801513586
296138.8629881694
1209.1248064869
480040.5481724364
133543.965224840...

result:

ok Your answer is acceptable!^ ^

Test #7:

score: 0
Accepted
time: 211ms
memory: 3872kb

input:

336 5000
-300 0
-299 -11
-298 -21
-297 -30
-296 -38
-295 -45
-294 -51
-292 -62
-291 -67
-289 -76
-288 -80
-285 -91
-283 -98
-280 -108
-279 -111
-275 -122
-272 -130
-270 -135
-267 -142
-263 -151
-258 -162
-257 -164
-251 -175
-246 -184
-242 -191
-239 -196
-234 -204
-227 -215
-225 -218
-218 -228
-213 -...

output:

478.1249234790
408.1249346821
1341.2497853411
861.2498621622
4209.9993262153
1709.1247264650
846.2498645629
1389.1247776790
722.4998843683
753.1248794670
574.2499080948
1167.1248132088
439.6249296407
5650.2490957121
619.6249008328
2664.4995735631
2138.6246577262
2138.6246577262
1226.2498037462
1226....

result:

ok Your answer is acceptable!^ ^

Test #8:

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

input:

336 5000
-300 0
-299 -11
-298 -21
-297 -30
-296 -38
-295 -45
-294 -51
-292 -62
-291 -67
-289 -76
-288 -80
-285 -91
-283 -98
-280 -108
-279 -111
-275 -122
-272 -130
-270 -135
-267 -142
-263 -151
-258 -162
-257 -164
-251 -175
-246 -184
-242 -191
-239 -196
-234 -204
-227 -215
-225 -218
-218 -228
-213 -...

output:

95.1249847758
8382.6236584122
77361.1126188256
142408.1022084517
98056.2343066977
110581.2323021479
20412.9967330243
1253.1247994450
64468.6146821914
8915.6235731088
93179.1100872516
26286.2457930467
35118.2443795391
129681.2292453098
59545.6154700874
49997.9069436479
1685.1247303061
58020.615714154...

result:

ok Your answer is acceptable!^ ^

Test #9:

score: 0
Accepted
time: 308ms
memory: 4212kb

input:

5000 5000
-50000000 0
-49999912 -33572
-49999824 -59400
-49999710 -83347
-49999578 -105149
-49999382 -130924
-49999166 -154591
-49998916 -178069
-49998599 -203894
-49998262 -228398
-49997905 -251647
-49997466 -277451
-49997003 -302413
-49996511 -326907
-49995964 -352128
-49995393 -376671
-49994795 -...

output:

481990607715289152.0000000000
900047221765654016.0000000000
84250221940250688.0000000000
357963431771590208.0000000000
758024679881268480.0000000000
651805734823146624.0000000000
422072165676697024.0000000000
571948867049000960.0000000000
685946918254071936.0000000000
781017486185058688.0000000000
3...

result:

ok Your answer is acceptable!^ ^

Test #10:

score: 0
Accepted
time: 318ms
memory: 4164kb

input:

5000 5000
-50000000 0
-49999762 -138397
-49999461 -244153
-49999007 -349713
-49998392 -456086
-49997577 -566637
-49996632 -673023
-49995462 -784273
-49994137 -894156
-49992625 -1005080
-49990945 -1115094
-49989066 -1226720
-49987021 -1337531
-49984788 -1449227
-49982415 -1559155
-49979887 -1668061
-...

output:

259053235621570336.0000000000
472297822652967680.0000000000
271976504784319680.0000000000
930648802004915712.0000000000
110596169799101664.0000000000
385963628920199744.0000000000
441658509614740352.0000000000
259108170190959360.0000000000
379723525852206848.0000000000
43293947130702536.0000000000
2...

result:

ok Your answer is acceptable!^ ^

Extra Test:

score: 0
Extra Test Passed