QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562845#8669. 正方形计数sha7dow35 2447ms3844kbC++146.8kb2024-09-13 21:30:312024-09-13 21:30:31

Judging History

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

  • [2024-09-13 21:30:31]
  • 评测
  • 测评结果:35
  • 用时:2447ms
  • 内存:3844kb
  • [2024-09-13 21:30:31]
  • 提交

answer

#pragma GCC optimize("Ofast")

#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) {}
    template <class U, class FU>
    explicit Point(const Point<U, FU>& p) : x(p.x), y(p.y) {}

    T det(const P& p) const { return x * p.y - y * p.x; }
    T dot(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-(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}; }

    FP operator/(FT d) const { return {x / d, y / d}; }

    int quad() const {
        return sign(y) ? sign(y) + (sign(y) > 0) : sign(x) - (sign(x) < 0);
    }

    struct paCmp {
        bool operator()(const P& p, const P& q) const {
            int x = p.quad(), y = q.quad();
            if (x != y) return x < y;
            const auto dt = p.det(q);
            return sign(dt) > 0;
        }
    };

    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) {}
    template <class U, class FU>
    explicit Line(const Line<U, FU>& l) : u(l.u), v(l.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);
    }
    // cross 的符号, [1, 0, -1]: p 在 uv [左边, 上, 右边].
    int toLeft(const P& p) const { return sign(cross(p)); }

    FP getInter(const L& l) const {
        T c1 = l.cross(u), c2 = -l.cross(v);
        return (u * c2 + v * c1) / (c1 + c2);
    }

    struct paCmp {
        bool operator()(const L& l1, const L& l2) const {
            P d1 = l1.v - l1.u, d2 = l2.v - l2.u;
            if (sign(d1.det(d2)) == 0 && sign(d1.dot(d2)) >= 0)
                return l1.toLeft(l2.u) < 0;
            return typename P::paCmp()(d1, d2);
        }
    };

    static vector<L> halfplane(vector<L>& l, T lim) {
        l.push_back({{-lim, 0}, {-lim, -1}}); l.push_back({{0, -lim},{1, -lim}});
        l.push_back({{lim, 0}, {lim, 1}}); l.push_back({{0, lim}, {-1, lim}});
        auto check = [](const L& l1, const L& l2, const L& l3) {
            return FL(FP(l1.u), FP(l1.v)).toLeft(l2.getInter(l3)) < 0;
        };
        deque<L> q;
        sort(l.begin(), l.end(), paCmp());
        for (int i = 0; i < l.size(); i++) {
            P p = l[i].v - l[i].u;
            if (i > 0 && (l[i - 1].v - l[i - 1].u).det(p) == 0 &&
                sign((l[i - 1].v - l[i - 1].u).dot(p)) > 0)
                continue;
            while (q.size() > 1 &&
                   check(l[i], q.end()[-1], q.end()[-2]))
                q.pop_back();
            while (q.size() > 1 && check(l[i], q[0], q[1]))
                q.pop_front();
            if (!q.empty() && (q.back().v - q.back().u).det(p) <= 0)
                return {};
            q.emplace_back(l[i]);
        }
        while (q.size() > 1 && check(q.front(), q.end()[-1], q.end()[-2]))
            q.pop_back();
        while (q.size() > 1 && check(q.back(), q[0], q[1]))
            q.pop_front();
        return vector<L>(q.begin(), q.end());
    }
};


_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;
    }
    T d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

_T T euclid(T a, T b, T c, T n) {
    if (n < 0) return -euclid(-a, b - a, c, -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 countHalfplaneInter(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];
        if (b[i] < 0)
            s -= euclid(a[i], c[i] - 1, -b[i], floor(vx, vn) + 1) -
                 euclid(a[i], c[i] - 1, -b[i], floor(ux - 1, un) + 1);
        if (b[i] > 0)
            s += euclid(-a[i], -c[i], b[i], floor(ux, un) + 1) -
                 euclid(-a[i], -c[i], b[i], floor(vx - 1, vn) + 1);
        if (b[i] < 0 && b[u] < 0 && ux % un == 0)
            s += floor(uy - 1, un);
        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);
    ll V = 0;
    for (int i = 0; i < n; i++) {
        cin >> p[i];
        V = max({V, p[i].x, p[i].y});
    }
    reverse(p.begin(), p.end());
    
    vector<L> l;

    int ans = 0;

    for (int x = 1; x <= V; x++) {
        for (int y = 0; y <= V; y++) {
            l.clear();

            auto add = [&](P d) {
                for (int i = 0; i < n; i++) {
                    l.push_back({p[i] - d, p[(i + 1) % n] - d});
                }
            };

            add({0, 0});
            add({x, y});
            add({-y, x});
            add({x - y, x + y});

            l = L::halfplane(l, V + 10);

            ll cnt = countHalfplaneInter(l);
            // if (cnt) cout << x << " " << y << " " << cnt << endl;
            ans += cnt;
        }
    }
    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

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 2447ms
memory: 3552kb

input:

4
131 603
131 1828
1919 1828
1919 603

output:

405657336

result:

wrong answer 1st numbers differ - expected: '361182910200', found: '405657336'

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 1828ms
memory: 3564kb

input:

3
131 603
131 1828
1919 603

output:

-685200259

result:

wrong answer 1st numbers differ - expected: '63739309181', found: '-685200259'

Subtask #3:

score: 15
Accepted

Test #11:

score: 15
Accepted
time: 1ms
memory: 3636kb

input:

8
0 13
4 15
15 15
15 6
13 1
12 0
5 0
0 6

output:

4047

result:

ok 1 number(s): "4047"

Test #12:

score: 15
Accepted
time: 0ms
memory: 3568kb

input:

8
0 4
1 15
2 15
15 14
15 4
14 0
1 0
0 2

output:

4200

result:

ok 1 number(s): "4200"

Test #13:

score: 15
Accepted
time: 0ms
memory: 3504kb

input:

5
7 15
15 13
15 0
3 0
0 15

output:

3635

result:

ok 1 number(s): "3635"

Test #14:

score: 15
Accepted
time: 0ms
memory: 3772kb

input:

8
0 12
2 14
7 15
13 15
15 10
15 1
8 0
0 0

output:

4511

result:

ok 1 number(s): "4511"

Test #15:

score: 15
Accepted
time: 1ms
memory: 3568kb

input:

6
0 11
3 15
7 15
15 12
10 0
0 0

output:

3006

result:

ok 1 number(s): "3006"

Test #16:

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

input:

5
0 0
0 2
1 2
2 1
2 0

output:

4

result:

ok 1 number(s): "4"

Subtask #4:

score: 20
Accepted

Dependency #3:

100%
Accepted

Test #17:

score: 20
Accepted
time: 144ms
memory: 3840kb

input:

8
49 299
144 300
300 260
250 15
115 0
30 0
23 19
0 85

output:

443602646

result:

ok 1 number(s): "443602646"

Test #18:

score: 20
Accepted
time: 143ms
memory: 3620kb

input:

8
0 133
103 300
130 300
257 294
297 227
300 150
277 40
161 4

output:

351466521

result:

ok 1 number(s): "351466521"

Test #19:

score: 20
Accepted
time: 145ms
memory: 3620kb

input:

8
76 286
114 300
300 300
300 205
291 0
47 0
4 57
2 235

output:

605026927

result:

ok 1 number(s): "605026927"

Test #20:

score: 20
Accepted
time: 136ms
memory: 3620kb

input:

8
0 102
40 274
282 300
300 234
267 0
34 0
6 57
0 86

output:

497330741

result:

ok 1 number(s): "497330741"

Test #21:

score: 20
Accepted
time: 121ms
memory: 3556kb

input:

7
0 288
156 300
212 300
265 176
300 86
278 0
0 36

output:

446722651

result:

ok 1 number(s): "446722651"

Subtask #5:

score: 0
Wrong Answer

Dependency #4:

100%
Accepted

Test #22:

score: 0
Wrong Answer
time: 689ms
memory: 3552kb

input:

5
257 800
766 800
800 353
667 0
42 0

output:

1701500430

result:

wrong answer 1st numbers differ - expected: '18881369614', found: '1701500430'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%