QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#413483 | #8669. 正方形计数 | yaoxi_std | 100 ✓ | 2648ms | 3848kb | C++14 | 5.7kb | 2024-05-17 16:56:53 | 2024-05-17 16:56:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define debug(fmt, ...) \
fprintf(stderr, "[%d] " fmt "\n", __LINE__, ##__VA_ARGS__)
template <class _Tx, class _Ty>
inline void chkmin(_Tx& x, const _Ty& y) {
x = min<common_type_t<_Tx, _Ty>>(x, y);
}
template <class _Tx, class _Ty>
inline void chkmax(_Tx& x, const _Ty& y) {
x = max<common_type_t<_Tx, _Ty>>(x, y);
}
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
bool Mbe;
constexpr int N = 10, inf = 2e3 + 10;
int n, lmt;
ll div_f(ll n, ll d) {
if (d < 0) n = -n, d = -d;
return n >= 0 ? n / d : (n - d + 1) / d;
}
ll div_c(ll n, ll d) {
if (d < 0) n = -n, d = -d;
return n >= 0 ? (n + d - 1) / d : n / d;
}
struct point {
int x, y;
point& operator+=(const point& rhs) {
return x += rhs.x, y += rhs.y, *this;
}
point& operator-=(const point& rhs) {
return x -= rhs.x, y -= rhs.y, *this;
}
point& operator*=(int k) {
return x *= k, y *= k, *this;
}
friend point operator+(point lhs, const point& rhs) {
return lhs += rhs;
}
friend point operator-(point lhs, const point& rhs) {
return lhs -= rhs;
}
friend point operator*(point lhs, int rhs) {
return lhs *= rhs;
}
friend point operator*(int lhs, point rhs) {
return rhs *= lhs;
}
} pnt[N];
ll det(const point& u, const point& v) {
return (ll)u.x * v.y - (ll)u.y * v.x;
}
ll dot(const point& u, const point& v) {
return (ll)u.x * v.x + (ll)u.y * v.y;
}
int quadrant(const point& u) {
return (u.y < 0) << 1 | ((u.x < 0) ^ (u.y < 0));
}
struct line {
point p, d;
};
vector<line> half_plane_inter(vector<line> a) {
a.push_back({{-inf, -inf}, {1, 0}});
a.push_back({{inf, -inf}, {0, 1}});
a.push_back({{inf, inf}, {-1, 0}});
a.push_back({{-inf, inf}, {0, -1}});
sort(a.begin(), a.end(), [](const line& u, const line& v) {
int qu = quadrant(u.d), qv = quadrant(v.d);
if (qu != qv) return qu < qv;
ll d = det(u.d, v.d);
return d ? d > 0 : det(v.p - u.p, u.d) > 0;
});
a.erase(unique(a.begin(), a.end(), [](const line& u, const line& v) {
return quadrant(u.d) == quadrant(v.d) && det(u.d, v.d) == 0;
}), a.end());
auto above = [](const line& u, const line& v, const line& w) {
return det(v.d, u.p - v.p) * det(u.d, w.d) >
det(w.d, u.p - w.p) * det(u.d, v.d);
};
auto above_eq = [](const line& u, const line& v, const line& w) {
return det(v.d, u.p - v.p) * det(u.d, w.d) >=
det(w.d, u.p - w.p) * det(u.d, v.d);
};
int hd = 0, tl = -1;
vector<line> que(a.size());
for (auto it : a) {
while (hd < tl && above(que[tl - 1], que[tl], it)) --tl;
while (hd < tl && above(it, que[hd], que[hd + 1])) ++hd;
que[++tl] = it;
}
while (hd < tl && above(que[tl - 1], que[tl], que[hd])) --tl;
if (tl - hd + 1 <= 2) return {};
if (det(que[hd].d, que[hd + 1].d) <= 0) return {};
for (auto it : a) if (!above_eq(que[hd], it, que[hd + 1])) return {};
return {que.begin() + hd, que.begin() + tl + 1};
}
int euclid(int n, int a, int b, int c) {
// debug("%d %d %d %d", n, a, b, c);
int s0 = n + 1, s1 = n * (n + 1) / 2;
int ac = a / c, bc = b / c, m = (a * n + b) / c;
if (!a) return s0 * bc;
if (a >= c || b >= c)
return s1 * ac + s0 * bc + euclid(n, a % c, b % c, c);
return n * m - euclid(m - 1, c, c - b - 1, a);
}
int floor_sum(int p, int q, int r, int n) {
if (q < 0) p = -p, q = -q, r = -r;
if (p < 0) r += n * p, p = -p;
int ans = 0;
ans += (n + 1) * (r / q), r %= q;
if (r < 0) ans -= n + 1, r += q;
return ans + euclid(n, p, r, q);
}
int floor_sum(int p, int q, int r, int ql, int qr) {
if (ql > qr) return 0;
return floor_sum(p, q, r + ql * p, qr - ql);
}
int calc_below(const line& u, int l, int r) {
if (l > r) return 0;
return (r - l + 1) * u.p.y + floor_sum(u.d.y, u.d.x, -u.p.x * u.d.y - 1, l, r);
}
int calc_below_eq(const line& u, int l, int r) {
if (l > r) return 0;
return (r - l + 1) * u.p.y + floor_sum(u.d.y, u.d.x, -u.p.x * u.d.y, l, r);
}
int calc(const vector<line>& ln) {
if (ln.empty()) return 0;
int m = ln.size(), ans = 0;
for (int i = 0; i < m; ++i) {
line u = ln[(i + m - 1) % m], v = ln[i], w = ln[(i + 1) % m];
ll dnu = det(u.d, v.d), upu = u.p.x * dnu + det(v.p - u.p, v.d) * u.d.x;
ll dnw = det(v.d, w.d), upw = v.p.x * dnw + det(w.p - v.p, w.d) * v.d.x;
if (v.d.x > 0) {
ll lft = div_c(upu, dnu);
ll rht = w.d.x > 0 ? div_c(upw, dnw) - 1 : div_f(upw, dnw);
ans -= calc_below(v, lft, rht);
} else if (v.d.x < 0) {
ll lft = div_c(upw, dnw);
ll rht = u.d.x < 0 ? div_c(upu, dnu) - 1 : div_f(upu, dnu);
ans += calc_below_eq(v, lft, rht);
}
}
return ans;
}
bool Med;
int main() {
// debug("Mem: %.4lfMB.", fabs(&Med - &Mbe) / 1048576);
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> pnt[i].x >> pnt[i].y;
chkmax(lmt, pnt[i].x);
chkmax(lmt, pnt[i].y);
}
ll ans = 0;
reverse(pnt + 1, pnt + n);
for (int dx = 1; dx <= lmt; ++dx) {
for (int dy = 0; dx + dy <= lmt; ++dy) {
vector<point> vec;
vec.push_back({0, 0});
vec.push_back({dx, dy});
vec.push_back({dx - dy, dx + dy});
vec.push_back({-dy, dx});
vector<line> ln;
for (int i = 0; i < n; ++i) {
int j = (i + 1) % n;
for (auto it : vec) {
ln.push_back({pnt[i] - it, pnt[j] - pnt[i]});
}
}
ans += calc(half_plane_inter(ln));
}
}
cout << ans << '\n';
return 0;
}
/*
g++ -std=c++14 -O2 -o B B.cpp -Wall -Wextra
-Wshadow -fsanitize=address,undefined -g
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 857ms
memory: 3556kb
input:
4 131 603 131 1828 1919 1828 1919 603
output:
361182910200
result:
ok 1 number(s): "361182910200"
Test #2:
score: 0
Accepted
time: 196ms
memory: 3836kb
input:
4 239 211 239 962 261 962 261 211
output:
1498772
result:
ok 1 number(s): "1498772"
Test #3:
score: 0
Accepted
time: 1059ms
memory: 3840kb
input:
4 0 0 0 2000 2000 2000 2000 0
output:
1336001667000
result:
ok 1 number(s): "1336001667000"
Test #4:
score: 0
Accepted
time: 126ms
memory: 3628kb
input:
4 36 771 36 786 672 786 672 771
output:
427720
result:
ok 1 number(s): "427720"
Test #5:
score: 0
Accepted
time: 11ms
memory: 3488kb
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: 747ms
memory: 3552kb
input:
3 131 603 131 1828 1919 603
output:
63739309181
result:
ok 1 number(s): "63739309181"
Test #7:
score: 0
Accepted
time: 171ms
memory: 3604kb
input:
3 239 211 239 962 261 211
output:
353073
result:
ok 1 number(s): "353073"
Test #8:
score: 0
Accepted
time: 801ms
memory: 3600kb
input:
3 0 0 0 2000 2000 0
output:
222889277611
result:
ok 1 number(s): "222889277611"
Test #9:
score: 0
Accepted
time: 115ms
memory: 3820kb
input:
3 36 771 36 786 672 771
output:
98847
result:
ok 1 number(s): "98847"
Test #10:
score: 0
Accepted
time: 2ms
memory: 3492kb
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: 0ms
memory: 3556kb
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: 0
Accepted
time: 0ms
memory: 3532kb
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: 0
Accepted
time: 0ms
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: 0
Accepted
time: 1ms
memory: 3532kb
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: 0
Accepted
time: 0ms
memory: 3628kb
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: 0
Accepted
time: 0ms
memory: 3604kb
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: 53ms
memory: 3552kb
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: 0
Accepted
time: 55ms
memory: 3628kb
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: 0
Accepted
time: 51ms
memory: 3828kb
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: 0
Accepted
time: 49ms
memory: 3624kb
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: 0
Accepted
time: 46ms
memory: 3604kb
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: 261ms
memory: 3616kb
input:
5 257 800 766 800 800 353 667 0 42 0
output:
18881369614
result:
ok 1 number(s): "18881369614"
Test #23:
score: 0
Accepted
time: 339ms
memory: 3608kb
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: 0
Accepted
time: 286ms
memory: 3540kb
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: 0
Accepted
time: 360ms
memory: 3604kb
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: 0
Accepted
time: 390ms
memory: 3612kb
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: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #27:
score: 15
Accepted
time: 2418ms
memory: 3824kb
input:
8 0 1150 314 2000 1101 2000 1617 1607 1778 551 738 0 607 10 0 1011
output:
577130875850
result:
ok 1 number(s): "577130875850"
Test #28:
score: 0
Accepted
time: 2575ms
memory: 3604kb
input:
8 0 1841 1526 2000 1981 1680 1968 678 1893 26 973 0 616 315 524 434
output:
735496008519
result:
ok 1 number(s): "735496008519"
Test #29:
score: 0
Accepted
time: 1951ms
memory: 3820kb
input:
6 0 258 10 2000 1730 2000 2000 1510 1973 0 0 129
output:
1203935109430
result:
ok 1 number(s): "1203935109430"
Test #30:
score: 0
Accepted
time: 2112ms
memory: 3792kb
input:
7 200 2000 1686 2000 1951 1878 2000 863 1422 0 21 0 0 1015
output:
1100462975231
result:
ok 1 number(s): "1100462975231"
Test #31:
score: 0
Accepted
time: 2648ms
memory: 3832kb
input:
8 701 2000 1449 2000 1847 1928 2000 1496 1987 668 1588 108 263 0 0 1985
output:
997591862206
result:
ok 1 number(s): "997591862206"
Test #32:
score: 0
Accepted
time: 2424ms
memory: 3492kb
input:
8 15 2000 1235 2000 1545 1886 1970 1526 1828 427 1238 97 372 0 0 1786
output:
816089046494
result:
ok 1 number(s): "816089046494"
Test #33:
score: 0
Accepted
time: 1960ms
memory: 3848kb
input:
7 0 1685 1331 2000 2000 1941 2000 1310 1757 631 21 113 0 575
output:
633230324466
result:
ok 1 number(s): "633230324466"
Test #34:
score: 0
Accepted
time: 1864ms
memory: 3472kb
input:
8 0 650 0 1350 650 2000 1350 2000 2000 1350 2000 650 1350 0 650 0
output:
900037062925
result:
ok 1 number(s): "900037062925"