QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#799706 | #8082. Minimum Euclidean Distance | SGColin | AC ✓ | 130ms | 4040kb | C++20 | 6.3kb | 2024-12-05 17:12:26 | 2024-12-05 17:12:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef double T;
typedef long double ld;
inline int rd() {
int x = 0;
bool f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return f ? -x : x;
}
#define let const auto
#define lett const T
#define letp const P // P for Point
#define lets const S // S for Segment
#define letl const L // L for Line
#define letc const C // C for Convex
const T eps = 1e-8;
const double PI = 3.1415926535897932384;
#define z(x) (abs((x)) <= eps) // is zero
inline double sqr(double x) {return x * x;}
struct P {
T x, y;
P (T x = 0, T y = 0) : x(x), y(y) {}
P operator + (letp &p) const {return {x + p.x, y + p.y};}
P operator - (letp &p) const {return {x - p.x, y - p.y};}
P operator * (lett &d) const {return {x * d, y * d};}
P operator / (lett &d) const {return {x / d, y / d};}
P operator - () const {return {-x, -y};}
T operator | (letp &p) const {return x * p.x + y * p.y;} // dot
T operator ^ (letp &p) const {return x * p.y - y * p.x;} // cross
bool operator == (letp &p) const {return z(x - p.x) && z(y - p.y);}
bool operator != (letp &p) const {return ! operator == (p);}
bool operator < (letp &p) const {return z(x - p.x) ? y < p.y : x < p.x;}
bool operator > (letp &p) const {return !(*this < p || *this == p);}
// left(counterclockwise) = 1 | on = 0 | right(clockwise) = -1
int ori(letp &p) const {T t = (*this) ^ p; return (t > eps) - (t < -eps);}
T norm() const {return x * x + y * y;}
T dis(letp &p) const {return sqrt(((*this) - p).norm());}
P proj (letp &p) const {return (*this) * (((*this) | p) / norm());}
P refl (letp &p) const {return proj(p) * 2 - p;}
} zero;
double abs(letp &p) {return sqrt(p.norm());}
P unit(letp &p) {return p / abs(p);}
P perp(letp &p) {return {-p.y, p.x};} // turn pi / 2 left(counterclockwise)
P perpr(letp &p) {return {p.y, -p.x};} // turn pi / 2 right(clockwise)
bool orth(letp &p, letp &q) {return z(p | q);} // orthogonal
bool para(letp &p, letp &q) {return z(p ^ q);} // parallel
struct argcmp { // compared by polar angle
bool operator() (letp &a, letp &b) const {
const auto quad = [](letp &a) {
if (a.y < -eps) return 1; // halfplane with negative y
if (a.y > eps) return 4; // halfplane with positive y
if (a.x < -eps) return 5; // negative x-axis
if (a.x > eps) return 3; // positive x-axis
return 2; // origin
};
const int qa = quad(a), qb = quad(b);
if (qa != qb) return qa < qb;
const auto t = (a ^ b); //in the same quad
/* sorted by length in increasing order when parallel
if (z(t)) return norm(a) < norm(b) - eps; */
return t > eps;
}
};
struct L {
P p, v;
int ori (letp &a) const {return v.ori(a - p);}
P inter(letl &l) const {return p + v * ((l.v ^ (p - l.p)) / (v ^ l.v));}
L shift(letp &d) const {return {p + d, v};}
L shiftl(double d) const {return {p + perp(v) * d / abs(v), v};}
P proj(letp &a) const {return p + v.proj(a - p);}
P refl(letp &a) const {return p + v.refl(a - p);}
double dis(letp &a) const {return abs(v ^ (a - p)) / abs(v);}
double dis2(letp &a) const {return sqr(v ^ (a - p)) / (v).norm();}
L perpl() const {return {p, perp(v)};}
};
bool orth(letl &p, letl &q) {return z(p.v | q.v);} // orthogonal
bool para(letl &p, letl &q) {return z(p.v ^ q.v);} // parallel
struct S {
P a, b;
// on endPs = -1 | out = 0 | in = 1
int is_on(letp &p) const {
if (p == a || p == b) return -1;
return (p - a).ori(p - b) == 0 && ((p - a) | (p - b)) < -eps;
}
// inter on endPs = -1 | not inter = 0 | inter inside = 1
int is_inter(letl &l) const {
if (l.ori(a) == 0 || l.ori(b) == 0) return -1;
return l.ori(a) != l.ori(b);
}
// inter on endPs = -1 | not inter = 0 | inter inside = 1
int is_inter(lets &s) const {
if (is_on(s.a) || is_on(s.b) || s.is_on(a) || s.is_on(b)) return -1;
letl &l{a, b - a}, ls{s.a, s.b - s.a};
return l.ori(s.a) * l.ori(s.b) == -1 && ls.ori(a) * ls.ori(b) == -1;
}
double dis(letp &p) const {
if (((p - a) | (b - a)) < -eps || ((p - b) | (a - b)) < -eps)
return min(abs(a - p), abs(b - p));
return L{a, b - a}.dis(p);
}
double dis2(letp &p) const {
if (((p - a) | (b - a)) < -eps || ((p - b) | (a - b)) < -eps)
return min((a - p).norm(), (b - p).norm());
return L{a, b - a}.dis2(p);
}
double dis(lets &s) const {
if (is_inter(s)) return 0.0;
return min({dis(s.a), dis(s.b), s.dis(a), s.dis(b)});
}
};
struct Polygon {
vector<P> p; // counterclockwise
Polygon(const vector<P> p = {}) : p(p) {}
size_t nxt(const size_t i) const {return i == p.size() - 1 ? 0 : i + 1;}
size_t pre(const size_t i) const {return i == 0 ? p.size() - 1 : i - 1;}
bool is_convex() const {
bool pos = false, neg = false;
for (size_t i = 0; i < p.size(); ++i) {
int o = (p[i] - p[pre(i)]).ori(p[nxt(i)] - p[i]);
if (o == 1) pos = true;
if (o == -1) neg = true;
}
return !(pos && neg);
}
};
#define N 100007
#define fr first
#define sc second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
struct C : Polygon {
C (const vector<P> &p = {}) : Polygon(p) {}
double dis(letp &tar) const {
bool fl = true;
double ans = 1e18;
for (size_t i = 0; i < p.size(); ++i) {
ans = min(ans, S{p[i], p[pre(i)]}.dis(tar));
if (L{p[i], p[nxt(i)] - p[i]}.ori(tar) < 0) fl = false;
}
return fl ? 0 : ans;
}
} A;
int main() {
int n = rd(), q = rd();
A.p.resize(n);
rep(i, 0, n - 1) {A.p[i].x = rd(); A.p[i].y = rd();}
rep(i, 1, q) {
P a, b, c;
a.x = rd(); a.y = rd();
b.x = rd(); b.y = rd();
c = (a + b) / 2;
double ans = (c - a).norm() / 2;
ans += sqr(A.dis(c));
printf("%.10lf\n", ans);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3844kb
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.2500000000 0.7500000000 1.8750000000
result:
ok Your answer is acceptable!^ ^
Test #2:
score: 0
Accepted
time: 0ms
memory: 3864kb
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.5000000000 51.4705882353 1051.2500000000 66.6250000000 174.1250000000 562.6750000000 272.3942307692 287.3850000000 689.6250000000 436.2500000000
result:
ok Your answer is acceptable!^ ^
Test #3:
score: 0
Accepted
time: 130ms
memory: 4000kb
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:
2214785369560633.0000000000 1632645104370924.5000000000 3954739966640761.0000000000 5405105667896786.0000000000 817274719687553.0000000000 902260846427661.1250000000 3194363161448624.0000000000 1619744446324385.0000000000 363457485421825.2500000000 4776425533214309.0000000000 8267595460255074.000000...
result:
ok Your answer is acceptable!^ ^
Test #4:
score: 0
Accepted
time: 59ms
memory: 3896kb
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:
931340796015.3750000000 410570465847.7500000000 225774975043.7500000000 686588110927.3748779297 803635163394.8750000000 440321806244.7500000000 781364862674.5000000000 303496624306.7500000000 146653887864.7500000000 1361017661096.7497558594 409649028457.5000000000 417747460932.7500000000 46509181005...
result:
ok Your answer is acceptable!^ ^
Test #5:
score: 0
Accepted
time: 121ms
memory: 3988kb
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.5000000000 121018.5000000000 0.2500000000 189.6250000000 103099.6250000000 83253.1250000000 131701.2500000000 58352.5000000000 355863.1250000000 197638.8597240473 605772.4121621621 2062.4458977408 113763.2500000000 134694.5000000000 74679.6520547945 114481.2500000000 60577.2500000000 7456.250...
result:
ok Your answer is acceptable!^ ^
Test #6:
score: 0
Accepted
time: 18ms
memory: 3916kb
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.2500000000 377943.2944162437 299473.0000000000 243821.9171974522 559270.9922279794 100367.5923396674 472743.1250000000 374450.6250000000 77260.6250000000 106891.2307692308 193578.1250000000 98895.0654145078 124020.0000000000 296138.8749999999 1209.1250000000 480040.6250000000 133543.970689655...
result:
ok Your answer is acceptable!^ ^
Test #7:
score: 0
Accepted
time: 11ms
memory: 3792kb
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.1250000000 408.1250000000 1341.2500000000 861.2500000000 4210.0000000000 1709.1250000000 846.2500000000 1389.1250000000 722.5000000000 753.1250000000 574.2500000000 1167.1250000000 439.6250000000 5650.2500000000 619.6250000000 2664.5000000000 2138.6250000000 2138.6250000000 1226.2500000000 1226....
result:
ok Your answer is acceptable!^ ^
Test #8:
score: 0
Accepted
time: 11ms
memory: 3980kb
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.1250000000 8382.6250000000 77361.1250000000 142408.1250000000 98056.2500000000 110581.2500000000 20413.0000000000 1253.1250000000 64468.6250000000 8915.6250000000 93179.1250000000 26286.2500000000 35118.2500000000 129681.2500000000 59545.6250000000 49997.9103773585 1685.1250000000 58020.625000000...
result:
ok Your answer is acceptable!^ ^
Test #9:
score: 0
Accepted
time: 130ms
memory: 4040kb
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:
481990667522174080.0000000000 900047257776892032.0000000000 84250235108292080.0000000000 357963472278544256.0000000000 758024710210129280.0000000000 651805790712522752.0000000000 422072215223185920.0000000000 571948904059660608.0000000000 685946954834849920.0000000000 781017527404628096.0000000000 3...
result:
ok Your answer is acceptable!^ ^
Test #10:
score: 0
Accepted
time: 130ms
memory: 3988kb
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:
259053256470500448.0000000000 472297859897907072.0000000000 271976522374482752.0000000000 930648882061705984.0000000000 110596174224097024.0000000000 385963660067947136.0000000000 441658538323309440.0000000000 259108189662189408.0000000000 379723545376251136.0000000000 43293951022380792.0000000000 2...
result:
ok Your answer is acceptable!^ ^
Extra Test:
score: 0
Extra Test Passed