QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#413453#8669. 正方形计数yaoxi_std75 3341ms3804kbC++145.8kb2024-05-17 16:20:282024-05-17 16:20:29

Judging History

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

  • [2024-05-17 16:20:29]
  • 评测
  • 测评结果:75
  • 用时:3341ms
  • 内存:3804kb
  • [2024-05-17 16:20:28]
  • 提交

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 calc_b(const vector<line>& ln) {
  if (ln.empty()) return 0;
  int ans = 0;
  for (int i = 0; i <= lmt; ++i) {
    for (int j = 0; j <= lmt; ++j) {
      bool ok = 1;
      point p = {i, j};
      for (auto it : ln) ok &= det(it.d, p - it.p) >= 0;
      ans += ok;
      if (ok) debug("x = %d, y = %d", i, j);
    }
  }
  return ans;
}
int floor_sum(int p, int q, int r, int n) {
  int ans = 0;
  for (int i = 1; i <= n; ++i) ans += div_f(p * i + r, q);
  return ans;
}
int calc_below(const line& u, int l, int r) {
  if (l > r) return 0;
  int ans = 0;
  for (int i = l; i <= r; ++i)
    ans += u.p.y + div_c((ll)(i - u.p.x) * u.d.y, u.d.x) - 1;
  return ans;
}
int calc_below_eq(const line& u, int l, int r) {
  if (l > r) return 0;
  int ans = 0;
  for (int i = l; i <= r; ++i)
    ans += u.p.y + div_f((ll)(i - u.p.x) * u.d.y, u.d.x);
  return ans;
}
int calc(const vector<line>& ln) {
  if (ln.empty()) return 0;
  ll upmx = -inf, dnmx = 1;
  int m = ln.size(), ans = 0;
  for (int i = 0; i < m; ++i) {
    line u = ln[i], v = ln[(i + 1) % m];
    ll dnt = det(u.d, v.d), upt = u.p.x * dnt + det(v.p - u.p, v.d) * u.d.x;
    if (dnt < 0) upt = -upt, dnt = -dnt;
    if (upt * dnmx > upmx * dnt) upmx = upt, dnmx = dnt;
  }
  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: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

4
131 603
131 1828
1919 1828
1919 603

output:


result:


Subtask #2:

score: 25
Accepted

Test #6:

score: 25
Accepted
time: 1891ms
memory: 3584kb

input:

3
131 603
131 1828
1919 603

output:

63739309181

result:

ok 1 number(s): "63739309181"

Test #7:

score: 25
Accepted
time: 172ms
memory: 3580kb

input:

3
239 211
239 962
261 211

output:

353073

result:

ok 1 number(s): "353073"

Test #8:

score: 25
Accepted
time: 3341ms
memory: 3532kb

input:

3
0 0
0 2000
2000 0

output:

222889277611

result:

ok 1 number(s): "222889277611"

Test #9:

score: 25
Accepted
time: 116ms
memory: 3524kb

input:

3
36 771
36 786
672 771

output:

98847

result:

ok 1 number(s): "98847"

Test #10:

score: 25
Accepted
time: 3ms
memory: 3580kb

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

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

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

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

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

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

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: 65ms
memory: 3584kb

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: 64ms
memory: 3524kb

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: 65ms
memory: 3584kb

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: 67ms
memory: 3480kb

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: 62ms
memory: 3584kb

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: 525ms
memory: 3528kb

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: 548ms
memory: 3532kb

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: 393ms
memory: 3584kb

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: 725ms
memory: 3752kb

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: 725ms
memory: 3804kb

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
Skipped

Dependency #1:

0%