QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#562833 | #8669. 正方形计数 | sha7dow | 0 | 1912ms | 3832kb | C++14 | 6.8kb | 2024-09-13 21:25:14 | 2024-09-13 21:25:15 |
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);
}
// 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;
}
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) {
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<int>;
using L = Line<int>;
void solve() {
int n;
cin >> n;
vector<P> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
reverse(p.begin(), p.end());
vector<L> l;
int ans = 0;
for (int x = 1; x <= 2000; x++) {
for (int y = 0; y <= 2000; y++) {
// if (x == 0 && y == 0) continue;
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, 2008);
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
Runtime Error
Test #1:
score: 0
Runtime Error
input:
4 131 603 131 1828 1919 1828 1919 603
output:
result:
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 1912ms
memory: 3832kb
input:
3 131 603 131 1828 1919 603
output:
-2084839837
result:
wrong answer 1st numbers differ - expected: '63739309181', found: '-2084839837'
Subtask #3:
score: 0
Time Limit Exceeded
Test #11:
score: 0
Time Limit Exceeded
input:
8 0 13 4 15 15 15 15 6 13 1 12 0 5 0 0 6
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%