QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#605181#8082. Minimum Euclidean DistanceQingyyxAC ✓98ms4136kbC++206.9kb2024-10-02 15:57:532024-10-02 15:57:53

Judging History

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

  • [2024-10-02 15:57:53]
  • 评测
  • 测评结果:AC
  • 用时:98ms
  • 内存:4136kb
  • [2024-10-02 15:57:53]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define enl putchar('\n')
#define all(x) (x).begin(),(x).end()
#define debug(x) printf(" "#x":%d\n",x);
using namespace std;
const int MAXN = 5000 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
typedef pair<int, int> pii;
char buf[1 << 21], * p1 = buf, * p2 = buf, obuf[1 << 21], * o = obuf, of[35];
#define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline ll qpow(ll a, ll n) { ll res = 1; while (n) { if (n & 1)res = res * a % mod; n >>= 1; a = a * a % mod; }return res; }
template <class T = int>inline T read() { T s = 0, f = 1; char c = gc(); for (; !isdigit(c); c = gc())if (c == '-')f = -1; for (; isdigit(c); c = gc())s = s * 10 + c - '0'; return s * f; }
inline void read(int* a, int n) { for (int i = 1; i <= n; ++i)a[i] = read(); }
inline int inal(char* s) { int n = 0; for (s[0] = gc(); !isalpha(s[0]); s[0] = gc()); for (; isalpha(s[n]); s[++n] = gc()); return s[n] = 0, n; }
inline void outd(auto* a, int n) { for (int i = 1; i <= n; ++i)printf("%d ", a[i]); enl; }

int n, m, q;
const double eps = 1e-9;
int sgn(double x) { return (x > eps) - (x < -eps); }
// #define sgn(x) (((x) > eps) - ((x) < -eps))

struct Point {
    double x, y;
    double ang;
    Point(double x = 0, double y = 0) :x(x), y(y) {}
    Point operator -(const Point& b)const { return Point(x - b.x, y - b.y); }
    Point operator +(const Point& b)const { return Point(x + b.x, y + b.y); }
    Point operator *(double k)const { return Point(x * k, y * k); }
    double operator ^(const Point& b)const { return x * b.y - y * b.x; }
    double operator *(const Point& b)const { return x * b.x + y * b.y; }
    double operator !()const { return sqrt(*this * *this); }
    bool operator <(const Point& b) const { return sgn(y - b.y) < 0 || (sgn(y - b.y) == 0 && sgn(x - b.x) < 0); }
    bool operator ==(const Point& b)const { return !(*this < b) && !(b < *this); }
    bool operator >(const Point& b)const { return b < *this; }
    bool operator <=(const Point& b)const { return !(*this > b); }
    bool operator >=(const Point& b)const { return !(*this < b); }
    bool operator !=(const Point& b)const { return !(*this == b); }
    Point operator /(double k)const { return Point(x / k, y / k); }
    double operator ~()const { return atan2(y, x); }
    Point operator -()const { return Point(-x, -y); }
    double len2()const { return x * x + y * y; }
    double len()const { return !*this; }
    double dis(const Point& b)const { return (*this - b).len(); }
    double dis2(const Point& b)const { return (*this - b).len2(); }
    Point rotate(double a)const { return Point(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)); }
    Point rotate(double a, const Point& o)const { return Point(x * cos(a) - y * sin(a) + o.x, x * sin(a) + y * cos(a) + o.y); }
    Point unit()const { return *this / len(); }
    double sqr(const Point& b, const Point& c) {
        return (b - *this) ^ (c - *this);
    }
    double proj(const Point& b, const Point& c) {
        return (b - *this) * (c - *this);
    }
};

struct Line {
    Point s, e;
    double ang;
    Line(Point s = Point(0, 0), Point e = Point(0, 0)) :s(s), e(e) {}
    Line(double x1, double y1, double x2, double y2) :s(x1, y1), e(x2, y2) {}
    bool pojInLine(const Point& p)const {
        return sgn((p - s) * (e - s)) >= 0 && sgn((p - e) * (s - e)) >= 0;
    }

    bool inLine(const Point& p)const {
        return sgn((p - s) ^ (e - s)) == 0 && pojInLine(p);
    }
    bool isparallel(const Line& b)const {
        return sgn((e - s) ^ (b.e - b.s)) == 0;
    }
    bool isperpendicular(const Line& b)const {
        return sgn((e - s) * (b.e - b.s)) == 0;
    }
    static Point intersection(const Line& a, const Line& b) {        // 判交点 要判平行
        return a.s + (a.e - a.s) * ((a.s - b.s) ^ (b.e - b.s)) / ((b.e - b.s) ^ (a.e - a.s));
    }
    // bool isinter(const Line& b)const {
    //     if (isparallel(b)) return b.inLine(s) || b.inLine(e) || inLine(b.s) || inLine(b.e);
    //     Point p = intersection(*this, b);
    //     return b.inLine(p) && (*this).inLine(p);
    // }
    double angle() {
        return atan2(e.y - s.y, e.x - s.x);
    }
    bool isright(const Point& p)const {
        return sgn((p - s) ^ (e - s)) > 0;
    }
    bool isleft(const Point& p)const {
        return sgn((p - s) ^ (e - s)) < 0;
    }

    bool isinter(const Line& b)const {
        if (b.inLine(s) || b.inLine(e) || (*this).inLine(b.s) || (*this).inLine(b.e))return true;
        return (b.isright(s) ^ b.isright(e)) && ((*this).isright(b.s) ^ (*this).isright(b.e));
    }
    double dis(Point p) {
        if (!sgn(s.dis2(e)))return s.dis(p);
        if (sgn(s.proj(p, e)) < 0)return p.dis(s);
        if (sgn(e.proj(p, s)) < 0)return p.dis(e);
        return fabs(e.sqr(p, s)) / s.dis(e);
    }
    double dis(Line l) {
        return min({l.dis(s), l.dis(e), (*this).dis(l.s), (*this).dis(l.e)});
    }
};

struct polygon {
    int n;
    vector<Point>pt;
    vector<Line>le;


    bool inploygon(Point p) {
        if ((p ^ pt[1]) > 0 || (pt.back() ^ p) > 0) return 0;
        int ps = lower_bound(pt.begin() + 1, pt.end(), p, [&](const Point& a, const Point& b) {return sgn(a ^ b) > 0 || sgn(a ^ b) == 0 && a.dis2(p) < b.dis2(p); }) - pt.begin() - 1;
        if (Line(pt[ps], pt[(ps + 1) % n]).inLine(p))return 1;
        return sgn((p - pt[ps]) ^ (pt[(ps + 1) % n] - pt[ps])) < 0;
    }

    double dis(Point p) {
        if (inploygon(p))return 0;
        double res = 2e18;
        for (int i = 0; i < n; ++i)
            res = min(res, le[i].dis(p));
        return res;
    }
}A;

void solve() {
    cin >> n >> q;
    A.pt.resize(n);
    A.le.resize(n);
    A.n = n;
    int p = 0;
    for (int i = 0; i < n; ++i) {
        cin >> A.pt[i].x >> A.pt[i].y;
        if (A.pt[p] < A.pt[i])p = i;
    }
    rotate(A.pt.begin(), A.pt.begin() + p, A.pt.end());
    Point bs = A.pt[0];
    for (int i = 0; i < n; ++i)
        A.pt[i] = A.pt[i] - bs;
    for (int i = 0; i < n; ++i)
        A.le[i] = Line(A.pt[i], A.pt[(i + 1) % n]);
    for (int i = 1; i <= q; ++i) {
        Point S, T;
        cin >> S.x >> S.y >> T.x >> T.y;
        S = S - bs; T = T - bs;
        Point cent = (S + T) / 2;
        double AC = A.dis(cent);
        printf("%.12lf\n", 0.5 * cent.dis2(S) + AC * AC);
    }
}
signed main(signed argc, char const* argv[]) {
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    //=============================================================
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int TxT = 1;
    while (TxT--)
        solve();
    //=============================================================
#ifdef LOCAL
    end :
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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.250000000000
0.750000000000
1.875000000000

result:

ok Your answer is acceptable!^ ^

Test #2:

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

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.500000000000
51.470588235294
1051.250000000000
66.625000000000
174.125000000000
562.675000000000
272.394230769231
287.385000000000
689.625000000000
436.250000000000

result:

ok Your answer is acceptable!^ ^

Test #3:

score: 0
Accepted
time: 79ms
memory: 4036kb

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.000000000000
1632645104370924.500000000000
3954739966640761.000000000000
5405105667896786.000000000000
817274719687553.000000000000
902260846427661.125000000000
3194363161448624.000000000000
1619744446324385.000000000000
363457485421825.250000000000
4776425533214309.000000000000
826...

result:

ok Your answer is acceptable!^ ^

Test #4:

score: 0
Accepted
time: 47ms
memory: 3988kb

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.375000000000
410570465847.750000000000
225774975043.750000000000
686588110927.374877929688
803635163394.875000000000
440321806244.750000000000
781364862674.500000000000
303496624306.750000000000
146653887864.750000000000
1361017661096.749755859375
409649028457.500000000000
417747460932....

result:

ok Your answer is acceptable!^ ^

Test #5:

score: 0
Accepted
time: 32ms
memory: 4008kb

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.500000000000
121018.500000000000
0.250000000000
189.625000000000
103099.625000000000
83253.125000000000
131701.250000000000
58352.500000000000
355863.125000000000
197638.859724047303
605772.412162162131
2062.445897740785
113763.250000000000
134694.500000000000
74679.652054794511
114481.250000...

result:

ok Your answer is acceptable!^ ^

Test #6:

score: 0
Accepted
time: 10ms
memory: 4056kb

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.250000000000
377943.294416243676
299473.000000000000
243821.917197452218
559270.992227979354
100367.592339667433
472743.125000000000
374450.625000000000
77260.625000000000
106891.230769230766
193578.125000000000
98895.065414507786
124020.000000000000
296138.874999999942
1209.125000000000
4800...

result:

ok Your answer is acceptable!^ ^

Test #7:

score: 0
Accepted
time: 2ms
memory: 3920kb

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.125000000000
408.125000000000
1341.250000000000
861.250000000000
4210.000000000000
1709.125000000000
846.250000000000
1389.125000000000
722.500000000000
753.125000000000
574.250000000000
1167.125000000000
439.625000000000
5650.250000000000
619.625000000000
2664.500000000000
2138.625000000000
213...

result:

ok Your answer is acceptable!^ ^

Test #8:

score: 0
Accepted
time: 7ms
memory: 3976kb

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.125000000000
8382.625000000000
77361.125000000000
142408.125000000000
98056.250000000000
110581.250000000000
20413.000000000000
1253.125000000000
64468.625000000000
8915.625000000000
93179.125000000000
26286.250000000000
35118.250000000000
129681.250000000000
59545.625000000000
49997.910377358487...

result:

ok Your answer is acceptable!^ ^

Test #9:

score: 0
Accepted
time: 98ms
memory: 4056kb

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.000000000000
900047257776892032.000000000000
84250235108292080.000000000000
357963472278544256.000000000000
758024710210129280.000000000000
651805790712522752.000000000000
422072215223185920.000000000000
571948904059660608.000000000000
685946954834849920.000000000000
7810175274046...

result:

ok Your answer is acceptable!^ ^

Test #10:

score: 0
Accepted
time: 96ms
memory: 4136kb

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.000000000000
472297859897907072.000000000000
271976522374482752.000000000000
930648882061705984.000000000000
110596174224097024.000000000000
385963660067947136.000000000000
441658538323309440.000000000000
259108189662189408.000000000000
379723545376251136.000000000000
432939510223...

result:

ok Your answer is acceptable!^ ^

Extra Test:

score: 0
Extra Test Passed