QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#561943#7693. Convex Hull Extensionsha7dowWA 0ms3804kbC++146.4kb2024-09-13 13:25:182024-09-13 13:25:18

Judging History

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

  • [2024-09-13 13:25:18]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3804kb
  • [2024-09-13 13:25:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long; 

using db = double;
using ldb = long double; 
#define _T template <class T>
#define _FT template<class T, class FT = typename common_type<T, double>::type>
#define _F(X, Y) using X = Y<T, FT>; using F##X = Y<FT>

_T constexpr T eps = 0;
template<> constexpr double eps<double> = 1e-9;
template<> constexpr long double eps<long double> = 1e-11;

_T int sign(T x) {
    return (x > eps<T>) - (x < -eps<T>);
}

_T int cmp(T x, T y) {
    return sign(x - y);
}

_FT struct Point {
    _F(P, Point);

    T x, y;

    Point() = default;
    Point(T x, T y) : x(x), y(y) {}

    T det(const P& p) const { return x * p.y - y * p.x; }

    P operator+(const P& p) const { return {x + p.x, y + p.y}; }
    P operator-(const P& p) const { return {x - p.x, y - p.y}; }
    P operator*(T d) const { return {x * d, y * d}; }
    P operator-() const { return {-x, -y}; }

    P rotateCc2() const { return {-y, x}; }

    T abs2() const { return x * x + y * y; }
    T distTo2(const P& p) const { return (*this - p).abs2(); }
    
    FT abs() const { return sqrt(FT(abs2())); }
    FT distTo(const P& p) const { return (*this - p).abs(); }

    friend istream& operator>>(istream& is, P& p) {
        return is >> p.x >> p.y;
    }
    friend ostream& operator<<(ostream& os, const P& p) {
        return os << p.x << ' ' << p.y;
    }
};


_FT struct Line {
    _F(L, Line);
    _F(P, Point);

    P u, v;

    Line() = default;
    Line(P u, P v) : u(u), v(v) {}

    T cross(const P& p) const {  // uv.det(up)
        return (v.x - u.x) * (p.y - u.y) - (p.x - u.x) * (v.y - u.y);
    }
    int toLeft(const P& p) const { return sign(cross(p)); }

    FT distTo(const P& p) const {
        return fabs((v - u).det(p - u) / u.distTo(v));
    }
};

_T T floor(T x, T y) {
    return x >= 0 ? x / y : (x + 1) / y - 1;
}

_T T ceil(T x, T y) {
    return x <= 0 ? x / y : (x - 1) / y + 1;
}

_T T exgcd(T a, T b, T& x, T& y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    ll d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

_T T euclid(T a, T b, T c, T n)
{
    T p = floor(a, c), q = floor(b, c);
    if (p || q)
        return n * (n - 1) / 2 * p + n * q +
               euclid(a - p * c, b - q * c, c, n);
    T m = a * n + b;
    return m < c ? 0 : euclid(c, m % c, a, m / c);
}


_T T countLine(T A, T B, T C, T x1, T x2, T y1, T y2) {
    if (x1 > x2 || y1 > y2) return 0;
    C = -C;
    assert(A || B);
    if (A < 0) tie(x1, x2) = make_pair(-x2, -x1), A = -A;
    if (B < 0) tie(y1, y2) = make_pair(-y2, -y1), B = -B;
    T x, y;
    T d = exgcd(A, B, x, y);
    if (C % d != 0) {
        return 0;
    }
    if (!A) return x2 - x1 + 1;
    if (!B) return y2 - y1 + 1;
    A /= d, B /= d, C /= d;
    x = x * (C % B) % B;
    y = (C - A * x) / B;
    T l = max(ceil(x1 - x, B), ceil(y - y2, A));
    T r = min(floor(x2 - x, B), floor(y - y1, A));
    return max(T(0), r - l + 1);
}


_T T count(const vector<Line<T>>& l) {
    T s = 0;
    vector<T> a, b, c;
    for (auto [u, v] : l)
    {
        a.emplace_back(v.y - u.y);
        b.emplace_back(u.x - v.x);
        c.emplace_back(u.x * -a.back() + u.y * -b.back());
    }
    for (int i = 0; i < l.size(); i++)
    {
        int u = i - 1 >= 0 ? i - 1 : l.size() - 1,
            v = i + 1 < l.size() ? i + 1 : 0;
        T ux = b[u] * c[i] - b[i] * c[u],
          uy = c[u] * a[i] - c[i] * a[u],
          un = a[u] * b[i] - a[i] * b[u],
          vx = b[i] * c[v] - b[v] * c[i],
          vy = c[i] * a[v] - c[v] * a[i],
          vn = a[i] * b[v] - a[v] * b[i];
        T x1, x2, y1, y2;
        if (a[i] > 0) {
            y1 = ceil(uy, un), y2 = floor(vy, vn);
        } else {
            y1 = ceil(vy, vn), y2 = floor(uy, un); 
        }
        if (b[i] < 0) {
            x1 = ceil(ux, un), x2 = floor(vx, vn);
            s -= euclid(a[i], c[i] - 1, -b[i], x2 + 1) -
                 euclid(a[i], c[i] - 1, -b[i], x1);
        } else {
            x1 = ceil(vx, vn), x2 = floor(ux, un);
            if (b[i] > 0) {
                s += euclid(-a[i], -c[i], b[i], x2 + 1) -
                    euclid(-a[i], -c[i], b[i], x1);
            }
        }
        s -= countLine(a[i], b[i], c[i], x1, x2, y1, y2);
        if (ux % un == 0 && uy % un == 0) s++;
        
        if (b[i] < 0 && b[u] < 0 && ux % un == 0)
            s += ceil(uy, un) - 1;
        if (b[i] > 0 && b[v] > 0 && vx % vn == 0)
            s -= floor(vy, vn);
    }
    return s;
}

using P = Point<long long>;
using L = Line<long long>;


void solve() {
    int n;
    cin >> n;
    vector<P> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        p[i] = p[i] + P{1000010, 1000010};
    }

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        P a = p[i % n], b = p[(i + 1) % n], c = p[(i + 2) % n], d = p[(i + 3) % n];
        // cout << i << endl;
        if ((b - a).det(d - c) < 0) {
            cout << "infinitely many" << endl;
            return;
        } else if ((b - a).det(d - c) == 0) {
            // P vec = (b - a).rotateCc2();
            // P e = (b - a) * 10000000, f = e + vec;
            // vector<L> l;
            // l.push_back({a, b});
            // l.push_back({e, f});
            // l.push_back({c, d});
            // l.push_back({c, b});
            // long long s = count(l);
            // if (s) {
            //     cout << "infinitely many" << endl;
            //     return;
            // }
            if (cmp(L(a, b).distTo(c), 1.0) > 0) {
                cout << "infinitely many" << endl;
                return;
            }
            if (a.x == b.x || a.y == b.y) continue;
            if (b.x == c.x) {
                if (abs(a.x - b.x) == 1) continue;
            }
            if (b.y == c.y) {
                if (abs(a.y - b.y) == 1) continue;
            }
            cout << "infinitely many" << endl;
            return;
        } else {
            vector<L> l;
            l.push_back({a, b});
            l.push_back({c, d});
            l.push_back({c, b});
            ans += count(l);
        }
    }
    cout << ans << endl;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tc = 1;
    // cin >> tc;
    while (tc--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

23

result:

ok single line: '23'

Test #2:

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

input:

4
-7 -7
7 -7
7 7
-7 7

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #3:

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

input:

4
-1000 1000
-1000 999
-999 999
-999 1000

output:

0

result:

ok single line: '0'

Test #4:

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

input:

6
0 -901
900 -900
900 900
0 901
-900 900
-900 -900

output:

1457999998

result:

ok single line: '1457999998'

Test #5:

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

input:

6
900 -900
901 0
900 900
-900 900
-901 0
-900 -900

output:

1457999998

result:

ok single line: '1457999998'

Test #6:

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

input:

6
0 0
400 1
400 2
0 3
-400 2
-400 1

output:

1596

result:

ok single line: '1596'

Test #7:

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

input:

6
0 -901
900 -899
900 900
0 901
-900 900
-900 -899

output:

970921796

result:

ok single line: '970921796'

Test #8:

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

input:

6
2 -2
401 399
399 401
-2 2
-401 -399
-399 -401

output:

4794

result:

ok single line: '4794'

Test #9:

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

input:

6
399 -401
401 -399
2 2
-399 401
-401 399
-2 -2

output:

4794

result:

ok single line: '4794'

Test #10:

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

input:

4
-1 -1
-2 -2
-2 -3
-1 -2

output:

0

result:

ok single line: '0'

Test #11:

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

input:

4
0 0
0 1
-1 2
-1 1

output:

0

result:

ok single line: '0'

Test #12:

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

input:

48
5 -70
14 -68
22 -66
31 -63
39 -58
46 -52
52 -46
58 -39
63 -31
66 -22
68 -14
70 -5
70 5
68 14
66 22
63 31
58 39
52 46
46 52
39 58
31 63
22 66
14 68
5 70
-5 70
-14 68
-22 66
-31 63
-39 58
-46 52
-52 46
-58 39
-63 31
-66 22
-68 14
-70 5
-70 -5
-68 -14
-66 -22
-63 -31
-58 -39
-52 -46
-46 -52
-39 -58
...

output:

36

result:

ok single line: '36'

Test #13:

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

input:

4
-10 -10
-11 11
-11 10
-10 -11

output:

0

result:

ok single line: '0'

Test #14:

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

input:

4
10 -10
10 -11
11 10
11 11

output:

0

result:

ok single line: '0'

Test #15:

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

input:

4
5 5
6 5
-5 6
-6 6

output:

0

result:

ok single line: '0'

Test #16:

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

input:

4
100 -99
-99 -98
-100 -98
99 -99

output:

0

result:

ok single line: '0'

Test #17:

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

input:

4
0 1
-1 0
0 -1
1 0

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #18:

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

input:

4
-1000 0
0 -1000
1000 0
0 1000

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #19:

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

input:

4
-1000 1000
-1000 -1000
1000 -1000
1000 1000

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #20:

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

input:

4
0 0
0 2
-1 2
-1 1

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #21:

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

input:

4
-3 -2
-4 -2
-4 -3
-3 -4

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #22:

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

input:

4
6 -2
5 -2
4 -3
6 -3

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #23:

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

input:

48
4 -60
12 -59
19 -57
26 -54
33 -50
39 -45
45 -39
50 -33
54 -26
57 -19
59 -12
60 -4
60 4
59 12
57 19
54 26
50 33
45 39
39 45
33 50
26 54
19 57
12 59
4 60
-4 60
-12 59
-19 57
-26 54
-33 50
-39 45
-45 39
-50 33
-54 26
-57 19
-59 12
-60 4
-60 -4
-59 -12
-57 -19
-54 -26
-50 -33
-45 -39
-39 -45
-33 -50
...

output:

40

result:

ok single line: '40'

Test #24:

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

input:

4
7 3
7 4
5 4
6 3

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #25:

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

input:

4
-1000 0
-999 -1000
-998 0
-999 1000

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #26:

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

input:

4
0 -1000
1000 -999
0 -998
-1000 -999

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #27:

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

input:

3
999 -1000
1000 -1000
1000 -999

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #28:

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

input:

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

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #29:

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

input:

3
-1 0
0 1
-1 1

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #30:

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

input:

3
5 0
5 1
4 1

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #31:

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

input:

3
-777 -777
777 776
0 0

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #32:

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

input:

3
42 -13
42 -14
44 -13

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #33:

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

input:

3
-123 55
-122 57
-123 57

output:

infinitely many

result:

ok single line: 'infinitely many'

Test #34:

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

input:

48
7 -99
19 -98
32 -94
44 -89
55 -83
66 -75
75 -66
83 -55
89 -44
94 -32
98 -19
99 -7
99 7
98 19
94 32
89 44
83 55
75 66
66 75
55 83
44 89
32 94
19 98
7 99
-7 99
-19 98
-32 94
-44 89
-55 83
-66 75
-75 66
-83 55
-89 44
-94 32
-98 19
-99 7
-99 -7
-98 -19
-94 -32
-89 -44
-83 -55
-75 -66
-66 -75
-55 -83
...

output:

156

result:

ok single line: '156'

Test #35:

score: -100
Wrong Answer
time: 0ms
memory: 3752kb

input:

5
0 -1000
1000 -999
999 1000
-1000 1000
-1000 -999

output:

-491504995043

result:

wrong answer 1st lines differ - expected: '7986005002', found: '-491504995043'