This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
#define INF 1000000005
#define LINF 1000000000000000005ll
#define MAXN 1000005
int n, h;
int x[MAXN], y[MAXN];
bool used[MAXN];
int ans;
struct frac {
ll a, b;
frac(): a(0), b(1) {}
frac(ll _a): a(_a), b(1) {}
frac(ll _a, ll _b): a(_a), b(_b) {
if (b == 0) {
a = LINF;
b = 1;
return;
}
ll g = __gcd(abs(a), abs(b));
a /= g;
b /= g;
if (b < 0) {
a *= -1;
b *= -1;
}
}
frac operator+(const frac &o) const {
return frac(a * o.b + b * o.a, b * o.b);
}
frac operator-(const frac &o) const {
return *this + (-o);
}
frac operator-() const {
return frac(-a, b);
}
bool operator<(const frac &o) const {
assert(b > 0 && o.b > 0);
return a * o.b < b * o.a;
}
bool operator>(const frac &o) const {
assert(b > 0 && o.b > 0);
return a * o.b > b * o.a;
}
bool operator==(const frac &o) const {
return a == o.a && b == o.b;
}
bool operator<=(const frac &o) const {
return *this == o || *this < o;
}
friend ostream& operator<<(ostream &os, const frac &o) {
return os << o.a << '/' << o.b;
}
};
struct Range {
frac l, r;
bool operator<(const Range &o) const {
return r < o.r;
}
};
frac l[MAXN], r[MAXN];
vector<Range> ranges;
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> h;
REP (i, 0, n) {
cin >> x[i] >> y[i];
}
REP (zz, 0, 2) {
vi pts;
REP (i, 1, n - 1) {
auto ori = [&] (int a, int b, int c) {
// b - a cross c - a
ll x1 = x[b] - x[a], y1 = y[b] - y[a],
x2 = x[c] - x[a], y2 = y[c] - y[a];
return x1 * y2 - x2 * y1;
};
while (pts.size() >= 2 && ori(pts[pts.size() - 2], pts[pts.size() - 1], i) > 0) {
pts.pop_back();
}
if (i % 2 == 0) {
int j = pts.back();
l[i] = frac(x[i]) - frac((ll) (x[i] - x[j]) * (h - y[i]), y[j] - y[i]);
}
pts.pb(i);
}
swap(l, r);
REP (i, 0, n) {
x[i] *= -1;
}
reverse(x, x + n);
reverse(y, y + n);
}
REP (i, 0, n) {
r[i] = -r[i];
}
reverse(r, r + n);
for (int i = 2; i < n - 1; i += 2) {
cerr << i << ": " << l[i] << ' ' << r[i] << '\n';
ranges.pb((Range) {l[i], r[i]});
}
sort(ALL(ranges));
REP (i, 0, ranges.size()) {
if (used[i]) continue;
frac x = ranges[i].r;
ans++;
REP (j, i, ranges.size()) {
if (ranges[j].l <= x && x <= ranges[j].r) {
used[j] = 1;
} else {
break;
}
}
}
cout << ans << '\n';
return 0;
}
/*
2: -6/1 2/3
4: -1/1 5/1
6: 2/1 8/1
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:13:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
141 | REP (i, 0, ranges.size()) {
| ~~~~~~~~~~~~~~~~~~~
Main.cpp:141:5: note: in expansion of macro 'REP'
141 | REP (i, 0, ranges.size()) {
| ^~~
Main.cpp:13:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
145 | REP (j, i, ranges.size()) {
| ~~~~~~~~~~~~~~~~~~~
Main.cpp:145:9: note: in expansion of macro 'REP'
145 | REP (j, i, ranges.size()) {
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |