QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#562943 | #8669. 正方形计数 | sha7dow | 85 | 2065ms | 3892kb | C++14 | 7.0kb | 2024-09-13 23:23:14 | 2024-09-13 23:23:16 |
Judging History
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);
}
};
};
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> lll;
for (int i = 0; i < n; i++) {
lll.push_back({p[i], p[(i + 1) % n]});
}
sort(lll.begin(), lll.end(), L::paCmp());
ll ans = 0;
for (int x = 1; x <= V; x++) {
for (int y = 0; y <= V; y++) {
auto l = lll;
auto halfplane = [&](vector<L>& l) {
auto check = [](const L& l1, const L& l2, const L& l3) {
return Line<db>(Point<db>(l1.u), Point<db>(l1.v)).toLeft(l2.getInter(l3)) < 0;
};
deque<L> q;
vector<L> nw, nl;
auto mer = [&](P d) {
nl.clear();
for (int i = 0; i < n; i++) {
nl.push_back({lll[i].u - d, lll[i].v - d});
}
nw.resize(n + l.size());
merge(l.begin(), l.end(), nl.begin(), nl.end(), nw.begin(), L::paCmp());
swap(l, nw);
};
mer({x, y});
mer({-y, x});
mer({x - y, x + y});
// cout << l.size() << endl;
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 vector<L>{};
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());
};
l = halfplane(l);
// cout << l.size() << endl;
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: 10
Accepted
Test #1:
score: 10
Accepted
time: 1529ms
memory: 3596kb
input:
4 131 603 131 1828 1919 1828 1919 603
output:
361182910200
result:
ok 1 number(s): "361182910200"
Test #2:
score: 10
Accepted
time: 332ms
memory: 3604kb
input:
4 239 211 239 962 261 962 261 211
output:
1498772
result:
ok 1 number(s): "1498772"
Test #3:
score: 10
Accepted
time: 2065ms
memory: 3604kb
input:
4 0 0 0 2000 2000 2000 2000 0
output:
1336001667000
result:
ok 1 number(s): "1336001667000"
Test #4:
score: 10
Accepted
time: 215ms
memory: 3716kb
input:
4 36 771 36 786 672 786 672 771
output:
427720
result:
ok 1 number(s): "427720"
Test #5:
score: 10
Accepted
time: 20ms
memory: 3592kb
input:
4 0 100 100 200 200 100 100 0
output:
34001650
result:
ok 1 number(s): "34001650"
Subtask #2:
score: 25
Accepted
Test #6:
score: 25
Accepted
time: 1313ms
memory: 3656kb
input:
3 131 603 131 1828 1919 603
output:
63739309181
result:
ok 1 number(s): "63739309181"
Test #7:
score: 25
Accepted
time: 290ms
memory: 3600kb
input:
3 239 211 239 962 261 211
output:
353073
result:
ok 1 number(s): "353073"
Test #8:
score: 25
Accepted
time: 1434ms
memory: 3692kb
input:
3 0 0 0 2000 2000 0
output:
222889277611
result:
ok 1 number(s): "222889277611"
Test #9:
score: 25
Accepted
time: 189ms
memory: 3548kb
input:
3 36 771 36 786 672 771
output:
98847
result:
ok 1 number(s): "98847"
Test #10:
score: 25
Accepted
time: 4ms
memory: 3600kb
input:
3 0 0 0 100 100 0
output:
1473186
result:
ok 1 number(s): "1473186"
Subtask #3:
score: 15
Accepted
Test #11:
score: 15
Accepted
time: 1ms
memory: 3820kb
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: 3892kb
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: 3596kb
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: 3588kb
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: 3632kb
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: 3652kb
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: 88ms
memory: 3852kb
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: 88ms
memory: 3592kb
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: 91ms
memory: 3596kb
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: 85ms
memory: 3592kb
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: 79ms
memory: 3836kb
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: 15
Accepted
Dependency #4:
100%
Accepted
Test #22:
score: 15
Accepted
time: 449ms
memory: 3664kb
input:
5 257 800 766 800 800 353 667 0 42 0
output:
18881369614
result:
ok 1 number(s): "18881369614"
Test #23:
score: 15
Accepted
time: 523ms
memory: 3624kb
input:
8 691 800 737 795 800 651 372 98 136 266 118 318 24 629 12 753
output:
8760058886
result:
ok 1 number(s): "8760058886"
Test #24:
score: 15
Accepted
time: 417ms
memory: 3692kb
input:
8 718 800 740 800 800 726 800 670 711 367 595 150 86 0 57 136
output:
3064355626
result:
ok 1 number(s): "3064355626"
Test #25:
score: 15
Accepted
time: 623ms
memory: 3656kb
input:
8 0 347 16 449 364 798 674 800 750 800 797 14 195 0 0 70
output:
23587042437
result:
ok 1 number(s): "23587042437"
Test #26:
score: 15
Accepted
time: 670ms
memory: 3596kb
input:
8 322 800 596 800 686 777 800 280 764 69 396 0 46 179 0 660
output:
23185884331
result:
ok 1 number(s): "23185884331"
Subtask #6:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #27:
score: 0
Time Limit Exceeded
input:
8 0 1150 314 2000 1101 2000 1617 1607 1778 551 738 0 607 10 0 1011
output:
577130875850