QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562889#8669. 正方形计数sha7dow15 1847ms3840kbC++146.6kb2024-09-13 22:23:022024-09-13 22:23:02

Judging History

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

  • [2024-09-13 22:23:02]
  • 评测
  • 测评结果:15
  • 用时:1847ms
  • 内存:3840kb
  • [2024-09-13 22:23:02]
  • 提交

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);
    }
    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 (FP(u) * c2 + FP(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) {
        auto check = [](const L& l1, const L& l2, const L& l3) {
            return ((l1.v - l1.u) * (l2.v - l2.u).det(l3.v - l3.u))
                       .det((l2.u - l1.u) *
                                ((l2.v - l2.u).det(l3.v - l3.u)) +
                            (l2.v - l2.u) *
                                (l3.v - l3.u).det(l2.u - l3.u)) < 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());
    }
};


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

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

_T ll euclid(T a, T b, T c, ll 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);
    }
    ll m = a * n + b;
    return m < c ? 0 : euclid(c, T(m % c), a, m / c);
}

_T ll countHalfplaneInter(const vector<Line<T>>& l) {
    ll 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;
        ll ux = 1ll * b[u] * c[i] - 1ll * b[i] * c[u],
          uy = 1ll * c[u] * a[i] - 1ll * c[i] * a[u],
          un = a[u] * b[i] - a[i] * b[u],
          vx = 1ll * b[i] * c[v] - 1ll * b[v] * c[i],
          vy = 1ll * c[i] * a[v] - 1ll * 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<int>;
using L = Line<int>; 

void solve() {
    int n;
    cin >> n;
    vector<P> p(n);
    int 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;

    ll 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);
            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: 1729ms
memory: 3596kb

input:

4
131 603
131 1828
1919 1828
1919 603

output:

650869612476

result:

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

Subtask #2:

score: 0
Wrong Answer

Test #6:

score: 0
Wrong Answer
time: 1847ms
memory: 3492kb

input:

3
131 603
131 1828
1919 603

output:

3369813127592

result:

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

Subtask #3:

score: 15
Accepted

Test #11:

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

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: 1ms
memory: 3536kb

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: 1ms
memory: 3840kb

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: 1ms
memory: 3796kb

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: 0ms
memory: 3540kb

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: 3776kb

input:

5
0 0
0 2
1 2
2 1
2 0

output:

4

result:

ok 1 number(s): "4"

Subtask #4:

score: 0
Wrong Answer

Dependency #3:

100%
Accepted

Test #17:

score: 0
Wrong Answer
time: 117ms
memory: 3628kb

input:

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

output:

2059448117

result:

wrong answer 1st numbers differ - expected: '443602646', found: '2059448117'

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

0%