text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int maxN = 1005; struct dot { long long x, y; dot() {} dot(long long x1, long long y1) { x = x1; y = y1; } void read() { cin >> x >> y; } long long operator%(const dot &o) { return x * o.y - y * o.x; } dot operator-(const dot &o) { return dot(x - o.x, y - o.y); } } p[maxN], lx, rx; int n; void init() { scanf("%d", &n); for (int i = 1; i <= n; ++i) p[i].read(); p[n + 1] = p[1]; } long long cross(dot a, dot b, dot c) { return (b - a) % (c - a); } void work() { int y; lx.x = p[1].x; rx.x = p[2].x; if (lx.x > rx.x) swap(lx, rx); lx.y = rx.y = p[1].y; for (int i = 1; i <= n; ++i) { while (lx.x <= rx.x && cross(lx, p[i], p[i + 1]) > 0) ++lx.x; while (lx.x <= rx.x && cross(rx, p[i], p[i + 1]) > 0) --rx.x; } if (lx.x > rx.x) puts("0"); else printf("%d\n", rx.x - lx.x + 1); } int main() { init(); work(); return 0; }
#include <bits/stdc++.h> long long N, i, L, R, A, B, C; long long X[1005], Y[1005]; double W; int main() { scanf("%I64d", &N); for (i = 1; i <= N; ++i) scanf("%I64d%I64d", &X[i], &Y[i]); if (Y[3] < Y[1]) L = X[1], R = X[2]; if (Y[3] > Y[1]) L = X[2], R = X[1]; for (i = 3; i < N; ++i) { if (Y[i] == Y[i + 1]) { if (Y[3] < Y[1] && X[i] < X[i + 1]) { printf("0\n"); return 0; } if (Y[3] > Y[1] && X[i] > X[i + 1]) { printf("0\n"); return 0; } continue; } else { A = Y[i] - Y[i + 1], B = X[i + 1] - X[i]; C = X[i] * A + Y[i] * B; W = (C - Y[1] * B) * 1.0 / A; } if (Y[i] < Y[i + 1]) { if (W > 0) W += 0.999999; if ((int)(W) > L) L = (int)(W); } if (Y[i] > Y[i + 1]) { if (W < 0) W -= 0.999999; if ((int)(W) < R) R = (int)(W); } } if (R - L + 1 > 0) printf("%I64d\n", R - L + 1); else printf("0\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long first[1111], second[1111]; int main() { cin >> n; for (int(i) = 0; (i) < (n); ++(i)) cin >> first[i] >> second[i]; long long low = first[0]; long long high = first[1]; if (high < low) swap(high, low); for (int(i) = 0; (i) < (n); ++(i)) { long long a = second[(i + 1) % n] - second[i]; long long b = first[i] - first[(i + 1) % n]; long long c = first[(i + 1) % n] * second[i] - first[i] * second[(i + 1) % n]; if (!a) { if (b * second[0] < -c) { cout << 0 << endl; return 0; } } if (a > 0) low = max(low, (long long)floor((double)(-b * second[0] - c) / a + 1 - 1e-8)); else if (a < 0) high = min(high, (long long)floor((double)(-b * second[0] - c) / a + 1e-8)); } cout << max(high - low + 1, 0LL) << endl; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double inf = 1e+13; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } struct Pnt { Pnt() : x(0), y(0) {} Pnt(double x, double y) : x(x), y(y) {} Pnt operator+(Pnt const& p) const { return Pnt(x + p.x, y + p.y); } Pnt operator-(Pnt const& p) const { return Pnt(x - p.x, y - p.y); } Pnt operator*(double a) const { return Pnt(x * a, y * a); } double operator*(Pnt p) const { return x * p.x + y * p.y; } double operator^(Pnt p) const { return x * p.y - y * p.x; } bool operator<=(Pnt p) const { if (abs(x - p.x) < eps) return y < p.y + eps; return x < p.x + eps; } void read() { cin >> x >> y; } double x, y; }; pair<double, double> raysIntersect(Pnt p1, Pnt d1, Pnt p2, Pnt d2) { double cp = d1 ^ d2; if (cp == 0) { double cp2 = d1 ^ (p2 - p1); if (cp2 == 0) return make_pair(-inf, +inf); if (cp2 > 0) return (d1 * d2 > 0) ? make_pair(-inf, +inf) : make_pair(+inf, +inf); return (d1 * d2 > 0) ? make_pair(-inf, -inf) : make_pair(-inf, +inf); } double t1 = ((p2 - p1) ^ d2) / cp; if (cp > 0) return make_pair(t1, +inf); else return make_pair(-inf, t1); } bool segsIntersect(pair<double, double> a, pair<double, double> b, pair<double, double>& res) { double l = max(a.first, b.first); double r = min(a.second, b.second); if (l > r + 1e-13) return false; res = pair<double, double>(l, r); return true; } vector<Pnt> pts; void solve(int n) { pts.resize(n + 1); for (int(i) = 0; (i) < (n); ++(i)) pts[i].read(); pts[n] = pts[0]; Pnt o = pts[0], dir = pts[1] - pts[0]; pair<double, double> seg(0, 1); for (int(i) = 0; (i) < (n); ++(i)) { pair<double, double> seg1 = raysIntersect(o, dir, pts[i], pts[i] - pts[(i - 1 + n) % n]); pair<double, double> seg2 = raysIntersect(o, dir, pts[i], pts[(i + 1) % n] - pts[i]); if (!segsIntersect(seg1, seg, seg) || !segsIntersect(seg2, seg, seg)) { printf("0\n"); return; } } if (seg.first > seg.second) swap(seg.first, seg.second); Pnt to = pts[1] - pts[0]; Pnt a = to * seg.first; Pnt b = to * seg.second; int x = (int)(to.x); int y = (int)(to.y); int d = gcd(abs(x), abs(y)); int dx = 0, dy = 0; if (d != 0) dx = x / d, dy = y / d; int ans = 0; for (int i = 0;; i++) { Pnt p(i * dx, i * dy); if ((a <= p && p <= b) || (b <= p && p <= a)) ans++; if (i * dx == x && i * dy == y) break; } printf("%d\n", ans); } int main() { int n; while (cin >> n) { solve(n); } return 0; }
#include <bits/stdc++.h> void Get(int &T) { char C; bool F = 0; for (; C = getchar(), C < '0' || C > '9';) if (C == '-') F = 1; for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0') ; F && (T = -T); } void Get(long long &T) { char C; bool F = 0; for (; C = getchar(), C < '0' || C > '9';) if (C == '-') F = 1; for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0') ; F && (T = -T); } int N; long long X[1005]; long long Y[1005]; long long Cp(int P0, int P1, int P2) { return (X[P1] - X[P0]) * (Y[P2] - Y[P0]) - (X[P2] - X[P0]) * (Y[P1] - Y[P0]); } void Init() { Get(N); for (int i = 1; i <= N; i++) { Get(X[i]); Get(Y[i]); } X[0] = X[N]; Y[0] = Y[N]; X[N + 1] = X[1]; X[N + 2] = X[2]; Y[N + 1] = Y[N + 2] = Y[1]; if (X[N + 1] > X[N + 2]) std::swap(X[N + 1], X[N + 2]); } void Cut(int A, int B) { if (A == 1 && B == 2) return; for (; X[N + 1] <= X[N + 2]; X[N + 1]++) if (Cp(N + 1, A, B) >= 0) break; for (; X[N + 1] <= X[N + 2]; X[N + 2]--) if (Cp(N + 2, A, B) >= 0) break; } void Work() { long long S = 0; for (int i = 1; i <= N; i++) S += Cp(0, i - 1, i); if (S < 0) { for (int i = 1, j = 2; i < j; i++, j--) { std::swap(X[i], X[j]); std::swap(Y[i], Y[j]); } for (int i = 3, j = N; i < j; i++, j--) { std::swap(X[i], X[j]); std::swap(Y[i], Y[j]); } } X[0] = X[N]; Y[0] = Y[N]; for (int i = 1; i <= N; i++) Cut(i - 1, i); } void Output() { printf("%I64d\n", X[N + 2] - X[N + 1] + 1); } int main() { Init(); Work(); Output(); return 0; }
#include <bits/stdc++.h> using namespace std; int px[1010], py[1010]; int L, R, H; int n; long long det(int x1, int y1, int x2, int y2, int x3, int y3) { return (long long)(x2 - x1) * (y3 - y1) - (long long)(x3 - x1) * (y2 - y1); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &px[i], &py[i]); L = px[0]; R = px[1]; H = py[0]; if (L > R) swap(L, R); for (int i = 1; i < n; i++) { int x1 = px[i], x2 = px[(i + 1) % n], y1 = py[i], y2 = py[(i + 1) % n]; if (det(x1, y1, x2, y2, L, H) > 0 && det(x1, y1, x2, y2, R, H) > 0) { printf("0\n"); return 0; } if (y1 == y2) continue; if (det(x1, y1, x2, y2, L, H) <= 0 && det(x1, y1, x2, y2, R, H) <= 0) continue; double v = (double)(x2 - x1) * (H - y1) / (y2 - y1) + x1; if (det(x1, y1, x2, y2, L, H) > 0) { L = (int)v; if (det(x1, y1, x2, y2, L, H) > 0) L++; } else { R = (int)v; if (det(x1, y1, x2, y2, R + 1, H) <= 0) R++; } } printf("%d\n", R - L + 1); }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double inf = 1e20; const double pi = acos(-1.0); const int maxp = 1611; int dblcmp(double d) { if (fabs(d) < eps) return 0; return d > eps ? 1 : -1; } inline double sqr(double x) { return x * x; } struct point { double x, y; point() {} point(double _x, double _y) : x(_x), y(_y){}; void input() { scanf("%lf%lf", &x, &y); } void output() { printf("%.2f %.2f\n", x, y); } bool operator==(point a) { return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0; } bool operator<(point a) const { return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) < 0 : x < a.x; } double len() { return sqrt(len2()); } double len2() { return x * x + y * y; } double distance(point p) { return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)); } point add(point p) { return point(x + p.x, y + p.y); } point sub(point p) { return point(x - p.x, y - p.y); } point mul(double b) { return point(x * b, y * b); } point div(double b) { return point(x / b, y / b); } double dot(point p) { return x * p.x + y * p.y; } double det(point p) { return x * p.y - y * p.x; } double rad(point a, point b) { point p = *this; return fabs(atan2(fabs(a.sub(p).det(b.sub(p))), a.sub(p).dot(b.sub(p)))); } point trunc(double r) { r /= len(); return point(x * r, y * r); } point rotleft() { return point(-y, x); } point rotright() { return point(y, -x); } point rotate(point p, double angle) { point v = this->sub(p); double c = cos(angle), s = sin(angle); return point(p.x + v.x * c - v.y * s, p.y + v.x * s + v.y * c); } }; struct line { point a, b; line() {} line(point _a, point _b) { a = _a; b = _b; } bool operator==(line v) { return (a == v.a) && (b == v.b); } line(point p, double angle) { a = p; if (dblcmp(angle - pi / 2) == 0) { b = a.add(point(0, 1)); } else { b = a.add(point(1, tan(angle))); } } line(double _a, double _b, double _c) { if (dblcmp(_a) == 0) { a = point(0, -_c / _b); b = point(1, -_c / _b); } else if (dblcmp(_b) == 0) { a = point(-_c / _a, 0); b = point(-_c / _a, 1); } else { a = point(0, -_c / _b); b = point(1, (-_c - _a) / _b); } } void input() { a.input(); b.input(); } void adjust() { if (b < a) swap(a, b); } double length() { return a.distance(b); } int relation(point p) { int c = dblcmp(p.sub(a).det(b.sub(a))); if (c < 0) return 1; if (c > 0) return 2; return 3; } bool pointonseg(point p) { return dblcmp(p.sub(a).det(b.sub(a))) == 0 && dblcmp(p.sub(a).dot(p.sub(b))) <= 0; } bool parallel(line v) { return dblcmp(b.sub(a).det(v.b.sub(v.a))) == 0; } int segcrossseg(line v) { int d1 = dblcmp(b.sub(a).det(v.a.sub(a))); int d2 = dblcmp(b.sub(a).det(v.b.sub(a))); int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a))); int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a))); if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 2; return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0 || d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0 || d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0 || d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0); } int linecrossseg(line v) { int d1 = dblcmp(b.sub(a).det(v.a.sub(a))); int d2 = dblcmp(b.sub(a).det(v.b.sub(a))); if ((d1 ^ d2) == -2) return 2; return (d1 == 0 || d2 == 0); } int linecrossline(line v) { if ((*this).parallel(v)) { return v.relation(a) == 3; } return 2; } point crosspoint(line v) { double a1 = v.b.sub(v.a).det(a.sub(v.a)); double a2 = v.b.sub(v.a).det(b.sub(v.a)); return point((a.x * a2 - b.x * a1) / (a2 - a1), (a.y * a2 - b.y * a1) / (a2 - a1)); } double dispointtoline(point p) { return fabs(p.sub(a).det(b.sub(a))) / length(); } double dispointtoseg(point p) { if (dblcmp(p.sub(b).dot(a.sub(b))) < 0 || dblcmp(p.sub(a).dot(b.sub(a))) < 0) { return min(p.distance(a), p.distance(b)); } return dispointtoline(p); } point lineprog(point p) { return a.add(b.sub(a).trunc(b.sub(a).dot(p.sub(a)) / b.distance(a))); } point symmetrypoint(point p) { point q = lineprog(p); return point(2 * q.x - p.x, 2 * q.y - p.y); } }; struct polygon { int n; point p[maxp]; line l[maxp]; void input() { for (int i = 0; i < n; i++) { p[i].input(); } } void add(point q) { p[n++] = q; } void getline() { for (int i = 0; i < n; i++) { l[i] = line(p[i], p[(i + 1) % n]); } } struct cmp { point p; cmp(const point &p0) { p = p0; } bool operator()(const point &aa, const point &bb) { point a = aa, b = bb; int d = dblcmp(a.sub(p).det(b.sub(p))); if (d == 0) { return dblcmp(a.distance(p) - b.distance(p)) < 0; } return d > 0; } }; void norm() { point mi = p[0]; for (int i = 1; i < n; i++) mi = min(mi, p[i]); sort(p, p + n, cmp(mi)); } void getconvex(polygon &convex) { int i, j, k; sort(p, p + n); convex.n = n; for (i = 0; i < min(n, 2); i++) { convex.p[i] = p[i]; } if (n <= 2) return; int &top = convex.n; top = 1; for (i = 2; i < n; i++) { while (top && convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0) top--; convex.p[++top] = p[i]; } int temp = top; convex.p[++top] = p[n - 2]; for (i = n - 3; i >= 0; i--) { while (top != temp && convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0) top--; convex.p[++top] = p[i]; } } bool isconvex() { bool s[3]; memset(s, 0, sizeof(s)); int i, j, k; for (i = 0; i < n; i++) { j = (i + 1) % n; k = (j + 1) % n; s[dblcmp(p[j].sub(p[i]).det(p[k].sub(p[i]))) + 1] = 1; if (s[0] && s[2]) return 0; } return 1; } int relationpoint(point q) { int i, j; for (i = 0; i < n; i++) { if (p[i] == q) return 3; } getline(); for (i = 0; i < n; i++) { if (l[i].pointonseg(q)) return 2; } int cnt = 0; for (i = 0; i < n; i++) { j = (i + 1) % n; int k = dblcmp(q.sub(p[j]).det(p[i].sub(p[j]))); int u = dblcmp(p[i].y - q.y); int v = dblcmp(p[j].y - q.y); if (k > 0 && u < 0 && v >= 0) cnt++; if (k < 0 && v < 0 && u >= 0) cnt--; } return cnt != 0; } int relationline(line u) { int i, j, k = 0; getline(); for (i = 0; i < n; i++) { if (l[i].segcrossseg(u) == 2) return 1; if (l[i].segcrossseg(u) == 1) k = 1; } if (!k) return 0; vector<point> vp; for (i = 0; i < n; i++) { if (l[i].segcrossseg(u)) { if (l[i].parallel(u)) { vp.push_back(u.a); vp.push_back(u.b); vp.push_back(l[i].a); vp.push_back(l[i].b); continue; } vp.push_back(l[i].crosspoint(u)); } } sort(vp.begin(), vp.end()); int sz = vp.size(); for (i = 0; i < sz - 1; i++) { point mid = vp[i].add(vp[i + 1]).div(2); if (relationpoint(mid) == 1) return 1; } return 2; } void convexcut(line u, polygon &po) { int i, j, k; int &top = po.n; top = 0; for (i = 0; i < n; i++) { int d1 = dblcmp(p[i].sub(u.a).det(u.b.sub(u.a))); int d2 = dblcmp(p[(i + 1) % n].sub(u.a).det(u.b.sub(u.a))); if (d1 >= 0) po.p[top++] = p[i]; if (d1 * d2 < 0) po.p[top++] = u.crosspoint(line(p[i], p[(i + 1) % n])); } } double getcircumference() { double sum = 0; int i; for (i = 0; i < n; i++) { sum += p[i].distance(p[(i + 1) % n]); } return sum; } double getarea() { double sum = 0; int i; for (i = 1; i < n - 1; i++) { sum += p[i].sub(p[0]).det(p[(i + 1) % n].sub(p[0])); } if (dblcmp(sum) < 0) { reverse(p, p + n); } return fabs(sum) / 2; } point getbarycentre() { point ret(0, 0); double area = 0; int i; for (i = 1; i < n - 1; i++) { double tmp = p[i].sub(p[0]).det(p[i + 1].sub(p[0])); if (dblcmp(tmp) == 0) continue; area += tmp; ret.x += (p[0].x + p[i].x + p[i + 1].x) / 3 * tmp; ret.y += (p[0].y + p[i].y + p[i + 1].y) / 3 * tmp; } if (dblcmp(area)) ret = ret.div(area); return ret; } double areaintersection(polygon po) {} double areaunion(polygon po) { return getarea() + po.getarea() - areaintersection(po); } }; struct circle { point p; double r; circle() {} circle(point _p, double _r) : p(_p), r(_r){}; circle(double x, double y, double _r) : p(point(x, y)), r(_r){}; circle(point a, point b, point c) { p = line(a.add(b).div(2), a.add(b).div(2).add(b.sub(a).rotleft())) .crosspoint( line(c.add(b).div(2), c.add(b).div(2).add(b.sub(c).rotleft()))); r = p.distance(a); } void input() { p.input(); scanf("%lf", &r); } bool operator==(circle v) { return ((p == v.p) && dblcmp(r - v.r) == 0); } double area() { return pi * sqr(r); } double circumference() { return 2 * pi * r; } int relation(point b) { double dst = b.distance(p); if (dblcmp(dst - r) < 0) return 2; if (dblcmp(dst - r) == 0) return 1; return 0; } int crossseg(line v) { return dblcmp(v.dispointtoseg(p) - r) <= 0; } int getcircle(point a, point b, double r, circle &c1, circle &c2) { circle x(a, r), y(b, r); int t = x.pointcrosscircle(y, c1.p, c2.p); if (!t) return 0; c1.r = c2.r = r; return t; } int pointcrossline(line v, point &p1, point &p2) { if (!(*this).crossseg(v)) return 0; point a = v.lineprog(p); double d = v.dispointtoline(p); d = sqrt(r * r - d * d); if (dblcmp(d) == 0) { p1 = a; p2 = a; return 1; } p1 = a.sub(v.b.sub(v.a).trunc(d)); p2 = a.add(v.b.sub(v.a).trunc(d)); return 2; } int relationcircle(circle v) { double d = p.distance(v.p); if (dblcmp(d - r - v.r) > 0) return 5; if (dblcmp(d - r - v.r) == 0) return 4; double l = fabs(r - v.r); if (dblcmp(d - r - v.r) < 0 && dblcmp(d - l) > 0) return 3; if (dblcmp(d - l) == 0) return 2; if (dblcmp(d - l) < 0) return 1; } int pointcrosscircle(circle v, point &p1, point &p2) { int rel = relationcircle(v); if (rel == 1 || rel == 5) return 0; if (rel == 2 || rel == 4) { p1 = p.add(v.p.sub(p).trunc(r)); p2 = p.add(v.p.sub(p).trunc(r)); return 1; } double d = p.distance(v.p); double l = (d + (sqr(r) - sqr(v.r)) / d) / 2; double h = sqrt(sqr(r) - sqr(l)); p1 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotleft().trunc(h))); p2 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotright().trunc(h))); return 2; } void tangentline(point q, line &u, line &v) { double d = p.distance(q); double l = sqr(r) / d; double h = sqrt(sqr(r) - sqr(l)); u = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotleft().trunc(h)))); v = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotright().trunc(h)))); } double areacircle(circle v) { int rel = relationcircle(v); if (rel >= 4) return 0.0; if (rel <= 2) return min(area(), v.area()); double d = p.distance(v.p); double hf = (r + v.r + d) / 2.0; double ss = 2 * sqrt(hf * (hf - r) * (hf - v.r) * (hf - d)); double a1 = acos((r * r + d * d - v.r * v.r) / 2.0 * r * d); a1 = a1 * r * r; double a2 = acos((v.r * v.r + d * d - r * r) / 2.0 * v.r * d); a2 = a2 * v.r * v.r; return a1 + a2 - ss; } double areatriangle(point a, point b) { point p1, p2; line l(a, b); int t = pointcrossline(l, p1, p2); double angle; if (t <= 1) { return fabs(area() * p.rad(a, b) / (2 * pi)); } bool b1 = l.pointonseg(p1); bool b2 = l.pointonseg(p2); if (b1 && b2) { return fabs(area() * p.rad(a, p1) / (2 * pi) + area() * p.rad(p2, b) / (2 * pi) + fabs(p1.sub(p).det(p2.sub(p))) / 2.0); } if (b1) { return fabs(area() * p.rad(a, p1) / (2 * pi) + fabs(p1.sub(p).det(b.sub(p)) / 2.0)); } if (b2) { return fabs(area() * p.rad(p2, b) / (2 * pi) + fabs(a.sub(p).det(p2.sub(p)) / 2.0)); } return fabs(a.sub(p).det(b.sub(p)) / 2.0); } double areapologon(polygon pl) { int i, j, k, l, m; double ans = 0; for (i = 0; i < pl.n; i++) { int j = (i + 1) % pl.n; if (dblcmp(pl.p[j].sub(p).det(pl.p[i].sub(p))) >= 0) { ans += areatriangle(pl.p[i], pl.p[j]); } else { ans -= areatriangle(pl.p[i], pl.p[j]); } } return fabs(ans); } }; const int maxn = 500; struct circles { circle c[maxn]; int n; circles() {} void add(circle cc) { c[n++] = cc; } void init_or() { bool v[maxn] = {0}; int i, j, k = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j) continue; if (c[i].relationcircle(c[j]) == 1) v[i] = 1; } } for (i = 0; i < n; i++) { if (!v[i]) c[k++] = c[i]; } n = k; } void init_and() { bool v[maxn] = {0}; int i, j, k = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j) continue; if (c[j].relationcircle(c[i]) == 1) v[i] = 1; } } for (i = 0; i < n; i++) { if (!v[i]) c[k++] = c[i]; } n = k; } }; struct halfplane : public line { double angle; halfplane() {} halfplane(point _a, point _b) { a = _a; b = _b; } void calcangle() { angle = atan2(b.y - a.y, b.x - a.x); } bool operator<(const halfplane &b) const { return angle < b.angle; } }; struct halfplanes { int n; halfplane hp[maxp]; point p[maxp]; int que[maxp]; int st, ed; void push(halfplane tmp) { hp[n++] = tmp; } void unique() { int m = 1, i; for (i = 1; i < n; i++) { if (dblcmp(hp[i].angle - hp[i - 1].angle)) hp[m++] = hp[i]; else if (dblcmp( hp[m - 1].b.sub(hp[m - 1].a).det(hp[i].a.sub(hp[m - 1].a)) > 0)) hp[m - 1] = hp[i]; } n = m; } bool halfplaneinsert() { int i; for (i = 0; i < n; i++) hp[i].calcangle(); sort(hp, hp + n); unique(); que[st = 0] = 0; que[ed = 1] = 1; p[1] = hp[0].crosspoint(hp[1]); for (i = 2; i < n; i++) { while (st < ed && dblcmp((hp[i].b.sub(hp[i].a).det(p[ed].sub(hp[i].a)))) < 0) ed--; while (st < ed && dblcmp((hp[i].b.sub(hp[i].a).det(p[st + 1].sub(hp[i].a)))) < 0) st++; que[++ed] = i; if (hp[i].parallel(hp[que[ed - 1]])) return false; p[ed] = hp[i].crosspoint(hp[que[ed - 1]]); } while ( st < ed && dblcmp(hp[que[st]].b.sub(hp[que[st]].a).det(p[ed].sub(hp[que[st]].a))) < 0) ed--; while (st < ed && dblcmp(hp[que[ed]] .b.sub(hp[que[ed]].a) .det(p[st + 1].sub(hp[que[ed]].a))) < 0) st++; if (st + 1 >= ed) return false; return true; } void getconvex(polygon &con) { p[st] = hp[que[st]].crosspoint(hp[que[ed]]); con.n = ed - st + 1; int j = st, i = 0; for (; j <= ed; i++, j++) { con.p[i] = p[j]; } } }; struct point3 { double x, y, z; point3() {} point3(double _x, double _y, double _z) : x(_x), y(_y), z(_z){}; void input() { scanf("%lf%lf%lf", &x, &y, &z); } void output() { printf("%.2lf %.2lf %.2lf", x, y, z); } bool operator==(point3 a) { return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0 && dblcmp(a.z - z) == 0; } bool operator<(point3 a) const { return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) == 0 ? dblcmp(z - a.z) < 0 : y < a.y : x < a.x; } double len() { return sqrt(len2()); } double len2() { return x * x + y * y + z * z; } double distance(point3 p) { return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y) + (p.z - z) * (p.z - z)); } point3 add(point3 p) { return point3(x + p.x, y + p.y, z + p.z); } point3 sub(point3 p) { return point3(x - p.x, y - p.y, z - p.z); } point3 mul(double d) { return point3(x * d, y * d, z * d); } point3 div(double d) { return point3(x / d, y / d, z / d); } double dot(point3 p) { return x * p.x + y * p.y + z * p.z; } point3 det(point3 p) { return point3(y * p.z - p.y * z, p.x * z - x * p.z, x * p.y - p.x * y); } double rad(point3 a, point3 b) { point3 p = (*this); return acos(a.sub(p).dot(b.sub(p)) / (a.distance(p) * b.distance(p))); } point3 trunc(double r) { r /= len(); return point3(x * r, y * r, z * r); } point3 rotate(point3 o, double r) {} }; struct line3 { point3 a, b; line3() {} line3(point3 _a, point3 _b) { a = _a; b = _b; } bool operator==(line3 v) { return (a == v.a) && (b == v.b); } void input() { a.input(); b.input(); } double length() { return a.distance(b); } double dispointtoline(point3 p) { return b.sub(a).det(p.sub(a)).len() / a.distance(b); } point3 lineprog(point3 p) { return a.add(b.sub(a).trunc(b.sub(a).dot(p.sub(a)) / b.distance(a))); } }; struct plane { point3 a, b, c, o; plane() {} plane(point3 _a, point3 _b, point3 _c) { a = _a; b = _b; c = _c; o = pvec(); } plane(double _a, double _b, double _c, double _d) { o = point3(_a, _b, _c); if (dblcmp(_a) != 0) { a = point3((-_d - _c - _b) / _a, 1, 1); } else if (dblcmp(_b) != 0) { a = point3(1, (-_d - _c - _a) / _b, 1); } else if (dblcmp(_c) != 0) { a = point3(1, 1, (-_d - _a - _b) / _c); } } void input() { a.input(); b.input(); c.input(); } point3 pvec() { return b.sub(a).det(c.sub(a)); } int crossline(line3 u, point3 &p) { double x = o.dot(u.b.sub(a)); double y = o.dot(u.a.sub(a)); double d = x - y; if (dblcmp(fabs(d)) == 0) return 0; p = u.a.mul(x).sub(u.b.mul(y)).div(d); return 1; } }; int count(line u) { int x = (int)ceil(u.a.x); int y = (int)floor(u.b.x + eps); return y - x + 1; } halfplanes hps; polygon pl, con; int main() { int i, j, k, n, cc = 0, cas; cas = 1; while (cas--) { scanf("%d", &n); pl.n = n; pl.input(); line uu = line(pl.p[0], pl.p[1]); pl.getarea(); pl.getline(); hps.n = 0; for (i = 0; i < n; i++) { hps.push(halfplane(pl.l[i].a, pl.l[i].b)); } hps.halfplaneinsert(); hps.getconvex(pl); pl.getline(); for (i = 0; i < pl.n; i++) pl.l[i].adjust(); line u = uu; line seg; int b = 0; set<point> st; u.adjust(); for (i = 0; i < pl.n; i++) { if (!u.segcrossseg(pl.l[i])) continue; if (u.parallel(pl.l[i])) { if (dblcmp(u.a.sub(pl.l[i].a).det(u.a.sub(pl.l[i].b))) == 0) { b = 1; if (u.b < pl.l[i].a) { puts("0"); return 0; } if (pl.l[i].b < u.a) { puts("0"); return 0; } if (u.a < pl.l[i].a) { seg.a = pl.l[i].a; } else { seg.a = u.a; } if (pl.l[i].b < u.b) { seg.b = pl.l[i].b; } else { seg.b = u.b; } } } else { st.insert(u.crosspoint(pl.l[i])); } } if (!b && st.size() == 0) { puts("0"); continue; } if (!b) { set<point>::iterator ptr = st.begin(); if (st.size() == 2) { { seg.a = *ptr; ++ptr; seg.b = *ptr; } } else { seg.a = *ptr; seg.b = *ptr; } } if (n == 187) { } cout << count(seg) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void read(int &digit) { digit = 0; char c; for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar()) ; bool type = false; if (c == '-') type = true, c = getchar(); for (; c >= '0' && c <= '9'; digit = digit * 10 + c - '0', c = getchar()) ; if (type == true) digit = -digit; } struct Point { int x, y; } a[1010]; int n, m, y00; long long mjyl, mjyr; int main() { read(n); for (int i = 1; i <= n; i++) read(a[i].x), read(a[i].y); y00 = a[1].y; mjyl = a[1].x, mjyr = a[2].x; if (mjyl > mjyr) swap(mjyl, mjyr); for (int i = 2; i <= n; i++) { int _a = a[i].x, _b = a[i].y; int _c = a[i % n + 1].x, _d = a[i % n + 1].y; if (_b == _d && _b == y00) continue; if (_b == _d && ((_b < y00) ^ (_a > _c))) { printf("0\n"); return 0; } if (_b == _d) continue; double q = y00 - _b; q *= _c - _a; q /= (_d - _b); q += _a; if (_d - _b > 0) { long long ql = floor(q + 0.99); mjyl = max(mjyl, ql); } else { long long ql = floor(q + 1e-6); mjyr = min(mjyr, ql); } } if (mjyl <= mjyr) cout << mjyr - mjyl + 1 << endl; else printf("0\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 4096; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} } p[MAXN], pa, pb; Point operator-(const Point& lhs, const Point& rhs) { return Point(lhs.x - rhs.x, lhs.y - rhs.y); } long long operator*(const Point& lhs, const Point& rhs) { return lhs.x * rhs.y - lhs.y * rhs.x; } long long operator%(const Point& lhs, const Point& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void intersection(const Point& u1, const Point& u2, const Point& v1, const Point& v2, long long& num, long long& den) { static long long gcd; num = (u1 - v1) * (v1 - v2); den = (u1 - u2) * (v1 - v2); if (den < 0) { num = -num; den = -den; } gcd = ::gcd(num >= 0 ? num : -num, den); num /= gcd; den /= gcd; } long long floor(long long a, long long b); long long ceil(long long a, long long b); long long floor(long long a, long long b) { return a >= 0 ? a / b : -ceil(-a, b); } long long ceil(long long a, long long b) { return a >= 0 ? (a + b - 1) / b : -floor(-a, b); } int main() { int n; long long num, den; long long left, right, cross; cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i].x >> p[i].y; } pa = p[0]; pb = p[1]; if (pa.x > pb.x) { swap(pa, pb); } left = pa.x; right = pb.x; for (int i = 2; i < n - 1; ++i) { cross = (p[i + 1] - p[i]) * (pb - pa); if (cross != 0) { intersection(pa, pb, p[i], p[i + 1], num, den); if (cross > 0) { right = min(right, pa.x + floor((pb.x - pa.x) * num, den)); } else if (cross < 0) { left = max(left, pa.x + ceil((pb.x - pa.x) * num, den)); } } else if ((p[i + 1] - p[i]) % (p[1] - p[0]) > 0) { cout << 0 << endl; return 0; } } cout << max(right - left + 1, 0LL) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int cmp(long double first, long double second = 0, long double tol = 1e-9) { return (first <= second + tol) ? (first + tol < second) ? -1 : 0 : 1; } struct point { long double first, second; point() : first(0), second(0) {} point(long double X, long double Y) : first(X), second(Y) {} int cmp(point a) { if (::cmp(first, a.first)) return ::cmp(first, a.first); return ::cmp(second, a.second); } point operator+(point a) { return point(first + a.first, second + a.second); } point operator-(point a) { return point(first - a.first, second - a.second); } point operator*(long double a) { return point(first * a, second * a); } point operator/(long double a) { return point(first / a, second / a); } long double operator*(point a) { return first * a.first + second * a.second; } long double operator%(point a) { return first * a.second - second * a.first; } bool operator<(point a) { return cmp(a) == -1; } bool operator==(point a) { return cmp(a) == 0; } bool operator!=(point a) { return cmp(a) != 0; } bool operator<=(point a) { return cmp(a) <= 0; } point operator[](long double a) { return point(cos(a) * first - sin(a) * second, sin(a) * first + cos(a) * second); } }; long double abs(point a) { return sqrt(a * a); } long double dist(point a, point b) { return abs(a - b); } int ccw(point a, point b, point c) { return cmp((b - a) % (c - a)); } bool between(point a, point b, point c) { return ccw(a, b, c) == 0 && cmp((a - b) * (c - b)) <= 0; } long double angle(point u, point v) { return atan2(u % v, u * v); } int main() { int n, Y, l, r; point ult, cur; scanf("%d", &n); for (int(i) = (0); (i) < (n); (i)++) { int first, second; scanf("%d%d", &first, &second); cur.first = first, cur.second = second; if (i < 2) { Y = second; if (!i) l = first; else r = first; if (i && l > r) swap(l, r); } else { while (l <= r && ccw(ult, cur, point(l, Y)) == 1) l++; while (r >= l && ccw(ult, cur, point(r, Y)) == 1) r--; } ult = cur; } printf("%d\n", r - l + 1); }
#include <bits/stdc++.h> using namespace std; struct point { int x; int y; point() {} point(const int x, const int y) : x(x), y(y) {} } A[100000]; inline bool leftTurn(point A, point B) { return (long long)A.x * B.y < (long long)A.y * B.x; } inline bool rightTurn(point A, point B) { return !leftTurn(A, B); } point makeVector(point A, point B) { return point(B.x - A.x, B.y - A.y); } point opposite(point A) { return point(-A.x, -A.y); } int n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &A[i].x, &A[i].y); } int left = A[0].x; int right = A[1].x; for (int i = 2; i < n - 1; i++) { point segment = makeVector(A[i], A[i + 1]); point opp = opposite(segment); if (leftTurn(makeVector(A[i], A[0]), segment) && leftTurn(makeVector(A[i], A[1]), segment)) { cout << 0 << endl; return 0; } if (!rightTurn(makeVector(A[i], A[0]), segment) || !rightTurn(makeVector(A[i], A[1]), segment)) { double t = double(A[0].y - A[i].y) / segment.y; if (A[i].y < A[i + 1].y && A[0].x < A[1].x) { long long x = ceil(A[i].x + (long long)segment.x * t); if (left < x) left = x; } else if (A[i].y > A[i + 1].y && A[0].x < A[1].x) { long long x = A[i].x + (long long)segment.x * t; if (x < right) right = x; } else if (A[i].y > A[i + 1].y && A[0].x > A[1].x) { long long x = A[i].x + (long long)segment.x * t; if (x < left) left = x; } else if (A[i].y < A[i + 1].y && A[0].x > A[1].x) { long long x = ceil(A[i].x + (long long)segment.x * t); if (right < x) right = x; } } } if (A[0].x < A[1].x) cout << ((right - left + 1 > 0) ? right - left + 1 : 0) << endl; else cout << ((left - right + 1 > 0) ? left - right + 1 : 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long test(double x) { if (x < 0) { return (test(-x + 1.0 - 0.000000001)) * -1; } return x; } int main() { int n; long long x[2000] = {}; long long y[2000] = {}; cin >> n; for (int i = 1; i <= n; i++) cin >> x[i] >> y[i]; long long minx = min(x[1], x[2]); long long maxx = max(x[1], x[2]); for (int i = 3; i < n; i++) { if (y[i] == y[i + 1]) { if (y[3] < y[1] && x[i + 1] > x[i]) { minx = 1e9; } if (y[3] > y[1] && x[i + 1] < x[i]) { minx = 1e9; } continue; } long long disty = y[i] - y[i + 1]; long long distx = x[i + 1] - x[i]; long long distcom = x[i] * disty + y[i] * distx; double inter = (distcom - y[1] * distx) * 1.0 / disty; if (y[i] < y[i + 1]) { minx = max(minx, test(inter + 1 - 0.000000001)); } else { maxx = min(maxx, test(inter)); } } if (minx > maxx) { minx = maxx + 1; } cout << maxx - minx + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long x[1005], y[1005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld%lld", x + i, y + i); if (y[n - 1] > y[0]) { for (int i = 0; i < n; i++) { x[i] = -x[i]; y[i] = -y[i]; } } int idx = n - 1, l, r, i; for (i = x[0]; i <= x[1] && idx > 1; i++) { while ((x[idx] - i) * (y[idx - 1] - y[0]) >= (x[idx - 1] - i) * (y[idx] - y[0]) && idx > 1) idx--; } l = i - 1; idx = 1; for (i = x[1]; i >= x[0] && idx < n - 1; i--) { while ((x[idx] - i) * (y[idx + 1] - y[0]) <= (x[idx + 1] - i) * (y[idx] - y[0]) && idx < n - 1) idx++; } r = i + 1; printf("%d\n", max(r - l + 1, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; struct Tpoint { int x, y; Tpoint() {} Tpoint(int _x, int _y) { x = _x; y = _y; } } H[maxn]; int n, ret; void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &H[i].x, &H[i].y); } long long Cross(Tpoint p0, Tpoint p1, Tpoint p2) { return ((long long)p1.x - p0.x) * (p2.y - p0.y) - ((long long)p2.x - p0.x) * (p1.y - p0.y); } void work() { int l = H[1].x, r = H[2].x; if (l > r) { int t = l; l = r; r = t; } H[n + 1] = H[1]; for (int i = 2; i <= n && l <= r; i++) { while (l <= r && Cross(H[i], H[i + 1], Tpoint(l, H[1].y)) > 0) l++; while (l <= r && Cross(H[i], H[i + 1], Tpoint(r, H[1].y)) > 0) r--; } ret = r - l + 1; } void print() { printf("%d\n", ret); } int main() { init(); work(); print(); return 0; }
#include <bits/stdc++.h> using namespace std; struct point { long long x, y; }; point s[1111]; int main() { long long n; cin >> n; for (long long i = 0; i < n; i++) cin >> s[i].x >> s[i].y; long long mn = min(s[0].x, s[1].x); long long mx = max(s[0].x, s[1].x); for (long long i = 2; i < n; i++) { if (s[i].y == s[i - 1].y) { if ((s[i].x > s[i - 1].x && s[0].y > s[2].y) || (s[i].x < s[i - 1].x && s[0].y < s[2].y)) { cout << 0 << endl; return 0; } else continue; } double y1 = s[i].y; double y2 = s[i - 1].y; double x1 = s[i].x; double x2 = s[i - 1].x; double A = y1 - y2; double B = x2 - x1; double C = y2 * x1 - y1 * x2; double cross = (-B * s[0].y - C) / A; long long x = (long long)cross; if (s[i].y < s[i - 1].y) { mx = min(mx, (long long)(cross + 1000000 + 1e-9) - 1000000); } else { mn = max(mn, (long long)(ceil(cross - 2e-9) + 1e-9)); } } if (mn > mx) { cout << 0 << endl; } else cout << mx - mn + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct point { int x, y; point(int x = 0, int y = 0) : x(x), y(y) {} }; point p[1005]; int n; long long cp(point a, point b) { return (long long)a.x * b.y - (long long)a.y * b.x; } point operator-(point a, point b) { return point(a.x - b.x, a.y - b.y); } int work() { int i; deque<point> q; for (i = min(p[1].x, p[2].x); i <= max(p[1].x, p[2].x); ++i) q.push_back(point(i, p[1].y)); for (i = 2; i <= n; ++i) { while (!q.empty() && cp(p[i + 1] - p[i], q.front() - p[i]) > 0) q.pop_front(); while (!q.empty() && cp(p[i + 1] - p[i], q.back() - p[i]) > 0) q.pop_back(); } return q.size(); } int main() { int i; scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d%d", &p[i].x, &p[i].y); p[n + 1] = p[1]; printf("%d\n", work()); return 0; }
#include <bits/stdc++.h> int n; int x[1002], y[1002]; int tx[1002], ty[1002]; int flag; double Y, left, right; void input() { int i, flag; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &x[i], &y[i]); } flag = 0; for (i = 0; i < n; i++) if (y[i] > y[0] || y[i] > y[1]) { flag = 1; break; } if (flag) { for (i = 0; i < n; i++) y[i] = -y[i]; } if (x[0] > x[1]) { for (i = 0; i < n; i++) { tx[(n - i + 1) % n] = x[i]; ty[(n - i + 1) % n] = y[i]; } for (i = 0; i < n; i++) { x[i] = tx[i]; y[i] = ty[i]; } } } void calc_range(int a, int b) { if (y[a] == y[b]) { if (x[a] < x[b]) flag = 0; else return; } double t, xx; t = (Y - y[a]) / (y[b] - y[a]); xx = x[a] + (x[b] - x[a]) * t; if (t <= 0) { if (xx < right) right = xx; } else { if (xx > left) left = xx; } } int main() { int i, a, b, ans; input(); Y = y[0]; left = ((x[0]) < (x[1]) ? (x[0]) : (x[1])); right = ((x[0]) > (x[1]) ? (x[0]) : (x[1])); flag = 1; for (i = 1; i < n; i++) { a = i; b = (i + 1) % n; calc_range(a, b); } if (flag == 0) { printf("0\n"); return 0; } if (fabs(right - left) < 1e-7) ans = 1; else if (right < left) ans = 0; else ans = floor(right + 1e-6) - ceil(left - 1e-6) + 1; printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n, i, t, ee, flag; long long x[1010], y[1010]; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%I64d%I64d", &x[i], &y[i]); t = n; if (y[n] > y[1]) for (i = 1; i <= n; i++) y[i] = -y[i], x[i] = -x[i]; for (i = x[1]; i <= x[2] && t > 2; i++) for (; (x[t] - i) * (y[t - 1] - y[1]) - (x[t - 1] - i) * (y[t] - y[1]) >= 0 && t > 2; t--) ; ee = i - 1; t = 2; for (i = x[2]; i >= x[1] && t < n; i--) for (; (x[t] - i) * (y[t + 1] - y[1]) - (x[t + 1] - i) * (y[t] - y[1]) <= 0 && t < n; t++) ; i++; if (i - ee + 1 > 0) printf("%d\n", i - ee + 1); else printf("0\n"); }
#include <bits/stdc++.h> int n, x[1003], y[1003]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", x + i, y + i); double l, r; if (x[1] > x[2]) l = x[2], r = x[1]; else r = x[2], l = x[1]; for (int i = 3; i < n; ++i) { double dx = x[i + 1] - x[i]; double dy = y[i + 1] - y[i]; double d = y[1] - y[i]; double xx; if (!dy) xx = 1e10; else xx = x[i] + d / dy * dx; if (d * dx - dy * (r - x[i]) > 0 && r > xx) r = xx; if (d * dx - dy * (l - x[i]) > 0 && l < xx) l = xx; } r = floor(r), l = ceil(l); if (r >= l) printf("%.0lf\n", r - l + 1.1); else puts("0"); return 0; }
#include <bits/stdc++.h> using namespace std; int x[1010], y[1010]; int main(void) { int N, i, j; cin >> N; for ((i) = 0; (i) < (int)(N); (i)++) cin >> x[i] >> y[i]; if (x[0] > x[1]) for ((i) = 0; (i) < (int)(N); (i)++) x[i] = -x[i]; int tmp = y[0]; for ((i) = 0; (i) < (int)(N); (i)++) y[i] -= tmp; if (y[2] < 0) for ((i) = 0; (i) < (int)(N); (i)++) y[i] = -y[i]; double low = x[0] - 1.0, high = x[1] + 1.0; for (i = 1; i < N; i++) { j = (i + 1) % N; if (y[i] == y[j]) { if (x[i] < x[j]) low = x[1] + 1.0; continue; } double p = x[i] - (double)(x[j] - x[i]) / (y[j] - y[i]) * y[i]; if (y[i] < y[j]) high = min(high, p); else low = max(low, p); } int ans = 0; for (i = x[0]; i <= x[1]; i++) if (i > low - 1.0E-7 && i < high + 1.0E-7) ans++; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct point { double x, y; }; bool multi(point p1, point p2, point p3) { return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x) > 0; } bool mul(point p1, point p2) { if (p1.x * p2.y - p1.y * p2.x >= 0) return 1; return 0; } point p[1005]; int n; bool mark[1005]; int ABS(int i) { if (i < 0) i = -i; return i; } point cal(point p1, point p2) { double y = p[0].y; double x = p1.x + (y - p1.y) * (p1.x - p2.x) / (p1.y - p2.y); point p3; p3.x = x; p3.y = y; return p3; } point p1, p2; point p3, p4; int main() { int i, j, k; double minx, maxx; int s; while (cin >> n) { for (i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); p[n] = p[0]; maxx = minx = p[0].x; if (maxx < p[1].x) maxx = p[1].x; else minx = p[1].x; p3.x = p[1].x - p[0].x; p3.y = 0; if (p[0].x < p[1].x) for (i = 1; i < n; i++) { if (p[i].y == p[i + 1].y) { if ((p[i + 1].x - p[i].x) * p3.x > 0) break; continue; } p1 = cal(p[i], p[i + 1]); p4.x = p[i + 1].x - p[i].x; p4.y = p[i + 1].y - p[i].y; p2 = p1; if (mul(p4, p3)) { if (minx <= p1.x && p1.x <= maxx) maxx = p1.x; else if (p1.x < minx) break; } else { if (minx <= p1.x && p1.x <= maxx) minx = p1.x; else if (p1.x > maxx) break; } } else { for (i = 1; i < n; i++) { if (p[i].y == p[i + 1].y) { if ((p[i + 1].x - p[i].x) * p3.x > 0) break; continue; } p1 = cal(p[i], p[i + 1]); p4.x = p[i + 1].x - p[i].x; p4.y = p[i + 1].y - p[i].y; p2 = p1; if (!mul(p4, p3)) { if (minx <= p1.x && p1.x <= maxx) maxx = p1.x; else if (p1.x < minx) break; } else { if (minx <= p1.x && p1.x <= maxx) minx = p1.x; else if (p1.x > maxx) break; } } } if (i < n) cout << "0" << endl; else { s = int(maxx) - int(minx) + 1; if (minx != double(int(minx))) s -= 1; cout << s << endl; } } return 0; }
#include <bits/stdc++.h> struct Point { long long int x; long long int y; std::string pToString() { std::string x_s(std::to_string(x)); std::string y_s(std::to_string(y)); std::string printP(x_s + " " + y_s); return printP; } }; bool operator<(Point const &p1, Point const &p2) { if (p1.x == p2.x) { return p1.y < p2.y; } return p1.x < p2.x; } Point operator-(Point const &p1, Point const &p2) { long long x = p1.x - p2.x; long long y = p1.y - p2.y; Point temp = {x, y}; return temp; } long long int cross(const Point &t1, const Point &t2, const Point &t3) { Point a = t1 - t3; Point b = t2 - t3; return a.x * b.y - b.x * a.y; } long long int getDist(Point const &p1, Point const &p2) { Point temp = p1 - p2; long long int dist = temp.x * temp.x + temp.y * temp.y; return dist; } void printStack(std::vector<Point> points) { for (Point p : points) { std::cout << p.pToString() << std::endl; } } void adjustPolygon(std::vector<Point> &polygon) { auto pointA = polygon[0]; auto pointB = polygon[1]; for (int i = 0; i < polygon.size(); i++) { polygon[i] = polygon[i] - pointB; } if (polygon[1].y > polygon[2].y) { for (int i = 0; i < polygon.size(); i++) { polygon[i].x *= -1; polygon[i].y *= -1; } } } long double getIntersect(Point a, Point b) { if (a.y == b.y) { return 1e9L; } if (a.y > b.y) { std::swap(a, b); } long double slope = (long double)b.y / (b.y - a.y); return b.x + (a.x - b.x) * slope; } int getTowers(std::vector<Point> polygon) { int numTowers; long long int min = 0; long long int max = polygon[0].x; Point maxBase = polygon[0]; Point minBase = polygon[1]; if (polygon.size() < 3) { return (max - min) + 1; } for (int i = 2; i < polygon.size() - 1; i++) { long long int crs1 = cross(polygon[i + 1], polygon[i], maxBase); long long int crs2 = cross(polygon[i + 1], polygon[i], minBase); if (crs1 < 0 && crs2 < 0) { return 0; } long double inter = getIntersect(polygon[i], polygon[i + 1]); if (crs1 < 0) { max = std::min(max, (long long int)std::floor(inter)); } if (crs2 < 0) { min = std::max(min, (long long int)std::ceil(inter)); } if (min > max) { return 0; } } numTowers = max - min; return numTowers + 1; } int main() { int numPoints; long long int inputX; long long int inputY; std::vector<Point> polygon; int numTowers; std::cin >> numPoints; for (int i = 0; i < numPoints; i++) { std::cin >> inputX >> inputY; Point p = {inputX, inputY}; polygon.emplace_back(p); } adjustPolygon(polygon); numTowers = getTowers(polygon); if (numTowers < 0) { std::cout << "0" << std::endl; } else { std::cout << numTowers << std::endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2000; long long x[N], y[N]; int l, r; const long double inf = 1 << 25; long double inter(int i) { long long A1 = 0; long long B1 = 1; long long C1 = -y[0]; long long A2 = y[i + 1] - y[i]; long long B2 = x[i] - x[i + 1]; long long C2 = -(A2 * x[i] + B2 * y[i]); long double d = A1 * B2 - A2 * B1; if (d == 0) return inf; return -(C1 * B2 - C2 * B1) / (d + 0.); } int sgn(int i, long long q) { long long a = (x[i] - q) * (y[i + 1] - y[0]) - (x[i + 1] - q) * (y[i] - y[0]); return a > 0 ? 1 : (a < 0 ? -1 : 0); } void add(int i, long double x) { int q = (int)x - 1; int sig = sgn(i, q); while ((sgn(i, q + 1) == sig || (sgn(i, q + 1) == 0 && sig < 0)) && (abs(x - inf) > 1e-5)) q++; sig = sgn(i, q); if (sig <= 0) { r = min(r, q); } else { l = max(l, q + 1); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < (int)n; ++i) scanf("%I64d%I64d", &x[i], &y[i]); l = min(x[0], x[1]); r = max(x[0], x[1]); for (int i = 1; i < n - 1; i++) { add(i, inter(i)); } printf("%d\n", max(r - l + 1, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; struct rec { double x, y; } ver[1010], p[1010], q[1010], f[10]; int n, m, cnt, now, ans, i; double a, b, c, eps = 1e-10; double mul(rec a, rec b, rec c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } void line(rec x, rec y, double &a, double &b, double &c) { a = y.y - x.y; b = x.x - y.x; c = x.y * y.x - x.x * y.y; } rec cross(rec x, rec y, double a, double b, double c) { double u = fabs(a * x.x + b * x.y + c), v = fabs(a * y.x + b * y.y + c); return (rec){(x.x * v + y.x * u) / (u + v), (x.y * v + y.y * u) / (u + v)}; } void cut(double a, double b, double c) { int i, now = 0; for (i = 1; i <= cnt; i++) if (a * p[i].x + b * p[i].y + c >= 0) q[++now] = p[i]; else { if (a * p[i - 1].x + b * p[i - 1].y + c > 0) q[++now] = cross(p[i], p[i - 1], a, b, c); if (a * p[i + 1].x + b * p[i + 1].y + c > 0) q[++now] = cross(p[i], p[i + 1], a, b, c); } for (i = 1; i <= now; i++) p[i] = q[i]; cnt = now, p[0] = p[cnt], p[cnt + 1] = p[1]; } void solve() { double a, b, c; memcpy(p, ver, sizeof(p)); p[0] = p[n], cnt = n; for (i = 1; i <= n; i++) { line(ver[i], ver[i + 1], a, b, c); cut(a, b, c); } } int main() { cin >> n; for (i = 1; i <= n; i++) cin >> ver[i].x >> ver[i].y; ver[n + 1] = ver[1]; solve(); for (i = 1; i <= cnt; i++) if (fabs(mul(ver[1], ver[2], p[i])) < eps) f[++m] = p[i]; else if (mul(ver[1], ver[2], p[i]) * mul(ver[1], p[i + 1], ver[2]) > eps) { line(ver[1], ver[2], a, b, c); f[++m] = cross(p[i], p[i + 1], a, b, c); } if (m == 1) f[2] = f[1]; if (m) { if (f[1].x > f[2].x) swap(f[1], f[2]); if (ver[1].x > ver[2].x) swap(ver[1], ver[2]); ans = floor(min(ver[2].x, f[2].x)) - ceil(max(ver[1].x, f[1].x)) + 1; if (ans < 0) ans = 0; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int i, j, k, l, n, m, s, an, sum; const double eps = 1e-7; const double oo = 2e9; const int N = 2100; class arr { public: double x, y; } a[N], t[N * 2]; bool com(const arr &o, const arr &p) { return o.x < p.x; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lf%lf", &a[i].x, &a[i].y); if (a[2].x < a[1].x) { for (int i = 1; i <= n; i++) { a[i].x = -a[i].x; a[i].y = -a[i].y; if (fabs(a[i].x) < eps) a[i].x = 0; if (fabs(a[i].y) < eps) a[i].y = 0; } } a[n + 1] = a[1]; for (int i = 3; i <= n; i++) { double b, k, xx; if (a[i].x == a[i + 1].x) { if (a[i].y > a[i + 1].y) xx = -oo; else xx = a[i].x; } else { k = (a[i].y - a[i + 1].y) / (a[i].x - a[i + 1].x); b = a[i].y - a[i].x * k; if (k == 0) { if (a[i].x > a[i + 1].x) xx = -oo; else xx = oo; } else xx = (a[1].y - b) / k; if (a[i + 1].y < a[i].y) xx = -oo; } xx = floor(xx + 1.0 - eps); t[++s].x = xx; t[s].y = 1; if (a[i].x == a[i - 1].x) { if (a[i].y > a[i - 1].y) xx = oo; else xx = a[i].x; } else { k = (a[i].y - a[i - 1].y) / (a[i].x - a[i - 1].x); b = a[i].y - a[i].x * k; if (k == 0) { if (a[i].x < a[i - 1].x) xx = oo; else xx = -oo; } else xx = (a[1].y - b) / k; if (a[i - 1].y < a[i].y) xx = oo; } xx = floor(xx) + 1; t[++s].x = xx; t[s].y = -1; } sort(&t[1], &t[s + 1], com); int ss = 0; an = 0; for (int i = 1; i <= s; i++) { ss += int(t[i].y); if ((t[i].x >= a[1].x) && (ss == n - 2)) { for (int j = i + 1; j <= s; j++) if (t[j].y == -1) { an = int(t[j].x - t[i].x); break; } break; } if (t[i].x > a[2].x) break; } printf("%d", an); return 0; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 1003, MaxC = 1000000, NA = -1; double x[MaxN], y[MaxN]; int a[MaxN]; int n; int main(void) { int i, res; double cur, lop, loq, hip, hiq, p, q; while (scanf(" %d", &n) != EOF) { for (i = 0; i < n; i++) scanf(" %lf %lf", &x[i], &y[i]); if (x[0] > x[1]) for (i = 0; i < n; i++) { x[i] = MaxC - x[i]; y[i] = MaxC - y[i]; } assert(x[0] <= x[1]); assert(y[0] == y[1]); for (i = 2; i < n; i++) assert(y[i] <= y[0]); lop = x[0]; loq = 1; hip = x[1]; hiq = 1; for (i = 3; i < n; i++) { q = y[i - 1] - y[i]; p = (x[i - 1] - x[i]) * (y[0] - y[i]) + x[i] * q; if (q < 0) p *= -1, q *= -1; if (y[i - 1] > y[i]) if (hip * q > hiq * p) hip = p, hiq = q; if (y[i - 1] < y[i]) if (lop * q < loq * p) lop = p, loq = q; if (y[i - 1] == y[i]) if (x[i - 1] < x[i]) lop = x[1] + 1, loq = 1; } res = 0; for (cur = x[0]; cur <= x[1]; cur++) if (lop * 1 <= cur * loq && cur * hiq <= hip * 1) res++; printf("%d\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 4096; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} } p[MAXN], pa, pb; Point operator-(const Point& lhs, const Point& rhs) { return Point(lhs.x - rhs.x, lhs.y - rhs.y); } long long operator*(const Point& lhs, const Point& rhs) { return lhs.x * rhs.y - lhs.y * rhs.x; } long long operator%(const Point& lhs, const Point& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void intersection(const Point& u1, const Point& u2, const Point& v1, const Point& v2, long long& num, long long& den) { static long long gcd; num = (u1 - v1) * (v1 - v2); den = (u1 - u2) * (v1 - v2); if (den < 0) { num = -num; den = -den; } gcd = ::gcd(num >= 0 ? num : -num, den); num /= gcd; den /= gcd; } long long floor(long long a, long long b); long long ceil(long long a, long long b); long long floor(long long a, long long b) { return a >= 0 ? a / b : -ceil(-a, b); } long long ceil(long long a, long long b) { return a >= 0 ? (a + b - 1) / b : -floor(-a, b); } int main() { int n; long long num, den; long long left, right, cross; cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i].x >> p[i].y; } pa = p[0]; pb = p[1]; if (pa.x > pb.x) { swap(pa, pb); } left = pa.x; right = pb.x; for (int i = 2; i < n - 1; ++i) { cross = (p[i + 1] - p[i]) * (pb - pa); if (cross != 0) { intersection(pa, pb, p[i], p[i + 1], num, den); if (cross > 0) { right = min(right, pa.x + floor((pb.x - pa.x) * num, den)); } else if (cross < 0) { left = max(left, pa.x + ceil((pb.x - pa.x) * num, den)); } } else if ((p[i + 1] - p[i]) % (p[1] - p[0]) > 0) { cout << 0 << endl; return 0; } } cout << max(right - left + 1, 0LL) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; long long x[2000], y[2000], a[2000], b[2000], c[2000]; inline bool leq(double a, double b) { return b - a > -eps; } inline long long getl(double a) { long long b = (long long)a; for (int j = b - 10; j <= b + 10; j++) { if (leq(a, j)) { return j; } } } inline long long getr(double a) { long long b = (long long)a; for (int j = b + 10; j >= b - 10; j--) { if (leq(j, a)) { return j; } } } inline long long gcd(long long a, long long b) { while (b > 0) { a %= b; swap(a, b); } return a; } inline void fail() { printf("0\n"); exit(0); } inline long long vec(long long x1, long long y1, long long x2, long long y2) { return x1 * y2 - x2 * y1; } inline int sgn(long long a) { if (a < 0) { return -1; } if (a == 0) { return 0; } return 1; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d %I64d", &x[i], &y[i]); } x[n] = x[0]; y[n] = y[0]; for (int i = 1; i < n; i++) { a[i] = x[i] * y[i + 1] - y[i] * x[i + 1]; b[i] = x[i + 1] - x[i]; c[i] = y[i] - y[i + 1]; } long long vx = x[1] - x[0]; long long vy = y[1] - y[0]; long long g = gcd(abs(vx), abs(vy)); vx = sgn(vx) * (abs(vx) / g); vy = sgn(vy) * (abs(vy) / g); long long l = 0, r = abs(x[1] - x[0]) / abs(vx); if (vec(x[2] - x[0], y[2] - y[0], x[3] - x[0], y[3] - y[0]) > 0) { for (int i = 1; i < n; i++) { long long val1 = vy * b[i] + vx * c[i]; long long val2 = -a[i] - y[0] * b[i] - x[0] * c[i]; if (val1 == 0) { if (val2 <= 0) { continue; } fail(); } if (val1 < 0) { r = min(r, getr((double)val2 / val1)); continue; } l = max(l, getl((double)val2 / val1)); } } else { for (int i = 1; i < n; i++) { long long val1 = vy * b[i] + vx * c[i]; long long val2 = -a[i] - y[0] * b[i] - x[0] * c[i]; if (val1 == 0) { if (val2 >= 0) { continue; } fail(); } if (val1 < 0) { l = max(l, getl((double)val2 / val1)); continue; } r = min(r, getr((double)val2 / val1)); } } if (l > r) { fail(); } printf("%I64d\n", r - l + 1); return 0; }
#include <bits/stdc++.h> using namespace std; struct pointVector { long long int x; long long int y; }; bool crossProductIsPositive(pointVector A, pointVector B, pointVector C) { double cross = ((B.x - A.x) * (C.y - A.y)) - ((B.y - A.y) * (C.x - A.x)); if (cross <= 0) { return false; } return true; } long long int xIntercept(pointVector A, pointVector B) { return ((B.x * A.y - A.x * B.y) / (A.y - B.y)); } long long int xInterceptLow(pointVector A, pointVector B) { float h = (((float)B.x * (float)A.y - (float)A.x * (float)B.y) / ((float)A.y - (float)B.y)); return (long long int)ceil(h); } int main() { long long int N, x, y, x0, y0, X1, Y1; bool toLeft = false; cin >> N; vector<pointVector> points; pointVector P0, P1, P; cin >> X1 >> Y1; cin >> x0 >> y0; P0 = {X1 - x0, Y1 - y0}; P1 = {x0 - x0, y0 - y0}; if (P1.x > P0.x) { toLeft = true; P0.x *= -1; P0.y *= -1; } points.push_back(P0); points.push_back(P1); for (int i = 0; i < N - 2; ++i) { cin >> x >> y; if (toLeft) P = {-1 * (x - x0), -1 * (y - y0)}; else P = {x - x0, y - y0}; points.push_back(P); } long long int lower = 0, upper = points[0].x; for (int i = 2; i + 1 < N; ++i) { if (crossProductIsPositive(P0, points[i], points[i + 1]) && crossProductIsPositive(P1, points[i], points[i + 1])) { cout << 0 << endl; return 0; } long long int y0 = points[i].y, y1 = points[i + 1].y, xInter; if (y1 - y0 != 0) { if (crossProductIsPositive(P0, points[i], points[i + 1])) { xInter = xIntercept(points[i], points[i + 1]); upper = min(upper, xInter); } if (crossProductIsPositive(P1, points[i], points[i + 1])) { xInter = xInterceptLow(points[i], points[i + 1]); lower = max(lower, xInter); } } } if (upper >= lower) { cout << (long long int)(upper - lower) + 1 << endl; } else { cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> struct point { int x, y; point() { x = y = 0; } point(int a, int b) { x = a, y = b; } void scan() { scanf("%d%d", &x, &y); } } p[1005]; point operator-(const point &a, const point &b) { return point(a.x - b.x, a.y - b.y); } bool operator*(const point &a, const point &b) { return a.x * (long long)b.y > b.x * (long long)a.y; } int main() { int n, i, x, y, l, r; for (scanf("%d", &n), i = 0; i < n; i++) p[i].scan(); l = p[0].x, r = p[1].x, y = p[0].y; if (l > r) l ^= r, r ^= l, l ^= r; for (p[n] = p[0], i = 1; i < n; i++) { for (; l <= r && (p[i + 1] - p[i]) * (point(l - p[i].x, y - p[i].y)); l++) ; for (; l <= r && (p[i + 1] - p[i]) * (point(r - p[i].x, y - p[i].y)); r--) ; } printf("%d\n", r - l + 1); return 0; }
#include <bits/stdc++.h> inline long long crs(const std::complex<long long> &a, const std::complex<long long> &b) { return (conj(a) * b).imag(); } std::complex<long long> arr[1005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { long long x, y; scanf("%lld%lld", &x, &y); arr[i] = std::complex<long long>(x, y); }; if (arr[2].imag() > arr[0].imag()) { for (int i = 0; i < n; i++) arr[i] = -arr[i]; } int l = 1e9, r = -1e9, idx = n - 1; for (int i = arr[0].real(); i <= arr[1].real(); i++) { std::complex<long long> p(i, arr[0].imag()); while (idx > 1 && crs(arr[idx] - p, arr[idx - 1] - p) >= 0) idx--; if (idx <= 1) { l = i; break; } } idx = 1; for (int i = arr[1].real(); i >= arr[0].real(); i--) { std::complex<long long> p(i, arr[0].imag()); while (idx < n - 1 && crs(arr[idx + 1] - p, arr[idx] - p) >= 0) idx++; if (idx >= n - 1) { r = i; break; } } printf("%d\n", std::max(r - l + 1, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; int xx[1000], yy[1000]; int N; long long cross(int i, int j, int k) { long long x1 = xx[j] - xx[i]; long long y1 = yy[j] - yy[i]; long long x2 = xx[k] - xx[i]; long long y2 = yy[k] - yy[i]; return x1 * y2 - x2 * y1; } bool canBeTower(int x1, int x2, int N) { int ax = xx[0]; xx[0] = x1; bool res = true; for (int i = 2; i + 1 < N; i++) { long long left = cross(i, i + 1, 0); long long right = cross(i, i + 1, 0); if (left > 0 && right > 0) { res = false; break; } } xx[0] = ax; return res; } int lowerBound(vector<int>& arr, int N) { int left = 0, right = arr.size() - 1; while (left <= right) { int m = (int)(left + right) / 2; m = arr[m]; if (canBeTower(m, m, N)) { right = m; } else { left = m + 1; } } return left; } int main() { cin >> N; for (int i = 0; i < N; i++) cin >> xx[i] >> yy[i]; bool flip = xx[0] < xx[1]; int x = xx[0], y = yy[0]; for (int i = 0; i < N; i++) { xx[i] -= x; yy[i] -= y; } int left = min(xx[0], xx[1]); int right = max(xx[0], xx[1]); int total = 0; vector<int> arr; for (int i = left; i <= right; i++) arr.push_back(i); int lower = 0, upper = arr.size() - 1; while (lower < upper && canBeTower(arr[lower], arr[lower], N) == false) lower++; while (lower < upper && canBeTower(arr[upper], arr[upper], N) == false) upper--; bool l = canBeTower(arr[lower], arr[lower], N); bool r = canBeTower(arr[upper], arr[upper], N); total = upper - lower + 1; if ((l && r) == false) { total = 0; if (l) total++; if (r) total++; } cout << total << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef struct pt { long long x; long long y; }; int n; long long const INF = (long long)1e18; long long const UNK = (long long)2e18; pt A, B; pt v[2000]; double intersection(pt p1, pt p2) { if (p2.y == p1.y) { if (p1.x > p2.x) return -INF; return INF; } if (!((p2.y > p1.y) ^ (p1.y > A.y))) { return UNK; } double val = p2.x - 1.0 * (p2.y - A.y) / (p2.y - p1.y) * (p2.x - p1.x); return val; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d %I64d", &v[i].x, &v[i].y); } if (v[2].y > v[0].y) { for (int i = 0; i < n; i++) { v[i].y = -v[i].y; } } if (v[1].x < v[0].x) { for (int i = 0; i < n; i++) { v[i].x = -v[i].x; } } A = v[0]; B = v[1]; double top = INF; double bottom = -INF; for (int i = 2; i < n; i++) { double left = intersection(v[i], v[(i + 1) % n]); double right = intersection(v[i], v[i - 1]); if (left != UNK && right != UNK) { top = min(top, right); bottom = max(bottom, left); } else if (right != UNK) { top = min(top, right); } else if (left != UNK) { bottom = max(bottom, left); } else { double t1 = atan2(v[i - 1].y - v[i].y, v[i - 1].x - v[i].x); double t2 = atan2(v[i + 1].y - v[i].y, v[i + 1].x - v[i].x); if (t1 < t2) { top = -INF; bottom = INF; break; } } } int count = 0; for (int i = A.x; i <= B.x; i++) { if (i >= bottom && i <= top) { count++; } } printf("%d\n", count); }
#include <bits/stdc++.h> const int mn = 1100; int n, lo, hi; int x[mn], y[mn]; long long area(int ax, int ay, int bx, int by) { return (long long)ax * by - (long long)bx * ay; } int main() { scanf("%d", &n); int i; for (i = 1; i <= n; ++i) scanf("%d%d", x + i, y + i); lo = x[1], hi = x[2], x[n + 1] = x[1], y[n + 1] = y[1]; if (lo > hi) i = lo, lo = hi, hi = i; for (i = 1; i <= n; ++i) { while (lo <= hi && area(x[i + 1] - x[i], y[i + 1] - y[i], lo - x[i], y[1] - y[i]) > 0) ++lo; while (lo <= hi && area(x[i + 1] - x[i], y[i + 1] - y[i], hi - x[i], y[1] - y[i]) > 0) --hi; } printf("%d\n", hi - lo + 1); return 0; }
#include <bits/stdc++.h> using namespace std; struct point { int x, y; point() {} point(int x, int y) : x(x), y(y) {} } p[1010]; point operator-(const point &a, const point &b) { return point(a.x - b.x, a.y - b.y); } long long operator*(const point &a, const point &b) { return (long long)a.x * b.y - (long long)a.y * b.x; } int main() { int n, i, x, y, low, high; cin >> n; for (i = 0; i < n; i++) { cin >> x >> y; p[i] = point(x, y); } low = p[0].x; high = p[1].x; if (low > high) swap(low, high); p[n] = p[0]; for (i = 0; i < n; i++) { while (low <= high && (p[i + 1] - p[i]) * (point(low, p[0].y) - p[i]) > 0) low++; while (low <= high && (p[i + 1] - p[i]) * (point(high, p[0].y) - p[i]) > 0) high--; } cout << high - low + 1 << endl; }
#include <bits/stdc++.h> using namespace std; long long mydiv(long long a, long long b) { if (b < 0) { a = -a; b = -b; } if (a >= 0) return a / b; else return -((-a + b - 1) / b); } int main() { int n; scanf("%d", &n); vector<int> vx(n), vy(n); for (int i = 0; i < n; ++i) scanf("%d%d", &vx[i], &vy[i]); for (int i = 1; i < n; ++i) vy[i] -= vy[0]; vy[0] = 0; if (vy[2] < 0) { for (int i = 0; i < n; ++i) vy[i] = -vy[i]; } if (vx[0] > vx[1]) { swap(vx[0], vx[1]); swap(vy[0], vy[1]); reverse(vx.begin() + 2, vx.end()); reverse(vy.begin() + 2, vy.end()); } vx.push_back(vx[0]); vy.push_back(vy[0]); int lb = vx[0]; int ub = vx[1]; for (int i = 2; i <= n; ++i) { if (vy[i] < vy[i - 1]) { long long lb1 = vx[i] - mydiv((long long)vy[i] * (vx[i] - vx[i - 1]), vy[i] - vy[i - 1]); if (lb1 > ub) lb1 = ub + 1; lb = max(lb, (int)lb1); } else if (vy[i] > vy[i - 1]) { long long ub1 = vx[i] - mydiv((long long)vy[i] * (vx[i] - vx[i - 1]) + (vy[i] - vy[i - 1] - 1), vy[i] - vy[i - 1]); if (ub1 < lb) ub1 = lb - 1; ub = min(ub, (int)ub1); } else if (vx[i] > vx[i - 1]) ub = lb - 1; } printf("%d\n", max(0, ub - lb + 1)); return 0; }
#include <bits/stdc++.h> using namespace std; struct T { int x, y; void read() { scanf("%d%d", &x, &y); } T(int _x = 0, int _y = 0) : x(_x), y(_y) {} T operator-(T b) { return T(x - b.x, y - b.y); } long long operator*(T b) { return (long long)x * b.y - (long long)y * b.x; } } a[10000]; int n; int main() { cin >> n; for (int i = (1); i <= (n); i++) a[i].read(); if (a[1].x > a[2].x) { swap(a[1], a[2]), reverse(a + 3, a + n + 1); for (int i = (1); i <= (n); i++) a[i].y = -a[i].y; } a[n + 1] = a[1]; int l = a[1].x, r = a[2].x; for (int i = (3); i <= (n); i++) { while (l <= r && (T(l, a[1].y) - a[i]) * (a[i + 1] - a[i]) < 0) l++; while (l <= r && (T(r, a[1].y) - a[i]) * (a[i - 1] - a[i]) > 0) r--; } cout << r - l + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int xx[1000], yy[1000]; int N; long long cross(int i, int j, int k) { long long x1 = xx[j] - xx[i]; long long y1 = yy[j] - yy[i]; long long x2 = xx[k] - xx[i]; long long y2 = yy[k] - yy[i]; return x1 * y2 - x2 * y1; } bool canBeTower(int x1, int x2, int N) { int ax = xx[0]; xx[0] = x1; bool res = true; for (int i = 2; i + 1 < N; i++) { long long left = cross(i, i + 1, 0); long long right = cross(i, i + 1, 0); if (left > 0 && right > 0) { res = false; break; } } xx[0] = ax; return res; } int lowerBound(vector<int>& arr, int N) { int left = 0, right = arr.size() - 1; while (left <= right) { int m = (int)(left + right) / 2; m = arr[m]; if (canBeTower(m, m, N)) { right = m; } else { left = m + 1; } } return left; } int main() { cin >> N; for (int i = 0; i < N; i++) cin >> xx[i] >> yy[i]; bool flip = xx[0] < xx[1]; int x = xx[0], y = yy[0]; for (int i = 0; i < N; i++) { xx[i] -= x; yy[i] -= y; } int left = min(xx[0], xx[1]); int right = max(xx[0], xx[1]); int total = 0; vector<int> arr; for (int i = left; i <= right; i++) arr.push_back(i); int lower = 0, upper = arr.size() - 1; while (lower < upper && canBeTower(arr[lower], arr[lower], N) == false) lower++; while (lower < upper && canBeTower(arr[upper], arr[upper], N) == false) upper--; bool l = canBeTower(arr[lower], arr[lower], N); bool r = canBeTower(arr[upper], arr[upper], N); total = upper - lower + 1; if ((l && r) == false) { total = 0; if (l) total++; if (r) total++; } cout << total << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int turn2(std::complex<double> a, std::complex<double> b, std::complex<double> c) { double x = imag((a - b) * conj(c - b)); return x < 0 ? -1 : x > 0; } std::complex<double> omega(std::complex<double> x, std::complex<double> y) { return conj(x - y) / (x - y); } std::complex<double> rho(std::complex<double> x, std::complex<double> y) { return (conj(x) * y - x * conj(y)) / (x - y); } std::complex<double> intersection(std::complex<double> a, std::complex<double> b, std::complex<double> c, std::complex<double> d) { return (rho(a, b) - rho(c, d)) / (omega(a, b) - omega(c, d)); } bool parallel(std::complex<double> a, std::complex<double> b, std::complex<double> c, std::complex<double> d) { return imag((a - b) * conj(c - d)) == 0; } double EPS = 1e-8; double l = -1e20; double r = 1e20; vector<std::complex<double> > x; int n; int turn; std::complex<double> I = std::complex<double>(0, 1); int main() { cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; x.push_back(std::complex<double>(a, b)); } if (real(x[0]) > real(x[1])) { for (int i = 0; i < n; i++) x[i] *= -1; } turn = turn2(x[0], x[1], x[2]); for (int i = 2; i + 1 < n; i++) { if (parallel(x[0], x[1], x[i], x[i + 1])) { if (turn2(x[i], x[i + 1], x[i + 1] + I) != turn) { r = -1e20; l = 1e20; } } else { std::complex<double> s = intersection(x[0], x[1], x[i], x[i + 1]); if (turn2(x[i], x[i + 1], s + 1.0) == turn) { l = max(l, real(s)); } else { r = min(r, real(s)); } } } l = max(l, real(x[0])); r = min(r, real(x[1])); if (r - l >= -EPS) { printf("%d\n", (int)(floor(r + EPS) - ceil(l - EPS)) + 1); } else { printf("0\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } int n; int m; int x[1002], y[1002]; long long vect(long long x1, long long y8687969, long long x2, long long y2) { return x1 * y2 - x2 * y8687969; } long long scal(long long x1, long long y8687969, long long x2, long long y2) { return x1 * x2 + y8687969 * y2; } double cross(double x1, double y8687969, double x2, double y2, double x3, double y3, double x4, double y4) { double a = x2 - x1; double b = x3 - x4; double c = x3 - x1; double d = y2 - y8687969; double e = y3 - y4; double f = y3 - y8687969; return (c * e - b * f) / (a * e - b * d); } int gcd(int a, int b) { if (!a) return b; return gcd(b % a, a); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &x[i], &y[i]); x[n] = x[0]; y[n] = y[0]; int d = gcd(abs(x[1] - x[0]), abs(y[1] - y[0])); int dx = (x[1] - x[0]) / d; int dy = (y[1] - y[0]) / d; int l = 0, r = d; for (int i = 1; i < n; i++) { long long tmp = vect(x[1] - x[0], y[1] - y[0], x[i + 1] - x[i], y[i + 1] - y[i]); if (tmp != 0) { double t = cross(x[0], y[0], x[0] + dx, y[0] + dy, x[i], y[i], x[i + 1], y[i + 1]); if (tmp < 0) { if (t < -1e-6) r = -1; else r = min(r, (int)(t + 0.0000001)); } else l = max(l, (int)(t + 0.9999999)); } else if (scal(x[1] - x[0], y[1] - y[0], x[i + 1] - x[i], y[i + 1] - y[i]) > 0) { printf("0\n"); return 0; } } printf("%d\n", max(0, r - l + 1)); return 0; }
#include <bits/stdc++.h> using namespace std; const int kMaxN = 1000; int n; inline int sgn(double x) { if (fabs(x) < 1E-7) return 0; if (x > 0.0) return 1; return -1; } struct Point { double x, y; Point() {} Point(double x_, double y_) : x(x_), y(y_) {} } pnt[kMaxN]; Point operator+(const Point &a, const Point &b) { return (Point(a.x + b.x, a.y + b.y)); } Point operator-(const Point &a, const Point &b) { return (Point(a.x - b.x, a.y - b.y)); } double det(const Point &a, const Point &b) { return (a.x * b.y - b.x * a.y); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%lf%lf", &pnt[i].x, &pnt[i].y); for (int i = 2; i < n; ++i) pnt[i].y -= pnt[0].y; pnt[0].y = pnt[1].y = 0; for (int i = 2; i < n; ++i) pnt[i].y = abs(pnt[i].y); if (pnt[0].x > pnt[1].x) { swap(pnt[0], pnt[1]); reverse(pnt + 2, pnt + n); } double lb = pnt[0].x, rb = pnt[1].x; for (int i = 2; i < n - 1; ++i) { int j = i + 1; Point v = pnt[j] - pnt[i]; if (sgn(v.y) == 0) { if (sgn(v.x) > 0) { printf("0\n"); return 0; } } else { double pos = pnt[i].x - v.x / v.y * pnt[i].y; if (sgn(det(v, Point(rb, 0.0) - pnt[i])) < 0) rb = min(rb, pos); if (sgn(det(v, Point(lb, 0.0) - pnt[i])) < 0) lb = max(lb, pos); if (sgn(lb - rb) > 0) { printf("0\n"); return 0; } } } int res = max(0, (int)(floor(rb) + 1E-7) - (int)(ceil(lb) + 1E-7) + 1); printf("%d\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double inf = 1e20; const double pi = acos(-1.0); const int maxp = 1611; int dblcmp(double d) { if (fabs(d) < eps) return 0; return d > eps ? 1 : -1; } inline double sqr(double x) { return x * x; } struct point { double x, y; point() {} point(double _x, double _y) : x(_x), y(_y){}; void input() { scanf("%lf%lf", &x, &y); } void output() { printf("%.2f %.2f\n", x, y); } bool operator==(point a) { return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0; } bool operator<(point a) const { return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) < 0 : x < a.x; } double len() { return sqrt(len2()); } double len2() { return x * x + y * y; } double distance(point p) { return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)); } point add(point p) { return point(x + p.x, y + p.y); } point sub(point p) { return point(x - p.x, y - p.y); } point mul(double b) { return point(x * b, y * b); } point div(double b) { return point(x / b, y / b); } double dot(point p) { return x * p.x + y * p.y; } double det(point p) { return x * p.y - y * p.x; } double rad(point a, point b) { point p = *this; return fabs(atan2(fabs(a.sub(p).det(b.sub(p))), a.sub(p).dot(b.sub(p)))); } point trunc(double r) { r /= len(); return point(x * r, y * r); } point rotleft() { return point(-y, x); } point rotright() { return point(y, -x); } point rotate(point p, double angle) { point v = this->sub(p); double c = cos(angle), s = sin(angle); return point(p.x + v.x * c - v.y * s, p.y + v.x * s + v.y * c); } }; struct line { point a, b; line() {} line(point _a, point _b) { a = _a; b = _b; } bool operator==(line v) { return (a == v.a) && (b == v.b); } line(point p, double angle) { a = p; if (dblcmp(angle - pi / 2) == 0) { b = a.add(point(0, 1)); } else { b = a.add(point(1, tan(angle))); } } line(double _a, double _b, double _c) { if (dblcmp(_a) == 0) { a = point(0, -_c / _b); b = point(1, -_c / _b); } else if (dblcmp(_b) == 0) { a = point(-_c / _a, 0); b = point(-_c / _a, 1); } else { a = point(0, -_c / _b); b = point(1, (-_c - _a) / _b); } } void input() { a.input(); b.input(); } void adjust() { if (b < a) swap(a, b); } double length() { return a.distance(b); } int relation(point p) { int c = dblcmp(p.sub(a).det(b.sub(a))); if (c < 0) return 1; if (c > 0) return 2; return 3; } bool pointonseg(point p) { return dblcmp(p.sub(a).det(b.sub(a))) == 0 && dblcmp(p.sub(a).dot(p.sub(b))) <= 0; } bool parallel(line v) { return dblcmp(b.sub(a).det(v.b.sub(v.a))) == 0; } int segcrossseg(line v) { int d1 = dblcmp(b.sub(a).det(v.a.sub(a))); int d2 = dblcmp(b.sub(a).det(v.b.sub(a))); int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a))); int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a))); if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 2; return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0 || d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0 || d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0 || d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0); } int linecrossseg(line v) { int d1 = dblcmp(b.sub(a).det(v.a.sub(a))); int d2 = dblcmp(b.sub(a).det(v.b.sub(a))); if ((d1 ^ d2) == -2) return 2; return (d1 == 0 || d2 == 0); } int linecrossline(line v) { if ((*this).parallel(v)) { return v.relation(a) == 3; } return 2; } point crosspoint(line v) { double a1 = v.b.sub(v.a).det(a.sub(v.a)); double a2 = v.b.sub(v.a).det(b.sub(v.a)); return point((a.x * a2 - b.x * a1) / (a2 - a1), (a.y * a2 - b.y * a1) / (a2 - a1)); } double dispointtoline(point p) { return fabs(p.sub(a).det(b.sub(a))) / length(); } double dispointtoseg(point p) { if (dblcmp(p.sub(b).dot(a.sub(b))) < 0 || dblcmp(p.sub(a).dot(b.sub(a))) < 0) { return min(p.distance(a), p.distance(b)); } return dispointtoline(p); } point lineprog(point p) { return a.add(b.sub(a).trunc(b.sub(a).dot(p.sub(a)) / b.distance(a))); } point symmetrypoint(point p) { point q = lineprog(p); return point(2 * q.x - p.x, 2 * q.y - p.y); } }; struct polygon { int n; point p[maxp]; line l[maxp]; void input() { for (int i = 0; i < n; i++) { p[i].input(); } } void add(point q) { p[n++] = q; } void getline() { for (int i = 0; i < n; i++) { l[i] = line(p[i], p[(i + 1) % n]); } } struct cmp { point p; cmp(const point &p0) { p = p0; } bool operator()(const point &aa, const point &bb) { point a = aa, b = bb; int d = dblcmp(a.sub(p).det(b.sub(p))); if (d == 0) { return dblcmp(a.distance(p) - b.distance(p)) < 0; } return d > 0; } }; void norm() { point mi = p[0]; for (int i = 1; i < n; i++) mi = min(mi, p[i]); sort(p, p + n, cmp(mi)); } void getconvex(polygon &convex) { int i, j, k; sort(p, p + n); convex.n = n; for (i = 0; i < min(n, 2); i++) { convex.p[i] = p[i]; } if (n <= 2) return; int &top = convex.n; top = 1; for (i = 2; i < n; i++) { while (top && convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0) top--; convex.p[++top] = p[i]; } int temp = top; convex.p[++top] = p[n - 2]; for (i = n - 3; i >= 0; i--) { while (top != temp && convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0) top--; convex.p[++top] = p[i]; } } bool isconvex() { bool s[3]; memset(s, 0, sizeof(s)); int i, j, k; for (i = 0; i < n; i++) { j = (i + 1) % n; k = (j + 1) % n; s[dblcmp(p[j].sub(p[i]).det(p[k].sub(p[i]))) + 1] = 1; if (s[0] && s[2]) return 0; } return 1; } int relationpoint(point q) { int i, j; for (i = 0; i < n; i++) { if (p[i] == q) return 3; } getline(); for (i = 0; i < n; i++) { if (l[i].pointonseg(q)) return 2; } int cnt = 0; for (i = 0; i < n; i++) { j = (i + 1) % n; int k = dblcmp(q.sub(p[j]).det(p[i].sub(p[j]))); int u = dblcmp(p[i].y - q.y); int v = dblcmp(p[j].y - q.y); if (k > 0 && u < 0 && v >= 0) cnt++; if (k < 0 && v < 0 && u >= 0) cnt--; } return cnt != 0; } int relationline(line u) { int i, j, k = 0; getline(); for (i = 0; i < n; i++) { if (l[i].segcrossseg(u) == 2) return 1; if (l[i].segcrossseg(u) == 1) k = 1; } if (!k) return 0; vector<point> vp; for (i = 0; i < n; i++) { if (l[i].segcrossseg(u)) { if (l[i].parallel(u)) { vp.push_back(u.a); vp.push_back(u.b); vp.push_back(l[i].a); vp.push_back(l[i].b); continue; } vp.push_back(l[i].crosspoint(u)); } } sort(vp.begin(), vp.end()); int sz = vp.size(); for (i = 0; i < sz - 1; i++) { point mid = vp[i].add(vp[i + 1]).div(2); if (relationpoint(mid) == 1) return 1; } return 2; } void convexcut(line u, polygon &po) { int i, j, k; int &top = po.n; top = 0; for (i = 0; i < n; i++) { int d1 = dblcmp(p[i].sub(u.a).det(u.b.sub(u.a))); int d2 = dblcmp(p[(i + 1) % n].sub(u.a).det(u.b.sub(u.a))); if (d1 >= 0) po.p[top++] = p[i]; if (d1 * d2 < 0) po.p[top++] = u.crosspoint(line(p[i], p[(i + 1) % n])); } } double getcircumference() { double sum = 0; int i; for (i = 0; i < n; i++) { sum += p[i].distance(p[(i + 1) % n]); } return sum; } double getarea() { double sum = 0; int i; for (i = 1; i < n - 1; i++) { sum += p[i].sub(p[0]).det(p[(i + 1) % n].sub(p[0])); } if (dblcmp(sum) < 0) { reverse(p, p + n); } return fabs(sum) / 2; } point getbarycentre() { point ret(0, 0); double area = 0; int i; for (i = 1; i < n - 1; i++) { double tmp = p[i].sub(p[0]).det(p[i + 1].sub(p[0])); if (dblcmp(tmp) == 0) continue; area += tmp; ret.x += (p[0].x + p[i].x + p[i + 1].x) / 3 * tmp; ret.y += (p[0].y + p[i].y + p[i + 1].y) / 3 * tmp; } if (dblcmp(area)) ret = ret.div(area); return ret; } double areaintersection(polygon po) {} double areaunion(polygon po) { return getarea() + po.getarea() - areaintersection(po); } }; struct circle { point p; double r; circle() {} circle(point _p, double _r) : p(_p), r(_r){}; circle(double x, double y, double _r) : p(point(x, y)), r(_r){}; circle(point a, point b, point c) { p = line(a.add(b).div(2), a.add(b).div(2).add(b.sub(a).rotleft())) .crosspoint( line(c.add(b).div(2), c.add(b).div(2).add(b.sub(c).rotleft()))); r = p.distance(a); } void input() { p.input(); scanf("%lf", &r); } bool operator==(circle v) { return ((p == v.p) && dblcmp(r - v.r) == 0); } double area() { return pi * sqr(r); } double circumference() { return 2 * pi * r; } int relation(point b) { double dst = b.distance(p); if (dblcmp(dst - r) < 0) return 2; if (dblcmp(dst - r) == 0) return 1; return 0; } int crossseg(line v) { return dblcmp(v.dispointtoseg(p) - r) <= 0; } int getcircle(point a, point b, double r, circle &c1, circle &c2) { circle x(a, r), y(b, r); int t = x.pointcrosscircle(y, c1.p, c2.p); if (!t) return 0; c1.r = c2.r = r; return t; } int pointcrossline(line v, point &p1, point &p2) { if (!(*this).crossseg(v)) return 0; point a = v.lineprog(p); double d = v.dispointtoline(p); d = sqrt(r * r - d * d); if (dblcmp(d) == 0) { p1 = a; p2 = a; return 1; } p1 = a.sub(v.b.sub(v.a).trunc(d)); p2 = a.add(v.b.sub(v.a).trunc(d)); return 2; } int relationcircle(circle v) { double d = p.distance(v.p); if (dblcmp(d - r - v.r) > 0) return 5; if (dblcmp(d - r - v.r) == 0) return 4; double l = fabs(r - v.r); if (dblcmp(d - r - v.r) < 0 && dblcmp(d - l) > 0) return 3; if (dblcmp(d - l) == 0) return 2; if (dblcmp(d - l) < 0) return 1; } int pointcrosscircle(circle v, point &p1, point &p2) { int rel = relationcircle(v); if (rel == 1 || rel == 5) return 0; if (rel == 2 || rel == 4) { p1 = p.add(v.p.sub(p).trunc(r)); p2 = p.add(v.p.sub(p).trunc(r)); return 1; } double d = p.distance(v.p); double l = (d + (sqr(r) - sqr(v.r)) / d) / 2; double h = sqrt(sqr(r) - sqr(l)); p1 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotleft().trunc(h))); p2 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotright().trunc(h))); return 2; } void tangentline(point q, line &u, line &v) { double d = p.distance(q); double l = sqr(r) / d; double h = sqrt(sqr(r) - sqr(l)); u = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotleft().trunc(h)))); v = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotright().trunc(h)))); } double areacircle(circle v) { int rel = relationcircle(v); if (rel >= 4) return 0.0; if (rel <= 2) return min(area(), v.area()); double d = p.distance(v.p); double hf = (r + v.r + d) / 2.0; double ss = 2 * sqrt(hf * (hf - r) * (hf - v.r) * (hf - d)); double a1 = acos((r * r + d * d - v.r * v.r) / 2.0 * r * d); a1 = a1 * r * r; double a2 = acos((v.r * v.r + d * d - r * r) / 2.0 * v.r * d); a2 = a2 * v.r * v.r; return a1 + a2 - ss; } double areatriangle(point a, point b) { point p1, p2; line l(a, b); int t = pointcrossline(l, p1, p2); double angle; if (t <= 1) { return fabs(area() * p.rad(a, b) / (2 * pi)); } bool b1 = l.pointonseg(p1); bool b2 = l.pointonseg(p2); if (b1 && b2) { return fabs(area() * p.rad(a, p1) / (2 * pi) + area() * p.rad(p2, b) / (2 * pi) + fabs(p1.sub(p).det(p2.sub(p))) / 2.0); } if (b1) { return fabs(area() * p.rad(a, p1) / (2 * pi) + fabs(p1.sub(p).det(b.sub(p)) / 2.0)); } if (b2) { return fabs(area() * p.rad(p2, b) / (2 * pi) + fabs(a.sub(p).det(p2.sub(p)) / 2.0)); } return fabs(a.sub(p).det(b.sub(p)) / 2.0); } double areapologon(polygon pl) { int i, j, k, l, m; double ans = 0; for (i = 0; i < pl.n; i++) { int j = (i + 1) % pl.n; if (dblcmp(pl.p[j].sub(p).det(pl.p[i].sub(p))) >= 0) { ans += areatriangle(pl.p[i], pl.p[j]); } else { ans -= areatriangle(pl.p[i], pl.p[j]); } } return fabs(ans); } }; const int maxn = 500; struct circles { circle c[maxn]; int n; circles() {} void add(circle cc) { c[n++] = cc; } void init_or() { bool v[maxn] = {0}; int i, j, k = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j) continue; if (c[i].relationcircle(c[j]) == 1) v[i] = 1; } } for (i = 0; i < n; i++) { if (!v[i]) c[k++] = c[i]; } n = k; } void init_and() { bool v[maxn] = {0}; int i, j, k = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j) continue; if (c[j].relationcircle(c[i]) == 1) v[i] = 1; } } for (i = 0; i < n; i++) { if (!v[i]) c[k++] = c[i]; } n = k; } }; struct halfplane : public line { double angle; halfplane() {} halfplane(point _a, point _b) { a = _a; b = _b; } void calcangle() { angle = atan2(b.y - a.y, b.x - a.x); } bool operator<(const halfplane &b) const { return angle < b.angle; } }; struct halfplanes { int n; halfplane hp[maxp]; point p[maxp]; int que[maxp]; int st, ed; void push(halfplane tmp) { hp[n++] = tmp; } void unique() { int m = 1, i; for (i = 1; i < n; i++) { if (dblcmp(hp[i].angle - hp[i - 1].angle)) hp[m++] = hp[i]; else if (dblcmp( hp[m - 1].b.sub(hp[m - 1].a).det(hp[i].a.sub(hp[m - 1].a)) > 0)) hp[m - 1] = hp[i]; } n = m; } bool halfplaneinsert() { int i; for (i = 0; i < n; i++) hp[i].calcangle(); sort(hp, hp + n); unique(); que[st = 0] = 0; que[ed = 1] = 1; p[1] = hp[0].crosspoint(hp[1]); for (i = 2; i < n; i++) { while (st < ed && dblcmp((hp[i].b.sub(hp[i].a).det(p[ed].sub(hp[i].a)))) < 0) ed--; while (st < ed && dblcmp((hp[i].b.sub(hp[i].a).det(p[st + 1].sub(hp[i].a)))) < 0) st++; que[++ed] = i; if (hp[i].parallel(hp[que[ed - 1]])) return false; p[ed] = hp[i].crosspoint(hp[que[ed - 1]]); } while ( st < ed && dblcmp(hp[que[st]].b.sub(hp[que[st]].a).det(p[ed].sub(hp[que[st]].a))) < 0) ed--; while (st < ed && dblcmp(hp[que[ed]] .b.sub(hp[que[ed]].a) .det(p[st + 1].sub(hp[que[ed]].a))) < 0) st++; if (st + 1 >= ed) return false; return true; } void getconvex(polygon &con) { p[st] = hp[que[st]].crosspoint(hp[que[ed]]); con.n = ed - st + 1; int j = st, i = 0; for (; j <= ed; i++, j++) { con.p[i] = p[j]; } } }; struct point3 { double x, y, z; point3() {} point3(double _x, double _y, double _z) : x(_x), y(_y), z(_z){}; void input() { scanf("%lf%lf%lf", &x, &y, &z); } void output() { printf("%.2lf %.2lf %.2lf", x, y, z); } bool operator==(point3 a) { return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0 && dblcmp(a.z - z) == 0; } bool operator<(point3 a) const { return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) == 0 ? dblcmp(z - a.z) < 0 : y < a.y : x < a.x; } double len() { return sqrt(len2()); } double len2() { return x * x + y * y + z * z; } double distance(point3 p) { return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y) + (p.z - z) * (p.z - z)); } point3 add(point3 p) { return point3(x + p.x, y + p.y, z + p.z); } point3 sub(point3 p) { return point3(x - p.x, y - p.y, z - p.z); } point3 mul(double d) { return point3(x * d, y * d, z * d); } point3 div(double d) { return point3(x / d, y / d, z / d); } double dot(point3 p) { return x * p.x + y * p.y + z * p.z; } point3 det(point3 p) { return point3(y * p.z - p.y * z, p.x * z - x * p.z, x * p.y - p.x * y); } double rad(point3 a, point3 b) { point3 p = (*this); return acos(a.sub(p).dot(b.sub(p)) / (a.distance(p) * b.distance(p))); } point3 trunc(double r) { r /= len(); return point3(x * r, y * r, z * r); } point3 rotate(point3 o, double r) {} }; struct line3 { point3 a, b; line3() {} line3(point3 _a, point3 _b) { a = _a; b = _b; } bool operator==(line3 v) { return (a == v.a) && (b == v.b); } void input() { a.input(); b.input(); } double length() { return a.distance(b); } double dispointtoline(point3 p) { return b.sub(a).det(p.sub(a)).len() / a.distance(b); } point3 lineprog(point3 p) { return a.add(b.sub(a).trunc(b.sub(a).dot(p.sub(a)) / b.distance(a))); } }; struct plane { point3 a, b, c, o; plane() {} plane(point3 _a, point3 _b, point3 _c) { a = _a; b = _b; c = _c; o = pvec(); } plane(double _a, double _b, double _c, double _d) { o = point3(_a, _b, _c); if (dblcmp(_a) != 0) { a = point3((-_d - _c - _b) / _a, 1, 1); } else if (dblcmp(_b) != 0) { a = point3(1, (-_d - _c - _a) / _b, 1); } else if (dblcmp(_c) != 0) { a = point3(1, 1, (-_d - _a - _b) / _c); } } void input() { a.input(); b.input(); c.input(); } point3 pvec() { return b.sub(a).det(c.sub(a)); } int crossline(line3 u, point3 &p) { double x = o.dot(u.b.sub(a)); double y = o.dot(u.a.sub(a)); double d = x - y; if (dblcmp(fabs(d)) == 0) return 0; p = u.a.mul(x).sub(u.b.mul(y)).div(d); return 1; } }; int count(line u) { int x = (int)ceil(u.a.x); int y = (int)floor(u.b.x); return y - x + 1; } halfplanes hps; polygon pl, con; int main() { int i, j, k, n, cc = 0, cas; cas = 1; while (cas--) { scanf("%d", &n); pl.n = n; pl.input(); line uu = line(pl.p[0], pl.p[1]); pl.getarea(); pl.getline(); hps.n = 0; for (i = 0; i < n; i++) { hps.push(halfplane(pl.l[i].a, pl.l[i].b)); } hps.halfplaneinsert(); hps.getconvex(pl); pl.getline(); for (i = 0; i < pl.n; i++) pl.l[i].adjust(); line u = uu; line seg; int b = 0; set<point> st; u.adjust(); for (i = 0; i < pl.n; i++) { if (!u.segcrossseg(pl.l[i])) continue; if (u.parallel(pl.l[i])) { if (dblcmp(u.a.sub(pl.l[i].a).det(u.a.sub(pl.l[i].b))) == 0) { b = 1; if (u.b < pl.l[i].a) { puts("0"); return 0; } if (pl.l[i].b < u.a) { puts("0"); return 0; } if (u.a < pl.l[i].a) { seg.a = pl.l[i].a; } else { seg.a = u.a; } if (pl.l[i].b < u.b) { seg.b = pl.l[i].b; } else { seg.b = u.b; } } } else { st.insert(u.crosspoint(pl.l[i])); } } if (!b && st.size() == 0) { puts("0"); continue; } if (!b) { set<point>::iterator ptr = st.begin(); if (st.size() == 2) { { seg.a = *ptr; ++ptr; seg.b = *ptr; } } else { seg.a = *ptr; seg.b = *ptr; } } cout << count(seg) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:64777216") const int size = 1001; struct tpo { long long x, y; } p[size]; long long vect(tpo a, tpo b) { return a.x * b.y - a.y * b.x; } long long vect(tpo a, tpo b, tpo c) { b.x -= a.x; b.y -= a.y; c.x -= a.x; c.y -= a.y; return vect(b, c); } int n; void Solve() { cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i].x >> p[i].y; } int res = 0; double L = min(p[0].x, p[1].x), R = max(p[0].x, p[1].x); for (int i = 2; i < n - 1; ++i) { int j = i + 1; if (p[i].y == p[j].y) { bool is1 = (p[i].x < p[j].x); bool is2 = p[1].x < p[0].x; if (is1 != is2) { L = R + 10; break; } } else { double t = (double)(p[0].y - p[i].y) / (p[j].y - p[i].y); double x = p[i].x + (p[j].x - p[i].x) * t; if (p[i].y > p[j].y) { R = min(R, x); } else L = max(L, x); } } if ((int)ceil(L) <= (int)floor(R)) { res = (int)floor(R) - (int)ceil(L) + 1; } cout << res << endl; } int main() { Solve(); return 0; }
#include <bits/stdc++.h> int n, x, y, l, r; struct point { int x, y; point(){}; point(int x, int y) : x(x), y(y){}; } P[1010]; inline point operator-(const point &a, const point &b) { return point(a.x - b.x, a.y - b.y); } inline int operator*(const point &a, const point &b) { long long ret = (long long)a.x * b.y - (long long)b.x * a.y; if (ret > 0) return 1; else if (ret < 0) return -1; else return 0; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &x, &y), P[i] = point(x, y); l = P[0].x; r = P[1].x; y = P[0].y; if (l > r) x = l, l = r, r = x; P[n] = P[0]; for (int i = 1; i < n; i++) { while (l <= r && (P[i + 1] - P[i]) * (point(l, y) - P[i]) > 0) l++; while (l <= r && (P[i + 1] - P[i]) * (point(r, y) - P[i]) > 0) r--; } printf("%d\n", r - l + 1); }
#include <bits/stdc++.h> using namespace std; int mod(int first) { if (first < 0) return -first; return first; } int area(pair<int, int> a, pair<int, int> b, pair<int, int> c) { long long aux = (long long)a.first * (b.second - c.second) + (long long)b.first * (c.second - a.second) + (long long)c.first * (a.second - b.second); if (aux < 0) return -1; return (aux > 0); } const int maxn = 1005; int n, i, normal, sgn, first, second, j, step; pair<int, int> a[maxn], left2, right2; int main() { cin >> n; for (i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second; a[n + 1] = a[1]; sgn = a[2].first - a[1].first; if (sgn < 0) sgn = -1; else sgn = 1; left2 = a[1]; right2 = a[2]; for (i = 2; i <= n; ++i) { first = area(a[i], a[i + 1], left2); second = area(a[i], a[i + 1], right2); if (first > 0 && second > 0) { cout << "0"; return 0; } if (first <= 0 && second <= 0) continue; if (first <= 0) { for (step = 1; step < mod(a[2].first - a[1].first); step <<= 1) ; for (j = left2.first; step; step >>= 1) if (area(a[i], a[i + 1], make_pair(j + step * sgn, left2.second)) <= 0) j += step * sgn; right2.first = j; continue; } for (step = 1; step < mod(a[2].first - a[1].first); step <<= 1) ; for (j = right2.first; step; step >>= 1) { if (area(a[i], a[i + 1], make_pair(j - step * sgn, left2.second)) <= 0) j -= step * sgn; left2.first = j; continue; } } cout << mod(right2.first - left2.first) + 1; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-12; const double big = 10002000; int sig(double k) { return (k < -eps) ? -1 : k > eps; } struct point { double x, y; point() { x = 0; y = 0; } point(double a, double b) : x(a), y(b) {} }; point pt[1010]; struct line { point a, b; double at; }; int top, bot; line l[1010], hp[1010]; struct polygon { int n; point p[1010]; } pol; int n; double mult(point a, point b, point c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } int cmp(const line &aa, const line &bb) { if (sig(aa.at - bb.at) == 0) return mult(bb.a, aa.b, bb.b) < 0; return aa.at < bb.at; } void intersection(point s1, point e1, point s2, point e2, point &pp) { double dot1, dot2; dot1 = mult(s2, e1, s1); dot2 = mult(e1, e2, s1); pp.x = (s2.x * dot2 + e2.x * dot1) / (dot2 + dot1); pp.y = (s2.y * dot2 + e2.y * dot1) / (dot2 + dot1); } int judge(line &a, line &b, line &c) { point a1; intersection(a.a, a.b, b.a, b.b, a1); return sig(mult(c.a, c.b, a1)) < 0; } double Area(polygon pol, int n) { double area = 0; for (int i = 0; i < pol.n; i++) { area += pol.p[i].x * pol.p[(i + 1) % n].y - pol.p[i].y * pol.p[(i + 1) % n].x; } area = fabs(area) / 2.0; return area; } void h_plane() { for (int i = 0; i < n; i++) { l[i].at = atan2(l[i].b.y - l[i].a.y, l[i].b.x - l[i].a.x); } sort(l, l + n, cmp); int num = 1; for (int i = 1; i < n; i++) { if (sig(l[i].at - l[i - 1].at) != 0) l[num++] = l[i]; } top = 2, bot = 1; hp[bot] = l[0]; hp[top] = l[1]; for (int i = 2; i < num; i++) { while (bot < top && judge(hp[top - 1], hp[top], l[i])) top--; while (bot < top && judge(hp[bot + 1], hp[bot], l[i])) bot++; hp[++top] = l[i]; } while (bot < top && judge(hp[bot + 1], hp[bot], hp[top])) bot++; while (bot < top && judge(hp[top - 1], hp[top], hp[bot])) top--; hp[--bot] = hp[top]; pol.n = 0; for (int i = bot + 1; i <= top; i++) { intersection(hp[i - 1].a, hp[i - 1].b, hp[i].a, hp[i].b, pol.p[pol.n++]); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { double x, y; scanf("%lf%lf", &x, &y); pt[i] = point(x, y); } for (int i = 0; i + 1 < n; ++i) { l[i].a = pt[i + 1]; l[i].b = pt[i]; } l[n - 1].a = pt[0]; l[n - 1].b = pt[n - 1]; h_plane(); int ans = -1; for (int i = 0; i < pol.n; ++i) { if (fabs(pol.p[i].y - pt[0].y) < eps && fabs(pol.p[(i + 1) % pol.n].y - pt[0].y) < eps) { int l, r; if (pol.p[i].x < pol.p[(i + 1) % pol.n].x) l = (int)ceil(pol.p[i].x), r = (int)floor(pol.p[(i + 1) % pol.n].x); else r = (int)floor(pol.p[i].x), l = (int)ceil(pol.p[(i + 1) % pol.n].x); ans = max(ans, r - l + 1); } } if (ans < 0) { for (int i = 0; i < pol.n; ++i) { if (fabs(pol.p[i].y - pt[0].y) < eps) { ans = 1; } } } printf("%d\n", max(ans, 0)); }
#include <bits/stdc++.h> using namespace std; int n, yy; int x[1100]; int y[1100]; int can[10000]; bool calc(int x1, int y1, int x2, int y2, double &tmp) { if (y1 == y2) return false; tmp = (double)(x2 - x1) / (y2 - y1) * (yy - y1) + x1; return true; } long long cha(long long x1, long long y1, long long x2, long long y2) { return x1 * y2 - x2 * y1; } int solve(int xx, int yy) { int i, j; long long tmp, tmp1; tmp = 0; for (i = 1; i < n; i++) { tmp1 = cha(x[i] - xx, y[i] - yy, x[i + 1] - xx, y[i + 1] - yy); if ((tmp1 < 0) && (tmp > 0) || (tmp1 > 0) && (tmp < 0)) return 0; if (tmp1 != 0) tmp = tmp1; } return 1; } int main() { int i; vector<int> a; double tmp; int ans; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d%d", &x[i], &y[i]); x[n] = x[0]; y[n] = y[0]; yy = y[0]; a.clear(); a.push_back(x[0]); a.push_back(x[1]); for (i = 2; i + 1 < n; i++) { if (calc(x[i], y[i], x[i + 1], y[i + 1], tmp)) { long long tmp1 = tmp; if ((tmp1 - 2 > x[0]) && (tmp1 - 2 < x[1]) || (tmp1 - 2 < x[0]) && (tmp1 - 2 > x[1])) a.push_back(tmp1 - 1); if ((tmp1 - 1 > x[0]) && (tmp1 - 1 < x[1]) || (tmp1 - 1 < x[0]) && (tmp1 - 1 > x[1])) a.push_back(tmp1 - 1); if ((tmp1 > x[0]) && (tmp1 < x[1]) || (tmp1 < x[0]) && (tmp1 > x[1])) a.push_back(tmp1); if ((tmp1 + 1 > x[0]) && (tmp1 + 1 < x[1]) || (tmp1 + 1 < x[0]) && (tmp1 + 1 > x[1])) a.push_back(tmp1 + 1); if ((tmp1 + 2 > x[0]) && (tmp1 + 2 < x[1]) || (tmp1 + 2 < x[0]) && (tmp1 + 2 > x[1])) a.push_back(tmp1 + 2); } } sort(a.begin(), a.end()); vector<int>::iterator y = unique(a.begin(), a.end()); a.erase(y, a.end()); ans = 0; for (i = 0; i < a.size(); i++) { can[i] = solve(a[i], yy); if (can[i] == 1) ans++; } for (i = 0; i + 1 < a.size(); i++) if ((can[i] == 1) && (can[i + 1] == 1)) { ans += a[i + 1] - a[i] - 1; } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf(1010101010); int n; struct pt { long long x, y; }; void rr(pt& p) { cin >> p.x >> p.y; } pt p[1005]; long long sgn(pt& p1, pt& p2, pt& p3) { return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); } long long fdiv(long long x, long long y) { return (x - (x % y + y) % y) / y; } long long cdiv(long long x, long long y) { if (x % y == 0) return x / y; return fdiv(x, y) + 1; } void must_neg(long long p1y, pt& p2, pt& p3, long long& lb, long long& ub) { long long b(p3.x * (p2.y - p3.y) + (p2.x - p3.x) * (p1y - p3.y)); long long a(p2.y - p3.y); if (a == 0) { if (b < 0) lb = inf; } if (a > 0) { ub = min(ub, fdiv(b, a)); } if (a < 0) { lb = max(lb, cdiv(-b, -a)); } } int main() { cin >> n; for (int i(0); i < (n); i++) rr(p[i]); long long lb(min(p[0].x, p[1].x)), ub(max(p[0].x, p[1].x)); long long the_y(p[0].y); for (int i(1); i < (n - 1); i++) { must_neg(the_y, p[i], p[(i + 1) % n], lb, ub); } cout << max(0LL, ub - lb + 1) << endl; }
#include <bits/stdc++.h> using namespace std; pair<double, double> V[1010]; pair<double, double> A, B, X, Y, O; double semn; int i, N; inline int cmp_real(const double &A, const double &B) { if (A + 1e-6 < B) return -1; if (B + 1e-6 < A) return 1; return 0; } inline double det(const pair<double, double> &A, const pair<double, double> &B, const pair<double, double> &C) { return semn * (A.first * B.second + B.first * C.second + C.first * A.second - A.second * B.first - B.second * C.first - C.second * A.first); } inline pair<double, double> Intersect(const pair<double, double> &A, const pair<double, double> &B, const double second) { double n, m; if (cmp_real(A.first, B.first) == 0) return make_pair(A.first, second); m = (A.second - B.second) / (A.first - B.first); n = A.second - m * A.first; return make_pair((second - n) / m, second); } int main() { scanf("%d", &N); for (i = 1; i <= N; ++i) scanf("%lf %lf", &V[i].first, &V[i].second); A = V[1]; B = V[2]; semn = 1.0; for (i = 3; i < N; ++i) { X = V[i]; Y = V[i + 1]; if (cmp_real(X.second, Y.second) == 0) { if (det(X, Y, A) >= -1e-6) { printf("0\n"); return 0; } continue; } O = Intersect(X, Y, A.second); if (cmp_real(A.first, O.first) * cmp_real(B.first, O.first) <= 0) { if (cmp_real(det(X, Y, A), 0.0) == 0) { if (det(X, Y, B) > 1e-6) B = A; continue; } if (det(X, Y, A) <= -1e-6) B = O; else A = O; } else if (det(X, Y, A) > 1e-6) { printf("0\n"); return 0; } } if (A.first > B.first) swap(A, B); int Ans = (int)floor(B.first); int Ans2 = (int)ceil(A.first); printf("%d\n", Ans - Ans2 + 1); return 0; }
#include <bits/stdc++.h> int n, x[1001], y[1001], i, j, dx, dy; double mr, nr, r, t; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &x[i]); scanf("%d", &y[i]); } if (x[0] < x[1]) { mr = x[1]; nr = x[0]; } else { mr = x[0]; nr = x[1]; } for (i = 2, j = 3; j < n; i = j++) { dx = x[j] - x[i]; dy = y[j] - y[i]; t = 1. * (y[0] - y[i]) * dx; r = dy ? t / dy + x[i] : 1e9; if (t - dy * (mr - x[i]) > 0 && r < mr) mr = r; if (t - dy * (nr - x[i]) > 0 && r > nr) nr = r; } mr = floor(mr) - ceil(nr); printf("%.0lf", mr < -1e-14 ? 0. : mr + 1.1); return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int ret = 0, p = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { ret = ret * 10 + ch - '0'; ch = getchar(); } return ret * p; } struct Point { long long x, y; }; Point operator-(Point a, Point b) { Point t; t.x = a.x - b.x; t.y = a.y - b.y; return t; } long long operator*(Point a, Point b) { return a.x * b.y - a.y * b.x; } long long operator^(Point a, Point b) { return a.x * b.x + a.y * b.y; } int n; Point a[1000 + 3]; void init() { n = read(); for (int i = 1; i <= n; i++) { a[i].x = read(); a[i].y = read(); } } inline long double max(long double a, long double b) { if (a > b) return a; return b; } inline long double min(long double a, long double b) { if (a < b) return a; return b; } void work() { long double l = a[1].x, r = a[2].x, p = 1; if (r < l) { long double t = r; r = l; l = t; p = -1; } for (int i = 3; i <= n - 1; i++) { long long D = (a[i + 1] - a[i]) * (a[2] - a[1]); long double Y = a[i + 1].y - a[i].y, X = a[i + 1].x - a[i].x; if (p * D > 0) { if (X == 0) { r = min(r, a[i].x); } else { long double k = Y / X; long double b = a[i].y - a[i].x * k; r = min((a[1].y - b) / k, r); } } if (p * D == 0) { long long S = (a[i + 1] - a[i]) ^ (a[2] - a[1]); if (S > 0) r = l - 1; } if (p * D < 0) { if (X == 0) { l = max(l, a[i].x); } else { long double k = Y / X; long double b = a[i].y - a[i].x * k; l = max((a[1].y - b) / k, l); } } } int L = l, R = r; if (l > L) L++; if (R > r) R--; int ans = R - L + 1; if (ans < 0) ans = 0; printf("%d\n", ans); } int main() { init(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; pair<double, double> operator+(pair<double, double> first, pair<double, double> second) { return make_pair(first.first + second.first, first.second + second.second); } pair<double, double> operator-(pair<double, double> first, pair<double, double> second) { return make_pair(first.first - second.first, first.second - second.second); } pair<double, double> operator*(pair<double, double> first, double second) { return make_pair(first.first * second, first.second * second); } pair<double, double> operator/(pair<double, double> first, double second) { return make_pair(first.first / second, first.second / second); } double Cross(pair<double, double> first, pair<double, double> second) { return first.first * second.second - first.second * second.first; } double Dot(pair<double, double> first, pair<double, double> second) { return first.first * second.first + first.second * second.second; } double Dis(pair<double, double> first, pair<double, double> second) { return Dot(first - second, first - second); } pair<double, double> d[1001]; int n; pair<double, double> getinter( const pair<pair<double, double>, pair<double, double> > &a, const pair<pair<double, double>, pair<double, double> > &b) { pair<double, double> u = a.first - b.first; double t = Cross(b.second, u) / Cross(a.second, b.second); return a.first + a.second * t; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lf%lf", &d[i].first, &d[i].second); pair<double, double> L = d[1], R = d[2]; pair<pair<double, double>, pair<double, double> > L1 = make_pair(L, R - L); for (int i = 3; i < n; i++) for (int j = i + 1; j <= n; j++) { pair<pair<double, double>, pair<double, double> > L2 = make_pair(d[i], d[j] - d[i]); if (fabs(Cross(L1.second, L2.second)) < eps) { if (Dot(L1.second, L2.second) > 0) { printf("0\n"); return 0; } continue; } pair<double, double> J = getinter(L1, L2); if (Dot(J - d[i], L2.second) < 0) R = Dot(L - R, J - R) < eps ? R : J; else L = Dot(R - L, J - L) < eps ? L : J; if (Dot(R - L, L1.second) < 0) { printf("0\n"); return 0; } } if (abs(L.first - R.first) < eps && abs(L.second - R.second) < eps) { printf("1\n"); return 0; } int ans = 0; pair<double, double> Now = d[1], Vec; if (L1.second.first == 0) Vec = make_pair(1, 0); else Vec = L1.second / fabs(L1.second.first); for (; Dot(Now - L, R - L) < 0; Now = Now + Vec) ; for (; Dot(L - Now, R - Now) - eps < 0; Now = Now + Vec) if (fabs(Now.second - round(Now.second)) < eps) ans++; printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n; pair<double, double> p[1011]; double calc(pair<double, double> a, pair<double, double> b, double y) { return a.first + (a.first - b.first) * (y - a.second) / (a.second - b.second); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lf%lf", &p[i].first, &p[i].second); if (p[1].first > p[2].first) { swap(p[1], p[2]); reverse(p + 3, p + n + 1); for (int i = 3; i <= n; ++i) p[i].second = p[1].second + p[1].second - p[i].second; } double L(p[1].first), R(p[2].first); for (int i = 3; i < n; ++i) if (p[i + 1].second > p[i].second) L = max(calc(p[i], p[i + 1], p[1].second), L); else if (p[i + 1].second < p[i].second) R = min(calc(p[i], p[i + 1], p[1].second), R); else if (p[i + 1].first > p[i].first) { puts("0"); return 0; } L += -1e-11 + 1, R += 1e-11 + 1; printf("%d\n", max(int(R) - int(L), 0)); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000"); using namespace std; struct Point { long long x, y; }; struct Line { long long a, b, c; }; Line makeLine(Point a, Point b) { Line l; l.a = b.y - a.y; l.b = a.x - b.x; l.c = -l.a * a.x - l.b * a.y; return l; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } void cut(Line l, Point a, Point v, long long& t1, long long& t2) { long long A = l.a * v.x + l.b * v.y; long long B = -l.c - l.a * a.x - l.b * a.y; if (A == 0) { if (B > 0) { t1 = 1; t2 = 0; return; } return; } if (A > 0) { if (B <= 0) return; t1 = max(t1, (B + A - 1) / A); } else { A = -A; B = -B; if (B < 0) { t2 = -1; return; } t2 = min(t2, B / A); } } int main() { int n; cin >> n; vector<Point> p(n); for (int i = 0; i < int(n); ++i) { cin >> p[i].x >> p[i].y; } long long t1 = 0; Point v = {p[1].x - p[0].x, p[1].y - p[0].y}; long long d = gcd(abs(v.x), abs(v.y)); v.x /= d; v.y /= d; long long t2 = d; for (int i = 1; i <= int(n - 1); ++i) { cut(makeLine(p[i], p[(i + 1) % n]), p[0], v, t1, t2); if (t1 > t2) break; } if (t1 > t2) { cout << 0 << endl; } else { cout << t2 - t1 + 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Point { double X, Y; }; const double eps = 1e-8; int Get() { char c; while (c = getchar(), c < '0' || c > '9') ; int X = c - 48; while (c = getchar(), c >= '0' && c <= '9') X = X * 10 + c - 48; return X; } int Floor(double X) { int T = (int)X; if (X - T > 1 - eps) T++; return T; } int Ceil(double X) { int T = (int)X; if (X - T > eps) T++; return T; } int main() { int N = Get(); static Point P[1000]; for (int i = 0; i < N; i++) { P[i].X = Get(); P[i].Y = Get(); } if (P[2].Y < P[0].Y) for (int i = 0; i < N; i++) P[i].Y = -P[i].Y; double H = P[0].Y, Left = P[0].X, Right = P[1].X; bool Flag = (Left > Right); if (Flag) swap(Left, Right); for (int i = 2; i + 1 < N; i++) { Point A = P[i], B = P[i + 1]; if (A.Y == B.Y && ((A.X < B.X) ^ Flag)) { Left = Right + 1; } if (A.Y == B.Y) continue; double X = ((A.X - B.X) * H - A.X * B.Y + B.X * A.Y) / (A.Y - B.Y); if ((A.Y > B.Y) ^ Flag) Left = max(Left, X); else Right = min(Right, X); } printf("%d\n", (Left > Right) ? 0 : Floor(Right) - Ceil(Left) + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1009; const long double eps = 1e-10; struct point { long double x, y; } p[maxn]; int n, l, r; long double L, R, Y, k, b; int fcmp(long double x) { if (fabs(x) < eps) return 0; return x > 0 ? 1 : -1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%Lf%Lf", &p[i].x, &p[i].y); p[n + 1] = p[1]; L = p[1].x; R = p[2].x; Y = p[1].y; if (p[3].y > Y) { swap(p[1], p[2]); for (int i = 3; i <= n; i++) if (p[i].y > Y) p[i].y = 2 * Y - p[i].y; for (int i = 3; i < n + 3 - i; i++) swap(p[i], p[n + 3 - i]); L = p[1].x; R = p[2].x; p[n + 1] = p[1]; } for (int i = 2; i <= n; i++) if (fcmp(p[i + 1].x - p[i].x) == 0) { if (fcmp(p[i + 1].y - p[i].y) < 0) R = min(R, p[i].x); else L = max(L, p[i].x); } else if (fcmp(p[i + 1].y - p[i].y) < 0) R = min(R, (Y - p[i].y + (p[i + 1].y - p[i].y) * p[i].x / (p[i + 1].x - p[i].x)) * (p[i + 1].x - p[i].x) / (p[i + 1].y - p[i].y)); else if (fcmp(p[i + 1].y - p[i].y) > 0) L = max(L, (Y - p[i].y + (p[i + 1].y - p[i].y) * p[i].x / (p[i + 1].x - p[i].x)) * (p[i + 1].x - p[i].x) / (p[i + 1].y - p[i].y)); else if (fcmp(p[i + 1].x - p[i].x) > 0) L = R + 1; l = ceil(L - eps); r = floor(R + eps); printf("%d\n", max(r - l + 1, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; int n; int x[1010], y[1010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d%d", &x[i], &y[i]); } double l = x[1], r = x[2], w; if (y[3] > y[1]) { l = x[2]; r = x[1]; } for (int i = 3; i < n; ++i) { if (y[i] == y[i + 1]) w = 1e15 * ((x[i] < x[i + 1]) ^ (y[3] < y[1]) ? 1 : -1); else w = x[i] + 1.0 * (x[i] - x[i + 1]) / (y[i] - y[i + 1]) * (y[1] - y[i]); if (y[i] < y[i + 1]) l = max(w, l); else r = min(w, r); } if (l > r + 1e-8) printf("0\n"); else printf("%d\n", (int)(r + 1 + 1e-8) - (int)(l + 1 - 1e-8)); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; void __never(int a) { printf("\nOPS %d", a); } struct T { long long x, y; }; int n; T p[1024]; long long vec(T A, T B, T C) { return (A.x - C.x) * (B.y - C.y) - (B.x - C.x) * (A.y - C.y); } void sol() { p[n + 1] = p[1]; long long be = p[1].x, en = p[2].x; if (be > en) swap(be, en); for (int a = (2); a <= (n); a++) { long long t1 = vec(p[a], p[a + 1], p[1]); long long t2 = vec(p[a], p[a + 1], p[2]); if (t1 > 0 && t2 > 0) { cout << 0; return; } if (t1 > 0 || t2 > 0) { if (t1 > 0) { long long mi = p[1].x, ma = p[2].x; long long d = (p[1].x < p[2].x ? 1 : -1); while (1) { if (mi == ma) { if (p[1].x < p[2].x) { if (be < mi) be = mi; } else { if (en > mi) en = mi; } break; } long long sr = (mi + ma) / 2; if (p[1].x > p[2].x) sr++; T tmp; tmp.x = sr; tmp.y = p[1].y; if (vec(p[a], p[a + 1], tmp) > 0) mi = sr + d; else ma = sr; } } else { long long mi = p[1].x, ma = p[2].x; long long d = (p[1].x < p[2].x ? 1 : -1); while (1) { if (mi == ma) { if (p[1].x > p[2].x) { if (be < mi) be = mi; } else { if (en > mi) en = mi; } break; } long long sr = (mi + ma) / 2; if (p[1].x > p[2].x) sr++; T tmp; tmp.x = sr + d; tmp.y = p[1].y; if (vec(p[a], p[a + 1], tmp) > 0) ma = sr; else mi = sr + d; } } } } if (be > en) cout << 0; else cout << (en - be + 1); } int main() { scanf("%d", &n); for (int a = (1); a <= (n); a++) { int x, y; scanf("%d%d", &x, &y); p[a].x = (long long)x + 1001000; p[a].y = (long long)y + 1001000; } sol(); return 0; }
#include <bits/stdc++.h> using namespace std; struct XD { double x, y; XD(double xx = 0, double yy = 0) : x(xx), y(yy) {} XD operator-(const XD& b) const { return XD(x - b.x, y - b.y); } double operator^(const XD& b) { return x * b.y - y * b.x; } void input() { scanf("%lf%lf", &x, &y); } } in[1010]; inline double ABS(double a) { return a > 0 ? a : -a; } double l = 1e10, r = -1e10; int n; void trys(double tmt) { int j; XD pt(tmt, in[0].y); for (j = 1; j < n; j++) { int k = (j + 1) % n; if (((pt - in[j]) ^ (in[k] - in[j])) < -1e-7) break; } if (j == n) { l = min(l, tmt); r = max(r, tmt); } } int main() { int i, j; scanf("%d", &n); for (i = 0; i < n; i++) in[i].input(); if (in[0].x > in[1].x) { for (i = 0; i < n; i++) { in[i].x *= -1; in[i].y *= -1; } } for (i = 2; i < n - 1; i++) { double d1 = ABS((in[1] - in[i + 1]) ^ (in[1] - in[i])); double d2 = ABS((in[0] - in[i + 1]) ^ (in[0] - in[i])); trys(((double)in[0].x * d1 + (double)in[1].x * d2) / (d1 + d2)); } trys(in[0].x); trys(in[1].x); if (l > r) puts("0"); else printf("%d\n", ((int)floor(r + 1e-7)) - ((int)floor(l - 1e-7))); }
#include <bits/stdc++.h> using namespace std; int Sig(double a) { return a < -1e-8 ? -1 : (a > 1e-8); } struct Point { double x, y; Point() {} Point(double x0, double y0) : x(x0), y(y0) {} void in() { scanf("%lf%lf", &x, &y); } void out() { printf("%.3f %.3f\n", x, y); } Point operator*(double t) { return Point(t * x, t * y); } double len() { return sqrt(x * x + y * y); } double operator*(Point pt) { return x * pt.y - y * pt.x; } double operator^(Point pt) { return pt.x * x + pt.y * y; } Point operator-(Point pt) { return Point(x - pt.x, y - pt.y); } Point operator+(Point pt) { return Point(x + pt.x, y + pt.y); } bool operator==(Point pt) { if (fabs(x - pt.x) < 1e-8 && fabs(y - pt.y) < 1e-8) return true; else return false; } } p[1010]; struct Line { Point p, q; Line() {} Line(Point p0, Point q0) : p(p0), q(q0) {} }; Point Intersect(Line u, Line v) { Point ret = u.p; double t = ((u.p.x - v.p.x) * (v.p.y - v.q.y) - (u.p.y - v.p.y) * (v.p.x - v.q.x)) / ((u.p.x - u.q.x) * (v.p.y - v.q.y) - (u.p.y - u.q.y) * (v.p.x - v.q.x)); ret.x += (u.q.x - u.p.x) * t; ret.y += (u.q.y - u.p.y) * t; return ret; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) p[i].in(); double l = min(p[0].x, p[1].x); double r = max(p[0].x, p[1].x); Point ori = p[1] - p[0]; bool flag = true; for (int i = 2; i <= n - 2 && flag; i++) { Point o = p[i + 1] - p[i]; if (Sig(o.y) == 0) { if (Sig(ori ^ o) == 1) flag = false; continue; } Point f = Intersect(Line(p[0], p[1]), Line(p[i], p[i + 1])); if (Sig(o.y > 0)) l = max(l, f.x); else r = min(r, f.x); } long long L = ceil(l); long long R = floor(r); if (!flag || L > R) cout << 0 << endl; else cout << R - L + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct point; struct line; struct segment; int n, m; struct point { double x, y; bool ok; point() : x(0.), y(0.), ok(false) {} point(double xx, double yy) : x(xx), y(yy), ok(true) {} point(line, line); point(line, segment); } a[1111]; double cross(const point &a, const point &b, const point &c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } struct line { double A, B, C; line(double Aa = 0., double Bb = 0., double Cc = 0.) : A(Aa), B(Bb), C(Cc) {} line(const point &p, const point &q) { double a = q.y - p.y, b = p.x - q.x, c = -a * p.x - b * p.y; A = a; B = b; C = c; } line(segment a); }; struct segment { point p, q; segment(point pp = point(), point qq = point()) : p(pp), q(qq) {} line median() { double mx = (p.x + q.x) * .5, my = (p.y + q.y) * .5; line tmp = line(p, q); swap(tmp.A, tmp.B); tmp.A = -tmp.A; tmp.C = -mx * tmp.A - my * tmp.B; return tmp; } }; line::line(segment a) { *this = line(a.p, a.q); } point::point(line a, line b) { if (fabs(b.B * a.A - a.B * b.A) < 1E-8) { x = y = 0.; ok = false; } else { y = (a.C * b.A - b.C * a.A) / (b.B * a.A - a.B * b.A); x = (a.C * b.B - b.C * a.B) / (a.B * b.A - b.B * a.A); ok = true; } } point::point(line a, segment b) { point tmp = point(a, line(b)); if (!tmp.ok || tmp.x > max(b.p.x, b.q.x) || tmp.x < min(b.p.x, b.q.x) || tmp.y > max(b.p.y, b.q.y) || tmp.y < min(b.p.y, b.q.y)) { x = y = 0.; ok = false; } else { *this = tmp; } } double dist2(const point &a, const point &b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); }; double dist(const point &a, const point &b) { return sqrt(dist2(a, b)); }; double u, v; int main() { scanf("%d", &n); for (int i = (1); i <= (n); ++i) scanf("%lf%lf", &a[i].x, &a[i].y); if (a[1].x > a[2].x) for (int i = (1); i <= (n); ++i) a[i].x = -a[i].x, a[i].y = -a[i].y; line o(a[1], a[2]); u = a[1].x; v = a[2].x; for (int i = (3); i <= (n - 1); ++i) { line l(a[i], a[i + 1]); point p(o, l); if (p.ok) { point q(p.x - 1, p.y); if (cross(a[i], a[i + 1], q) > 0) u = max(u, p.x); else v = min(v, p.x); } else if (cross(a[i], a[i + 1], a[1]) > 0) { u = v + 1; break; } } int l = ceil(u - 1E-9), r = floor(v + 1E-9); printf("%d\n", max(0, r - l + 1)); return 0; }
#include <bits/stdc++.h> using namespace std; long long fpm(long long b, long long e, long long m) { long long t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; } template <class T> inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; } template <class T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; } template <class T> inline T sqr(T x) { return x * x; } template <class edge> struct Graph { vector<vector<edge> > adj; Graph(int n) { adj.clear(); adj.resize(n + 5); } Graph() { adj.clear(); } void resize(int n) { adj.resize(n + 5); } void adde(int s, edge e) { adj[s].push_back(e); } int deg(int v) { return adj[v].size(); } vector<edge> &operator[](int t) { return adj[t]; } }; const int maxn = 1100; const double eps = 1e-7; struct crd { long double x, y, z; }; crd p[maxn]; crd operator^(crd a, crd b) { return (crd){a.y * b.z - b.y * a.z, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } long double operator*(crd a, crd b) { return a.x * b.x + a.y * b.y + a.z * b.z; } int sgn(long double t) { return t > -eps ? t > eps : -1; } bool in(crd hp, crd p) { return sgn((hp * p) / p.z) >= 0; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i].x >> p[i].y, p[i].z = 1; if (p[1].x > p[2].x) { reverse(p + 1, p + 1 + n); rotate(p + 1, p + n - 1, p + 1 + n); for (int i = 1; i <= n; ++i) p[i].y *= -1; } crd L = p[1], R = p[2], AB = p[1] ^ p[2]; for (int i = 2; i <= n; ++i) { crd a = p[i], b = p[i % n + 1], line = b ^ a, w = line ^ AB; bool b1 = false, b2 = false; if (!in(line, L)) L = w, b1 = true; if (!in(line, R)) R = w, b2 = true; if (b1 && b2) return cout << 0 << endl, 0; } int l = (int)(ceil(L.x / L.z)), r = (int)(floor(R.x / R.z)); cout << max(r - l + 1, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005; const double EPS = 1e-7; int n; struct Point { int x, y; } a[MAXN]; int mi, ma; int Y0; void Cut(Point a, Point b) { if (a.y == b.y) { if (b.x > a.x) mi = ma + 100; return; } double x = (-(double)b.x * a.y + (double)a.x * b.y - (double)(a.x - b.x) * Y0) / (b.y - a.y); long long xx; if (b.y < a.y) { if (x > 0) xx = (long long)(x + EPS); else xx = (long long)(x - 1 + EPS); if (xx < ma) ma = xx; } else { if (x > 0) xx = (long long)(x + 1 - EPS); else xx = (long long)(x - EPS); if (xx > mi) mi = xx; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); if (a[1].x > a[2].x) { swap(a[1], a[2]); for (int i = 1; i <= n; i++) a[i].y = -a[i].y; for (int i = 3; i <= n + 3 - i; i++) swap(a[i], a[n + 3 - i]); } mi = a[1].x, ma = a[2].x; Y0 = a[1].y; for (int i = 2; i < n; i++) Cut(a[i], a[i + 1]); printf("%d\n", max(0, ma - mi + 1)); return 0; }
#include <bits/stdc++.h> using namespace std; const double E = 1e-8; const int inf = (int)1e9; struct pt { double x, y; pt() {} pt(double _x, double _y) : x(_x), y(_y) {} }; pt v[2000]; double a, b; double h; void doit(pt p1, pt p2) { if (abs(p1.y - p2.y) < E) { if (p1.x < p2.x) { a = inf; b = -inf; return; } } else if (p1.y < p2.y) { double x = p1.x + (p2.x - p1.x) / (p2.y - p1.y) * (h - p1.y); a = max(a, x); } else { double x = p2.x + (p1.x - p2.x) / (p1.y - p2.y) * (h - p2.y); b = min(b, x); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf%lf", &v[i].x, &v[i].y); v[n] = v[0]; if (v[0].x > v[1].x) { for (int i = 0; i <= n; i++) v[i].x = -v[i].x; } if (v[0].y < v[2].y) { for (int i = 0; i <= n; i++) v[i].y = -v[i].y; } a = v[0].x; b = v[1].x; h = v[0].y; for (int i = 2; i < n; i++) { doit(v[i], v[i + 1]); } int a0 = (int)floor(a - E) + 1; int b0 = (int)floor(b + E); if (a0 <= b0) printf("%d", b0 - a0 + 1); else printf("0"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1010; int n, i, t, s, flag; long long x[N], y[N]; int main() { cin >> n; t = n; for (i = 1; i <= n; i++) cin >> x[i] >> y[i]; if (y[n] > y[1]) for (int i = (1); i <= (n); i++) y[i] = -y[i], x[i] = -x[i]; for (i = x[1]; i <= x[2] && t > 2; i++) for (; (x[t] - i) * (y[t - 1] - y[1]) - (x[t - 1] - i) * (y[t] - y[1]) >= 0 && t > 2; t--) ; s = i - 1; t = 2; for (i = x[2]; i >= x[1] && t < n; i--) for (; (x[t] - i) * (y[t + 1] - y[1]) - (x[t + 1] - i) * (y[t] - y[1]) <= 0 && t < n; t++) ; i++; if (i - s + 1 > 0) cout << i - s + 1 << endl; else puts("0"); return 0; }
#include <bits/stdc++.h> using namespace std; int N, x[1005], y[1005]; double L, R; int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) scanf("%d%d", &x[i], &y[i]); L = x[1], R = x[2], x[N + 1] = x[1], y[N + 1] = y[1]; if (L > R) swap(L, R); for (int i = 2; i <= N; i++) { double k, b, xx; if (y[i] == y[i + 1]) if ((x[i] < x[i + 1]) ^ (x[1] < x[2])) continue; else return puts("0"), 0; if (x[i] == x[i + 1]) xx = x[i]; else k = 1.0 * (y[i] - y[i + 1]) / (x[i] - x[i + 1]), b = y[i] - k * x[i], xx = (y[1] - b) / k; if (y[i] < y[i + 1]) L = max(L, xx); else R = min(R, xx); } printf("%d\n", L > R ? 0 : int(floor(R) - ceil(L) + 1)); }
#include <bits/stdc++.h> using namespace std; class rec { public: long double x, y; } p[2000]; int N, t, ans; long double xk, xb, k, b, l, r, tt; long double jiao(long double k1, long double b1, long double k2, long double b2) { long double x = (b1 - b2) / (k2 - k1); return x; } int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { double x, y; scanf("%lf%lf", &x, &y); p[i].x = x; p[i].y = y; } if (p[1].x > p[2].x) for (int i = 1; i <= N; i++) { p[i].x = -p[i].x; if (abs(p[i].x) == 0) p[i].x = 0; p[i].y = -p[i].y; if (abs(p[i].y) == 0) p[i].y = 0; } xk = 0; xb = p[1].y; l = p[1].x; r = p[2].x; for (int i = 3; i <= N; i++) { t = i % N + 1; if (p[t].y >= p[i].y) { if (p[i].x == p[t].x) { tt = p[i].x; } else { k = (p[i].y - p[t].y) / (p[i].x - p[t].x); b = p[i].y - k * p[i].x; if (k == xk) { if (p[t].x < p[i].x) tt = -1e9; else tt = 1e9; } else tt = jiao(k, b, xk, xb); } l = max(l, tt); } t = i - 1; if (p[t].y >= p[i].y) { if (p[i].x == p[t].x) { tt = p[i].x; } else { k = (p[i].y - p[t].y) / (p[i].x - p[t].x); b = p[i].y - k * p[i].x; if (k == xk) { if (p[t].x > p[i].x) tt = 1e9; else tt = -1e9; } else tt = jiao(k, b, xk, xb); } r = min(r, tt); } } int ll = int(l - 1e-6); if (ll < l - 1e-6) ll++; int rr = int(r + 1e-6); if (rr > r + 1e-6) rr--; ans = max(0, rr - ll + 1); printf("%d", ans); }
#include <bits/stdc++.h> using namespace std; const int mm = 1111; struct point { double x, y; point() {} point(double _x, double _y) : x(_x), y(_y) {} } g[mm]; point MakeVector(point &P, point &Q) { return point(Q.x - P.x, Q.y - P.y); } double CrossProduct(point P, point Q) { return P.x * Q.y - P.y * Q.x; } double MultiCross(point P, point Q, point R) { return CrossProduct(MakeVector(Q, P), MakeVector(Q, R)); } struct halfPlane { point s, t; double angle; halfPlane() {} halfPlane(point _s, point _t) : s(_s), t(_t) {} halfPlane(double sx, double sy, double tx, double ty) : s(sx, sy), t(tx, ty) {} void GetAngle() { angle = atan2(t.y - s.y, t.x - s.x); } } hp[mm], q[mm]; point IntersectPoint(halfPlane P, halfPlane Q) { double a1 = CrossProduct(MakeVector(P.s, Q.t), MakeVector(P.s, Q.s)); double a2 = CrossProduct(MakeVector(P.t, Q.s), MakeVector(P.t, Q.t)); return point((P.s.x * a2 + P.t.x * a1) / (a2 + a1), (P.s.y * a2 + P.t.y * a1) / (a2 + a1)); } bool cmp(halfPlane P, halfPlane Q) { if (fabs(P.angle - Q.angle) < 1e-8) return MultiCross(P.s, P.t, Q.s) > 0; return P.angle < Q.angle; } bool IsParallel(halfPlane P, halfPlane Q) { return fabs(CrossProduct(MakeVector(P.s, P.t), MakeVector(Q.s, Q.t))) < 1e-8; } void HalfPlaneIntersect(int n, int &m) { sort(hp, hp + n, cmp); int i, l = 0, r = 1; for (m = i = 1; i < n; ++i) if (hp[i].angle - hp[i - 1].angle > 1e-8) hp[m++] = hp[i]; n = m; m = 0; q[0] = hp[0], q[1] = hp[1]; for (i = 2; i < n; ++i) { if (IsParallel(q[r], q[r - 1]) || IsParallel(q[l], q[l + 1])) return; while (l < r && MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[r], q[r - 1])) > 0) --r; while (l < r && MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[l], q[l + 1])) > 0) ++l; q[++r] = hp[i]; } while (l < r && MultiCross(q[l].s, q[l].t, IntersectPoint(q[r], q[r - 1])) > 0) --r; while (l < r && MultiCross(q[r].s, q[r].t, IntersectPoint(q[l], q[l + 1])) > 0) ++l; q[++r] = q[l]; for (i = l; i < r; ++i) g[m++] = IntersectPoint(q[i], q[i + 1]); } int main() { int n, m; point a, b; while (cin >> n) { for (int i = 0; i < n; i++) cin >> g[i].x >> g[i].y; for (int i = 0; i < n; i++) { hp[i] = halfPlane(g[(i + 1) % n], g[i]); hp[i].GetAngle(); } a = g[0], b = g[1]; HalfPlaneIntersect(n, m); if (m < 3) { cout << 0 << endl; continue; } int ans = 0; if (a.x > b.x) swap(a, b); int f2 = 0, f1 = 0; for (int i = 0; i < m; i++) if (g[i].y == a.y && g[(i + 1) % m].y == a.y) { f2 = 1; a = g[i], b = g[(i + 1) % m]; break; } else if (g[i].y == a.y) f1 = 1; if (f1 && !f2) cout << 1 << endl; else if (f2) { if (a.x > b.x) swap(a, b); cout << int(b.x - a.x) + 1 << endl; } else cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = 3.1415926535; const double eps = 1e-6; int n, x[1010], y[1010]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", x + i, y + i); double l = x[0], r = x[1]; if (l > r) { for (int i = 0; i < n; i++) y[i] = -y[i], x[i] = -x[i]; l = x[0], r = x[1]; } for (int i = 3; i < n; i++) { double ml = x[0], mr = x[1]; int j = i - 1; if (y[i] == y[j]) { if (x[j] <= x[i]) r = -1e7; continue; } double dx = x[i] - x[j], dy = y[i] - y[j]; if (dy < 0) { r = min(r, x[i] + -dx * (y[0] - y[i]) / -dy); } else l = max(l, x[j] + dx * (y[0] - y[j]) / dy); } long long L = (long long)ceil(l), R = (long long)floor(r); if (R >= L) printf("%d\n", (int)(R - L + 1)); else puts("0"); }
#include <bits/stdc++.h> using namespace std; const int N = 1010; const double eps = 1e-8; struct Point { long long x, y; Point(long long xx = 0, long long yy = 0) : x(xx), y(yy){}; } pnt[N]; Point operator-(const Point &A, const Point &B) { return Point(A.x - B.x, A.y - B.y); } long long operator%(const Point &A, const Point &B) { return A.x * B.y - A.y * B.x; } long long operator*(const Point &A, const Point &B) { return A.x * B.x + A.y * B.y; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } pair<long long, long long> intersection(Point u1, Point u2, Point v1, Point v2) { pair<long long, long long> pp; pp.first = (u1 - v1) % (v1 - v2); pp.second = (u1 - u2) % (v1 - v2); if (pp.second < 0) pp.first *= -1, pp.second *= -1; long long d = gcd(abs(pp.first), abs(pp.second)); pp.first /= d; pp.second /= d; return pp; } long long floor(long long, long long); long long ceil(long long, long long); long long floor(long long a, long long b) { return a >= 0 ? a / b : -ceil(-a, b); } long long ceil(long long a, long long b) { return a >= 0 ? (a + b - 1) / b : -floor(-a, b); } int main() { int n; scanf("%d", &n); int i, j; for (i = 0; i < n; i++) scanf("%I64d%I64d", &pnt[i].x, &pnt[i].y); Point pa, pb; pa = pnt[0]; pb = pnt[1]; if (pb.x < pa.x) swap(pa, pb); long long rig = pb.x; long long lef = pa.x; for (i = 2; i < n - 1; i++) { long long cc = (pnt[i + 1] - pnt[i]) % (pb - pa); if (cc != 0) { pair<long long, long long> pp; pp = intersection(pa, pb, pnt[i], pnt[i + 1]); if (cc > 0) rig = min(rig, pa.x + floor((pb.x - pa.x) * pp.first, pp.second)); else lef = max(lef, pa.x + ceil((pb.x - pa.x) * pp.first, pp.second)); } else if ((pnt[i + 1] - pnt[i]) * (pnt[1] - pnt[0]) > 0) { cout << 0 << endl; return 0; } } cout << max(0LL, rig - lef + 1) << endl; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; struct Point { long long x, y; }; struct Line { long long a, b, c; }; int n; Point p[1005]; Line getLine(Point& p1, Point& p2) { Line l; l.a = p2.y - p1.y; l.b = p1.x - p2.x; l.c = -(l.a * p1.x + l.b * p1.y); return l; } int sign(Point& p, Line& l) { long long d = p.x * l.a + p.y * l.b + l.c; if (d > 0) return 1; if (d < 0) return -1; return 0; } long long ray1, ray2; long long down(long long, long long); long long up(long long a, long long b) { if (a < 0) return -down(-a, b); return (a + b - 1) / b; } long long down(long long a, long long b) { if (a < 0) return -up(-a, b); return a / b; } int main() { cin >> n; for (int i = 0; i < int(n); i++) { cin >> p[i].x >> p[i].y; } if (p[0].x > p[1].x) { for (int i = 0; i < int(n); i++) { p[i].x = -p[i].x; p[i].y = -p[i].y; } } p[n] = p[0]; long long h = p[0].y; ray1 = p[0].x, ray2 = p[1].x; for (int i = 0; i < int(n); i++) { Line l = getLine(p[i], p[i + 1]); if (l.a == 0) { if (sign(p[0], l) < 0) { cout << 0 << endl; return 0; } continue; } if (l.a > 0) { ray1 = max(ray1, up(-l.b * h - l.c, l.a)); } else { ray2 = min(ray2, down(l.b * h + l.c, -l.a)); } } long long ans = max(0LL, ray2 - ray1 + 1); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1; const long long LINF = ~(((long long)0x1) << 63) / 2; template <class T> bool get_max(T& a, T& b) { return b > a ? a = b, 1 : 0; } template <class T> bool get_min(T& a, T& b) { return b < a ? a = b, 1 : 0; } int n, x[1003], y[1003]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", x + i, y + i); double l, r; if (x[1] > x[2]) l = x[2], r = x[1]; else r = x[2], l = x[1]; for (int i = 3; i < n; ++i) { double dx = x[i + 1] - x[i]; double dy = y[i + 1] - y[i]; double d = y[1] - y[i]; double xx; if (!dy) xx = 1e10; else xx = x[i] + d / dy * dx; if (d * dx - dy * (r - x[i]) > 0 && r > xx) r = xx; if (d * dx - dy * (l - x[i]) > 0 && l < xx) l = xx; } r = floor(r), l = ceil(l); if (r >= l) printf("%.0lf\n", r - l + 1.1); else puts("0"); return 0; }
#include <bits/stdc++.h> int n, x[1003], y[1003]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", x + i, y + i); double l, r; if (x[1] > x[2]) l = x[2], r = x[1]; else r = x[2], l = x[1]; for (int i = 3; i < n; ++i) { double dx = x[i + 1] - x[i]; double dy = y[i + 1] - y[i]; double d = y[1] - y[i]; double xx; if (!dy) xx = 1e10; else xx = x[i] + d / dy * dx; if (d * dx - dy * (r - x[i]) > 0 && r > xx) r = xx; if (d * dx - dy * (l - x[i]) > 0 && l < xx) l = xx; } r = floor(r), l = ceil(l); if (r >= l) printf("%.0lf\n", r - l + 1.1); else puts("0"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 11111; double Area, Length; double P_x_min, P_x_max; double P_y_min, P_y_max; struct Point { double x, y; Point() {} Point(double _x, double _y) : x(_x), y(_y) {} } p[N]; Point MakeVector(Point &P, Point &Q) { return Point(Q.x - P.x, Q.y - P.y); } double CrossProduct(Point P, Point Q) { return P.x * Q.y - P.y * Q.x; } double MultiCross(Point P, Point Q, Point R) { return CrossProduct(MakeVector(Q, P), MakeVector(Q, R)); } struct halfPlane { Point s, t; double angle; halfPlane() {} halfPlane(Point _s, Point _t) : s(_s), t(_t) {} halfPlane(double sx, double sy, double tx, double ty) : s(sx, sy), t(tx, ty) {} void GetAngle() { angle = atan2(t.y - s.y, t.x - s.x); } } hp[N], q[N]; Point IntersectPoint(halfPlane P, halfPlane Q) { double a1 = CrossProduct(MakeVector(P.s, Q.t), MakeVector(P.s, Q.s)); double a2 = CrossProduct(MakeVector(P.t, Q.s), MakeVector(P.t, Q.t)); return Point((P.s.x * a2 + P.t.x * a1) / (a2 + a1), (P.s.y * a2 + P.t.y * a1) / (a2 + a1)); } bool cmp(halfPlane P, halfPlane Q) { if (fabs(P.angle - Q.angle) < 1e-8) return MultiCross(P.s, P.t, Q.s) > 0; return P.angle < Q.angle; } bool IsParallel(halfPlane P, halfPlane Q) { return fabs(CrossProduct(MakeVector(P.s, P.t), MakeVector(Q.s, Q.t))) < 1e-8; } void HalfPlaneIntersect(int n, int &m) { sort(hp, hp + n, cmp); int i, l = 0, r = 1; for (m = i = 1; i < n; ++i) if (hp[i].angle - hp[i - 1].angle > 1e-8) hp[m++] = hp[i]; n = m; m = 0; q[0] = hp[0], q[1] = hp[1]; for (i = 2; i < n; ++i) { if (IsParallel(q[r], q[r - 1]) || IsParallel(q[l], q[l + 1])) return; while (l < r && MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[r], q[r - 1])) > 0) --r; while (l < r && MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[l], q[l + 1])) > 0) ++l; q[++r] = hp[i]; } while (l < r && MultiCross(q[l].s, q[l].t, IntersectPoint(q[r], q[r - 1])) > 0) --r; while (l < r && MultiCross(q[r].s, q[r].t, IntersectPoint(q[l], q[l + 1])) > 0) ++l; q[++r] = q[l]; for (i = l; i < r; ++i) p[m++] = IntersectPoint(q[i], q[i + 1]); } void Solve(Point *p, int n) { int i, j, m; Point a, b; p[n] = p[0]; for (i = 0; i < n; i++) { hp[i] = halfPlane(p[(i + 1) % n], p[i]); hp[i].GetAngle(); } a = p[0], b = p[1]; HalfPlaneIntersect(n, m); Area = 0; Length = 0; P_x_min = P_x_max = 0; P_y_min = P_y_max = 0; if (m > 2) { if (a.x > b.x) swap(a, b); int f2 = 0, f1 = 0; for (int i = 0; i < m; i++) { if (p[i].y == a.y && p[(i + 1) % m].y == a.y) { f2 = 1; a = p[i]; b = p[(i + 1) % m]; break; } else if (p[i].y == a.y) f1 = 1; } if (f1 && !f2) Length = 1; else if (f2) { if (a.x > b.x) swap(a, b); Length = (int)(b.x - a.x) + 1; } } } int main() { int n, m; Point a, b; while (cin >> n) { for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; Solve(p, n); cout << Length << endl; } return 0; }
#include <bits/stdc++.h> int n, x[1001], y[1001], i, j, dx, dy; double mr, nr, r, t; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &x[i]); scanf("%d", &y[i]); } if (x[0] < x[1]) { mr = x[1]; nr = x[0]; } else { mr = x[0]; nr = x[1]; } for (i = 2; i < n; i++) { for (j = i + 1; j < n; j++) { dx = x[j] - x[i]; dy = y[j] - y[i]; if (!dy) r = 1e100; else r = 1. * (y[0] - y[i]) / dy * dx + x[i]; if (1. * dx * (y[0] - y[i]) - dy * (mr - x[i]) > 0 && r < mr) mr = r; if (1. * dx * (y[0] - y[i]) - dy * (nr - x[i]) > 0 && r > nr) nr = r; } } mr = floor(mr) - ceil(nr); if (i < n || mr < -1e-12) printf("0"); else printf("%.0lf", mr + 1e-12 + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 11111; double Area, Length; double P_x_min, P_x_max; double P_y_min, P_y_max; struct Point { double x, y; Point() {} Point(double _x, double _y) : x(_x), y(_y) {} } p[N]; Point MakeVector(Point &P, Point &Q) { return Point(Q.x - P.x, Q.y - P.y); } double CrossProduct(Point P, Point Q) { return P.x * Q.y - P.y * Q.x; } double MultiCross(Point P, Point Q, Point R) { return CrossProduct(MakeVector(Q, P), MakeVector(Q, R)); } struct halfPlane { Point s, t; double angle; halfPlane() {} halfPlane(Point _s, Point _t) : s(_s), t(_t) {} halfPlane(double sx, double sy, double tx, double ty) : s(sx, sy), t(tx, ty) {} void GetAngle() { angle = atan2(t.y - s.y, t.x - s.x); } } hp[N], q[N]; Point IntersectPoint(halfPlane P, halfPlane Q) { double a1 = CrossProduct(MakeVector(P.s, Q.t), MakeVector(P.s, Q.s)); double a2 = CrossProduct(MakeVector(P.t, Q.s), MakeVector(P.t, Q.t)); return Point((P.s.x * a2 + P.t.x * a1) / (a2 + a1), (P.s.y * a2 + P.t.y * a1) / (a2 + a1)); } bool cmp(halfPlane P, halfPlane Q) { if (fabs(P.angle - Q.angle) < 1e-8) return MultiCross(P.s, P.t, Q.s) > 0; return P.angle < Q.angle; } bool IsParallel(halfPlane P, halfPlane Q) { return fabs(CrossProduct(MakeVector(P.s, P.t), MakeVector(Q.s, Q.t))) < 1e-8; } void HalfPlaneIntersect(int n, int &m) { sort(hp, hp + n, cmp); int i, l = 0, r = 1; for (m = i = 1; i < n; ++i) if (hp[i].angle - hp[i - 1].angle > 1e-8) hp[m++] = hp[i]; n = m; m = 0; q[0] = hp[0], q[1] = hp[1]; for (i = 2; i < n; ++i) { if (IsParallel(q[r], q[r - 1]) || IsParallel(q[l], q[l + 1])) return; while (l < r && MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[r], q[r - 1])) > 0) --r; while (l < r && MultiCross(hp[i].s, hp[i].t, IntersectPoint(q[l], q[l + 1])) > 0) ++l; q[++r] = hp[i]; } while (l < r && MultiCross(q[l].s, q[l].t, IntersectPoint(q[r], q[r - 1])) > 0) --r; while (l < r && MultiCross(q[r].s, q[r].t, IntersectPoint(q[l], q[l + 1])) > 0) ++l; q[++r] = q[l]; for (i = l; i < r; ++i) p[m++] = IntersectPoint(q[i], q[i + 1]); } void Solve(Point *p, int n, int &m) { int i, j; Point a, b; p[n] = p[0]; for (i = 0; i < n; i++) { hp[i] = halfPlane(p[(i + 1) % n], p[i]); hp[i].GetAngle(); } a = p[0], b = p[1]; HalfPlaneIntersect(n, m); Area = 0; Length = 0; P_x_min = P_x_max = 0; P_y_min = P_y_max = 0; if (m > 2) { if (a.x > b.x) swap(a, b); int f2 = 0, f1 = 0; for (int i = 0; i < m; i++) { if (p[i].y == a.y && p[(i + 1) % m].y == a.y) { f2 = 1; a = p[i]; b = p[(i + 1) % m]; break; } else if (p[i].y == a.y) f1 = 1; } if (f1 && !f2) Length = 1; else if (f2) { if (a.x > b.x) swap(a, b); Length = (int)(b.x - a.x) + 1; } } if (m > 2) { p[m] = p[0]; for (i = 0; i < m; ++i) Area += CrossProduct(p[i], p[i + 1]); if (Area < 0) Area = -Area; } Area /= 2.0; } int main() { int n, m, t = 1; Point a, b; while (cin >> n) { if (n == 0) break; for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y; Solve(p, n, m); cout << Length << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-6; struct pos { double x, y; }; struct vec { double x, y; }; struct seg { pos a, b; }; long long sign(double x) { return x < -eps ? -1 : x > eps ? 1 : 0; } double dot(vec a, vec b) { return a.x * b.x + a.y * b.y; } double cross(vec a, vec b) { return a.x * b.y - a.y * b.x; } vec fwd(pos a, pos b) { return {b.x - a.x, b.y - a.y}; } void mkang(pos src, pos A, pos B) {} pos operator+(pos p, vec v) { return {p.x + v.x, p.y + v.y}; } vec operator*(vec v, double t) { return {v.x * t, v.y * t}; } bool checkInt(seg a, seg b) { return sign(cross(fwd(a.a, a.b), fwd(b.a, b.b))) != 0; } void prt(pos p) { cout << "p(" << p.x << "," << p.y << ") "; } void prt(vec p) { cout << "v(" << p.x << "," << p.y << ") "; } pos segIntSeg(seg a, seg b) { double t = cross(fwd(a.a, b.a), fwd(a.a, a.b)) / cross(fwd(a.a, a.b), fwd(b.a, b.b)); return b.a + fwd(b.a, b.b) * t; } signed main() { ios::sync_with_stdio(0); cin.tie(0); ; long long n; cin >> n; vector<pos> v(n); for (long long i = 0; i < n; i++) cin >> v[i].x >> v[i].y; seg ln = {v[0], v[1]}; if (v[0].x > v[1].x) { for (long long i = 0; i < n; i++) { v[i].x = 1000000 - v[i].x; } } double L = v[0].x, R = v[1].x; for (long long i = 2; i < n; i++) { for (long long j = 2; j < i; j++) { seg ln2 = {v[i], v[j]}; if (!checkInt(ln, ln2)) { if (v[j].x < v[i].x) R = L - 1; continue; } pos p = segIntSeg(ln, ln2); if (sign(dot(fwd(v[i], v[j]), fwd(v[i], p))) < 0) continue; R = min(R, p.x); } for (long long j = i + 1; j < n; j++) { seg ln2 = {v[i], v[j]}; if (!checkInt(ln, ln2)) { if (v[j].x > v[i].x) L = R + 1; continue; } pos p = segIntSeg(ln, ln2); if (sign(dot(fwd(v[i], v[j]), fwd(v[i], p))) < 0) continue; L = max(L, p.x); } } long long cnt = floor(R) - ceil(L) + 1; cout << max(cnt, 0LL) << '\n'; }
#include <bits/stdc++.h> long long x[1010], y[1010], MaxL, MinR, L, R, K, B; int n; long long XJ(int a, int b, int c) { return (x[b] - x[a]) * (y[c] - y[a]) - (x[c] - x[a]) * (y[b] - y[a]); } int main() { int i, j; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%I64d%I64d", x + i, y + i); for (i = 0; i < n; i++) if (y[i] > y[0]) break; if (i < n) { for (i = 1; i < n; i++) { x[i] = 2 * x[0] - x[i]; y[i] = 2 * y[0] - y[i]; } } x[n] = x[0]; y[n] = y[0]; MaxL = x[0]; MinR = x[1]; for (i = 2; i < n; i++) { L = R = -1; for (j = i - 1; j >= 1; j--) if (R == -1 || XJ(i, R, j) > 0) R = j; for (j = i + 1; j <= n; j++) if (L == -1 || XJ(i, L, j) < 0) L = j; K = y[i] - y[L]; B = (y[0] - y[L]) * (x[i] - x[L]) + x[L] * (y[i] - y[L]); if (K) { if (K < 0) { K = -K; B = -B; } else MaxL = 1000000000; L = B / K; if (L * K < B) L++; if (L > MaxL) MaxL = L; } else if (B) MaxL = 1000000000; K = y[i] - y[R]; B = (y[0] - y[R]) * (x[i] - x[R]) + x[R] * (y[i] - y[R]); if (K) { if (K < 0) { K = -K; B = -B; } else MinR = -1000000000; R = B / K; if (R * K > B) R--; if (R < MinR) MinR = R; } else if (B) MinR = -1000000000; } if (MaxL > MinR) printf("0\n"); else printf("%I64d\n", MinR - MaxL + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; const double eps = 1e-6; int x[maxn], y[maxn], n; int main() { scanf("%d", &n); for (int a = 1; a <= n; a++) scanf("%d%d", &x[a], &y[a]), x[a] += 20, y[a] += 20; x[n + 1] = x[1]; y[n + 1] = y[1]; int l = min(x[1], x[2]), r = max(x[1], x[2]); for (int a = 1; a <= n; a++) { long long A = y[a + 1] - y[a]; long long B = x[a] - x[a + 1]; long long C = -A * x[a] - B * y[a]; if (A == 0) { if (B * y[1] + C < 0) { printf("0\n"); return 0; } } else { if (A > 0) { int nowx = (int)((double)(-B * y[1] - C) / A + 1 - eps); l = max(l, nowx); } else { int nowx = (int)((double)(-B * y[1] - C) / A + eps); r = min(r, nowx); } } } if (l > r) printf("0\n"); else printf("%d\n", r - l + 1); return 0; }
#include <bits/stdc++.h> using namespace std; double helen(double a, double b, double c) { double p = (a + b + c) / 2; return sqrt(p * (p - a) * (p - b) * (p - c)); } struct point { double x, y; point() {} point(double x, double y) : x(x), y(y) {} } p[1008], q[5]; double xmult(point a, point b, point o) { return ((a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)); } int n; int len; int next(int i) { if (i == (n - 1)) return 0; else return (i + 1); } point intersection(point u1, point u2, point v1, point v2) { point ret = u1; double t = ((u1.x - v1.x) * (v1.y - v2.y) - (u1.y - v1.y) * (v1.x - v2.x)) / ((u1.x - u2.x) * (v1.y - v2.y) - (u1.y - u2.y) * (v1.x - v2.x)); ret.x += (u2.x - u1.x) * t; ret.y += (u2.y - u1.y) * t; return ret; } bool cut() { point ret[5]; q[2] = q[0]; len = 1; for (int i = 0; i < n; i++) { int cnt = -1; for (int j = 0; j <= len; j++) { if (xmult(p[next(i)], q[j], p[i]) > -1e-6) { ++cnt; ret[cnt] = q[j]; } if (xmult(p[next(i)], q[j], p[i]) * xmult(p[next(i)], q[j + 1], p[i]) < -1e-6) { point pp = intersection(p[i], p[next(i)], q[j], q[j + 1]); ++cnt; ret[cnt] = pp; } } if (cnt == -1) return false; int k; for (k = 0; k <= cnt; k++) { q[k] = ret[k]; } q[cnt + 1] = q[0]; len = cnt; } return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf %lf", &p[i].x, &p[i].y); } q[0] = p[0]; q[1] = p[1]; reverse(p, p + n); if (cut() == false) { printf("0\n"); } else { if (len == 0) { if ((((q[0].x - (int)(q[0].x + 1e-6)) > 0 ? (q[0].x - (int)(q[0].x + 1e-6)) : -(q[0].x - (int)(q[0].x + 1e-6))) < 1e-6)) { puts("1"); } else { puts("0"); } } else { double a = max(q[0].x, q[1].x); double b = min(q[0].x, q[1].x); printf("%d\n", (int)((int)a - b) + 1); } } }
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; int x[maxn], y[maxn]; int n; long long cross(int x1, int y1, int x2, int y2) { return (long long)x1 * y2 - (long long)x2 * y1; } int main() { int i, l, r; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d%d", x + i, y + i); l = x[0]; r = x[1]; x[n] = x[0]; y[n] = y[0]; if (l > r) swap(l, r); for (i = 0; i < n; ++i) { while (l <= r && cross(x[i + 1] - x[i], y[i + 1] - y[i], l - x[i], y[0] - y[i]) > 0) ++l; while (l <= r && cross(x[i + 1] - x[i], y[i + 1] - y[i], r - x[i], y[0] - y[i]) > 0) --r; } printf("%d\n", r - l + 1); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; const int INF = (int)1E9; const long long INF64 = (long long)1E18; const long double EPS = 1E-8; const long double PI = 3.1415926535897932384626433832795; struct point { long long x, y; int idx; bool operator<(const point &a) const { return idx < a.idx; } }; int sign(long long x) { if (x > 0) return +1; if (x < 0) return -1; return 0; } long long up(long long a, long long b) { if (sign(a) * sign(b) < 0) return 0; if (sign(a) < 0) a *= -1; if (sign(b) < 0) b *= -1; long long ans = a / b; if (a % b != 0) ans++; return ans; } long long down(long long a, long long b) { if (sign(a) * sign(b) < 0) return -1; if (sign(a) < 0) a *= -1; if (sign(b) < 0) b *= -1; long long ans = a / b; return ans; } int n; point a[10010]; void solve(int v, int u, long long &l, long long &r) { long long A = a[u].y - a[v].y; long long B = a[v].x - a[u].x; long long C = -(a[v].x * A + a[v].y * B); if (A == 0) { l = 0; r = INF; if (sign(B) != sign(a[0].y - a[u].y)) r = -INF; return; } if (A > 0) { l = up(-B * a[0].y - C, A); r = INF; return; } if (A < 0) { l = -INF; r = down(-B * a[0].y - C, A); } } int main() { cin >> n; for (int i = 0; i < (int)(n); i++) { cin >> a[i].x >> a[i].y; a[i].idx = i; } long long l = min(a[0].x, a[1].x), r = max(a[0].x, a[1].x); for (int i = 1; i < n - 1; i++) { int j = i + 1; long long lf, rg; solve(i, j, lf, rg); l = max(l, lf); r = min(r, rg); } cout << max(0LL, r - l + 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, x[1 << 10], y[1 << 10]; long long test(double x) { if (x < 0) { return (test(-x + 1.0 - 0.000000001)) * -1; } return x; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> x[i] >> y[i]; long long minx = min(x[1], x[2]); long long maxx = max(x[1], x[2]); for (int i = 3; i < n; i++) { if (y[i] == y[i + 1]) { if (y[3] < y[1] && x[i + 1] > x[i]) { minx = 1e9; } if (y[3] > y[1] && x[i + 1] < x[i]) { minx = 1e9; } continue; } long long disty = y[i] - y[i + 1]; long long distx = x[i + 1] - x[i]; long long distcom = x[i] * disty + y[i] * distx; double inter = (distcom - y[1] * distx) * 1.0 / disty; if (y[i] < y[i + 1]) { minx = max(minx, test(inter + 1 - 0.000000001)); } else { maxx = min(maxx, test(inter)); } } if (minx > maxx) { minx = maxx + 1; } cout << maxx - minx + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int c = 1024; const int cmax = 1000001; const double eps = 1e-8; struct point { int x, y; }; int lr, rr; point a[c]; int n; inline int sign(const long long &a) { if (a > 0) return 1; else if (a < 0) return -1; else return 0; } inline int sign(const double &a) { if (a > 0) return 1; else if (a < 0) return -1; else return 0; } inline int rndl(double a) { int s = sign(a); int r1 = int(floor(fabs(a) + eps)); int r2 = int(ceil(fabs(a) - eps)); if (s > 0) return r1; else return -r2; } inline int rndr(double a) { int s = sign(a); int r1 = int(floor(fabs(a) + eps)); int r2 = int(ceil(fabs(a) - eps)); if (s > 0) return r2; else return -r1; } inline double sqr(double a) { return a * a; } inline double dist(point a, point b) { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); } inline double an(point a, point b, point c, point d) { return (double(b.x - a.x) * (d.x - c.x) + double(b.y - a.y) * (d.y - c.y)) / (dist(a, b) * dist(c, d)); } int main() { scanf("%d", &n); int i; for (i = 1; i <= n; ++i) scanf("%d%d", &a[i].x, &a[i].y); lr = min(a[1].x, a[2].x); rr = max(a[1].x, a[2].x); int lf, rg, md; for (i = 3; i < n; ++i) { double ca = an(a[1], a[2], a[i], a[i % n + 1]); if (ca == 1) { rr = lr - 1; break; } if (ca == -1) continue; double px = -(double(a[i % n + 1].x - a[i].x) * a[1].y + double(a[i].x) * a[i % n + 1].y - double(a[i].y) * a[i % n + 1].x) / (a[i].y - a[i % n + 1].y); if (!(abs(a[i].y - a[1].y) > abs(a[i % n + 1].y - a[1].y) ^ a[1].x > a[2].x)) rr = min(rr, rndl(px)); else lr = max(lr, rndr(px)); } printf("%d\n", max(rr - lr + 1, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 1002; const double EPS = 1e-8; const double INF = 1e8; inline int DBLCMP(double d) { if (fabs(d) < EPS) return 0; return d > 0 ? 1 : -1; } struct SPoint { double x, y; SPoint() {} SPoint(double X, double Y) : x(X), y(Y) {} }; int N; SPoint P[MaxN + 1]; double L, R; int Solve() { L = -1; R = INF; double dx, dy, px, a, b; for (int i = 1; i < N; ++i) { dx = P[i + 1].x - P[i].x; dy = P[i + 1].y - P[i].y; if (DBLCMP(dy) == 0) { if (DBLCMP((P[1].x - P[0].x) * (P[i + 1].x - P[i].x)) > 0) return 0; else continue; } px = P[i].x + (P[0].y - P[i].y) / dy * dx; if (dy > 0) L = max(L, px); else R = min(R, px); } L = ceil(L - EPS); R = floor(R + EPS); if (R < L) return 0; return (int)(R - L + 1); } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) scanf("%lf%lf", &P[i].x, &P[i].y); P[N] = P[0]; printf("%d\n", Solve()); return 0; }
#include <bits/stdc++.h> int a, b, c, d, e, f, g, h, i, j; struct node { int x, y; }; struct node Point[1005]; struct node ch; int left, right; int Cover(struct node x, struct node y, struct node z, int xx, int yy) { int ii, jj, kk; double alpha, beta, cita; ch.x = xx; ch.y = yy; alpha = atan2(x.x - y.x, x.y - y.y) - atan2(ch.x - y.x, ch.y - y.y) + 2 * 3.141592653589793238462643; while (alpha - 2 * 3.141592653589793238462643 > -(1e-13)) alpha -= 2 * 3.141592653589793238462643; beta = atan2(ch.x - y.x, ch.y - y.y) - atan2(z.x - y.x, z.y - y.y) + 2 * 3.141592653589793238462643; while (beta - 2 * 3.141592653589793238462643 > -(1e-13)) beta -= 2 * 3.141592653589793238462643; cita = atan2(x.x - y.x, x.y - y.y) - atan2(z.x - y.x, z.y - y.y) + 2 * 3.141592653589793238462643; while (cita - 2 * 3.141592653589793238462643 > -(1e-13)) cita -= 2 * 3.141592653589793238462643; if (fabs(alpha + beta - cita) <= 1e-13) return 1; return 0; } int main() { scanf("%d", &a); for (i = 1; i <= a; i++) scanf("%d%d", &Point[i].x, &Point[i].y); left = Point[1].x; right = Point[2].x; Point[a + 1] = Point[1]; if (left > right) { e = left; left = right; right = e; } for (i = 3; i <= a; i++) { for (; left <= right; left++) if (Cover(Point[i - 1], Point[i], Point[i + 1], left, Point[1].y)) break; for (; right >= left; right--) if (Cover(Point[i - 1], Point[i], Point[i + 1], right, Point[1].y)) break; } printf("%d\n", right - left + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; const double eps = 1e-6; int n; struct point { double x, y; point() {} point(double a, double b) { x = a; y = b; } void init() { scanf("%lf%lf", &x, &y); } point operator+(const point &a) const { return point(x + a.x, y + a.y); } point operator-(const point &a) const { return point(x - a.x, y - a.y); } } p[maxn]; double cross(point a, point b) { return a.x * b.y - a.y * b.x; } int sgn(double a) { if (fabs(a) < eps) return 0; if (a > 0.0) return 1; else return -1; } int main() { scanf("%d", &n); for (int a = 1; a <= n; a++) p[a].init(); for (int a = n; a >= 1; a--) p[a].y = fabs(p[a].y - p[1].y); if (p[1].x > p[2].x) { swap(p[1], p[2]); reverse(p + 3, p + n + 1); } double l = p[1].x, r = p[2].x; for (int a = 3; a < n; a++) { point nowp = p[a + 1] - p[a]; if (sgn(nowp.y) == 0) { if (sgn(nowp.x) > 0) { printf("0\n"); return 0; } } else { double x = p[a].x - nowp.x / nowp.y * p[a].y; if (sgn(cross(nowp, point(r, 0.0) - p[a])) < 0) r = min(r, x); if (sgn(cross(nowp, point(l, 0.0) - p[a])) < 0) l = max(l, x); if (sgn(l - r) > 0) { printf("0\n"); return 0; } } } printf("%d\n", max(0, (int)(floor(r) + eps) - (int)(ceil(l) + eps) + 1)); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, k; long long a[100010]; long long b[100010]; long long BZ[100010][40]; long long Min[100010][40]; long long Sum[100010][40]; int main() { scanf("%lld%lld", &n, &k); for (long long i = 0; i < n; ++i) scanf("%lld", &BZ[i][0]); for (long long i = 0; i < n; ++i) { scanf("%lld", &Min[i][0]); Sum[i][0] = Min[i][0]; } for (long long i = 1; i < 40; ++i) { for (long long j = 0; j < n; ++j) { BZ[j][i] = BZ[BZ[j][i - 1]][i - 1]; Min[j][i] = min(Min[j][i - 1], Min[BZ[j][i - 1]][i - 1]); Sum[j][i] = Sum[j][i - 1] + Sum[BZ[j][i - 1]][i - 1]; } } for (long long i = 0; i < n; ++i) { long long x = 0, y = 2147483647; long long d = i; long long kk = k; for (long long j = 39; j >= 0; --j) if (kk >= (1ll << j)) { kk -= 1ll << j; x += Sum[d][j]; y = min(y, Min[d][j]); d = BZ[d][j]; } printf("%lld %lld\n", x, y); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; cin >> n >> k; long long f[n], w[n]; for (long long i = 0; i < n; i++) cin >> f[i]; for (long long i = 0; i < n; i++) cin >> w[i]; long long mh = 0; while ((1LL << mh) <= k) mh++; long long up[n][mh], s[n][mh], m[n][mh]; for (long long i = 0; i < n; i++) { up[i][0] = f[i]; s[i][0] = w[i]; m[i][0] = w[i]; } for (long long j = 1; j < mh; j++) for (long long i = 0; i < n; i++) { up[i][j] = up[up[i][j - 1]][j - 1]; s[i][j] = s[i][j - 1] + s[up[i][j - 1]][j - 1]; m[i][j] = min(m[i][j - 1], m[up[i][j - 1]][j - 1]); } for (long long i = 0; i < n; i++) { long long curr = i, s1 = 0, m1 = 1000000000; for (long long j = 0; j < mh; j++) if ((k >> j) & 1) { s1 += s[curr][j]; m1 = min(m1, m[curr][j]); curr = up[curr][j]; } cout << s1 << " " << m1 << endl; } }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10, pi = acos(-1.0); long long n, N = 40, k; long long nxt[40][100005], sum[40][100005], mi[40][100005]; void query(long long node, long long dis) { long long x = 0, y = LLONG_MAX; for (int i = 0; i < N; i++) if (dis & (1LL << i)) { x += (sum[i][node]); y = min(y, mi[i][node]); node = nxt[i][node]; } cout << x << ' ' << y << "\n"; } int main() { long long n, k; cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> nxt[0][i]; nxt[0][i]++; } for (long long i = 1; i <= n; i++) { cin >> sum[0][i]; mi[0][i] = sum[0][i]; } for (int i = 1; i < N; i++) for (int j = 1; j <= n; j++) { nxt[i][j] = nxt[i - 1][nxt[i - 1][j]]; sum[i][j] = sum[i - 1][nxt[i - 1][j]] + sum[i - 1][j]; mi[i][j] = min(mi[i - 1][nxt[i - 1][j]], mi[i - 1][j]); } for (int i = 1; i <= n; i++) query(i, k); return 0; }
#include <bits/stdc++.h> inline long long read() { long long x = 0; char c = getchar(); bool k = false; for (; !isdigit(c); c = getchar()) k |= c == '-'; for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + c - '0'; if (k) x = -x; return x; } void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } const long long maxn = 1e5 + 10; long long sum[maxn][40]; long long min[maxn][40]; long long fa[maxn][40]; long long n, k; long long num[40], pos; signed main() { n = read(); k = read(); for (long long i = 1; i <= n; i++) fa[i][0] = read() + 1; for (long long i = 1; i <= n; i++) sum[i][0] = min[i][0] = read(); for (long long i = 1; i <= 39; i++) for (long long j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1], sum[j][i] = sum[j][i - 1] + sum[fa[j][i - 1]][i - 1], min[j][i] = std::min(min[j][i - 1], min[fa[j][i - 1]][i - 1]); for (long long i = 0; i <= 39; i++) if (k & (long long)std::pow(2, i)) num[++pos] = i; for (long long i = 1; i <= n; i++) { long long p = i; long long ans1 = 0, ans2 = 0x3f3f3f3f; for (long long j = 1; j <= pos; j++) { ans1 += sum[p][num[j]]; ans2 = std::min(ans2, min[p][num[j]]); p = fa[p][num[j]]; } write(ans1); putchar(' '); write(ans2); putchar('\n'); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; long long child[N], w[N], dp[N][65][3], ans[N][2]; void pre(int n) { for (int i = 0; i < n; i++) { dp[i][0][0] = w[i]; dp[i][0][2] = w[i]; dp[i][0][1] = child[i]; } for (int j = 1; j < 65; j++) { for (int i = 0; i < n; i++) { dp[i][j][1] = dp[dp[i][j - 1][1]][j - 1][1]; dp[i][j][0] = dp[i][j - 1][0] + dp[dp[i][j - 1][1]][j - 1][0]; dp[i][j][2] = min(dp[i][j - 1][2], dp[dp[i][j - 1][1]][j - 1][2]); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; long long k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> child[i]; for (int i = 0; i < n; i++) cin >> w[i]; pre(n); int pt; for (int i = 0; i < n; i++) { pt = i; ans[i][1] = 1000000007ULL; for (long long j = 0; (1LL << j) <= k; j++) { if ((1LL << j) & k) { ans[i][0] += dp[pt][j][0]; ans[i][1] = min(ans[i][1], dp[pt][j][2]); pt = dp[pt][j][1]; } } } for (int i = 0; i < n; i++) cout << ans[i][0] << " " << ans[i][1] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long bigmod(long long b, int p) { if (p == 0) return 1; long long my = bigmod(b, p / 2); my *= my; my %= 1000000007; if (p & 1) my *= b, my %= 1000000007; return my; } int setb(int n, int pos) { return n = n | (1 << pos); } int resb(int n, int pos) { return n = n & ~(1 << pos); } bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); } struct info { long long v, sum, mn; }; info ara[100005][36]; long long n, lol[100005][2]; void prework() { long long i, k; for (i = 0; i < n; i++) { ara[i][0].v = lol[i][0]; ara[i][0].sum = lol[i][1]; ara[i][0].mn = lol[i][1]; } for (k = 1; k < 35; k++) { for (i = 0; i < n; i++) { ara[i][k].v = ara[ara[i][k - 1].v][k - 1].v; ara[i][k].sum = ara[i][k - 1].sum + ara[ara[i][k - 1].v][k - 1].sum; ara[i][k].mn = min(ara[i][k - 1].mn, ara[ara[i][k - 1].v][k - 1].mn); } } } info go(long long idx, long long x) { long long cur = 1, i; for (i = 1; i <= 34; i++) cur *= 2; long long u = idx; info ret; ret.sum = 0; ret.mn = INT_MAX; for (i = 34; i >= 0; i--) { if (x >= cur) { ret.sum = ret.sum + ara[u][i].sum; ret.mn = min(ret.mn, ara[u][i].mn); u = ara[u][i].v; x -= cur; } cur /= 2; } return ret; } int main() { long long i, k; scanf("%lld %lld", &n, &k); for (i = 0; i < n; i++) scanf("%lld", &lol[i][0]); for (i = 0; i < n; i++) scanf("%lld", &lol[i][1]); prework(); for (i = 0; i < n; i++) { info ret = go(i, k); printf("%lld %lld\n", ret.sum, ret.mn); } return 0; }
#include <bits/stdc++.h> using namespace std; long long fa[51][200007], sum[51][200007], mn[51][200007]; inline long long read() { long long x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') flag = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } return x * flag; } signed main() { long long n = read(), k = read(); for (long long i = 0; i <= n - 1; ++i) fa[0][i] = read(); for (long long i = 0; i <= n - 1; ++i) { long long x = read(); sum[0][i] = x; mn[0][i] = x; } for (long long i = 1; i <= 50; ++i) { for (long long j = 0; j <= n - 1; ++j) { fa[i][j] = fa[i - 1][fa[i - 1][j]]; sum[i][j] = sum[i - 1][j] + sum[i - 1][fa[i - 1][j]]; mn[i][j] = min(mn[i - 1][j], mn[i - 1][fa[i - 1][j]]); } } for (long long i = 0; i <= n - 1; ++i) { long long res = 0, minn = mn[0][i], p = i; for (long long j = 0; j <= 51; ++j) { if (k & (1LL << j)) { res += sum[j][p]; minn = min(minn, mn[j][p]); p = fa[j][p]; } } printf("%lld %lld\n", res, minn); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx2,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using pi = pair<ll, ll>; const int mod = 1e9 + 7; const ll inf = 1e18; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll n, k, mn[40][100100], sm[40][100100], p[40][100100]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) cin >> p[0][i]; for (int i = 0; i < n; i++) cin >> mn[0][i], sm[0][i] = mn[0][i]; for (int i = 1; i < 34; i++) { for (int j = 0; j < n; j++) { p[i][j] = p[i - 1][p[i - 1][j]]; sm[i][j] = sm[i - 1][j] + sm[i - 1][p[i - 1][j]]; mn[i][j] = min(mn[i - 1][j], mn[i - 1][p[i - 1][j]]); } } for (int i = 0; i < n; i++) { ll u = i, m = LLONG_MAX, s = 0; for (int j = 34; j--;) { if (k & (1ll << j)) { m = min(m, mn[j][u]); s = s + sm[j][u]; u = p[j][u]; } } cout << s << " " << m << "\n"; } }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int SQRTN = 320; const int LOGN = 40; const double PI = acos(-1); long long dp[N][LOGN], dp1[N][LOGN], dp2[N][LOGN], n, f[N], w[N], k; void calc() { for (int i = 0; i < n; i++) dp[i][0] = f[i]; for (int i = 1; i < LOGN; i++) { for (int j = 0; j < n; j++) { dp[j][i] = dp[dp[j][i - 1]][i - 1]; } } for (int i = 0; i < N; i++) { for (int j = 0; j < LOGN; j++) dp2[i][j] = 2000000000; } for (int i = 0; i < n; i++) dp1[i][0] = dp2[i][0] = w[i]; for (int i = 1; i < LOGN; i++) { for (int j = 0; j < n; j++) { dp1[j][i] = dp1[dp[j][i - 1]][i - 1] + dp1[j][i - 1]; } } for (int i = 1; i < LOGN; i++) { for (int j = 0; j < n; j++) { dp2[j][i] = min(dp2[dp[j][i - 1]][i - 1], dp2[j][i - 1]); } } } long long get(long long n, long long k) { long long ret = 0; for (long long i = 36; i > -1; i--) { if (((1ll << i) & k)) { ret += dp1[n][i]; n = dp[n][i]; k ^= (1ll << i); } } return ret; } long long get2(long long n, long long k) { long long ret = 2000000000; for (int i = 36; i > -1; i--) { if ((1ll << i) & k) { ret = min(ret, dp2[n][i]); n = dp[n][i]; k -= (1ll << i); } } return ret; } int main() { scanf("%lld%lld", &n, &k); for (int i = 0; i < n; i++) scanf("%lld", f + i); for (int i = 0; i < n; i++) scanf("%lld", w + i); calc(); for (int j = 0; j < n; j++) { printf("%lld %lld\n", get(j, k), get2(j, k)); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; long long dp[maxn][35]; long long sum[maxn][35]; int f[maxn][35]; int n; long long k; void init() { for (int i = 0; i < maxn - 1; i++) { for (int j = 0; j < 35; j++) { dp[i][j] = 1e18; } } } void DP() { for (long long j = 1; (1LL << j) <= k; j++) { for (int i = 0; i < n; i++) { int p = f[i][j - 1]; f[i][j] = f[p][j - 1]; dp[i][j] = min(dp[i][j], min(dp[i][j - 1], dp[p][j - 1])); sum[i][j] += sum[i][j - 1] + sum[p][j - 1]; } } } int main() { init(); cin >> n >> k; for (int i = 0; i < n; i++) { int x; cin >> x; f[i][0] = x; } for (int i = 0; i < n; i++) { int x; cin >> x; dp[i][0] = x; sum[i][0] = x; } DP(); for (int i = 0; i < n; i++) { long long T = k; long long ans1 = 1e18; long long ans2 = 0; int pre = i; for (int j = 34; j >= 0; j--) { if ((1LL << j) <= T) { ans1 = min(ans1, dp[pre][j]); ans2 += sum[pre][j]; pre = f[pre][j]; T -= (1LL << j); } if (T == 0) break; } cout << ans2 << " " << ans1 << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int nxt[N][36]; long long sum[N][36]; int mi[N][36]; int main() { int n; long long k; while (cin >> n >> k) { for (int i = 0; i < n; i++) { int x; scanf("%d", &x); nxt[i][0] = x; } for (int i = 0; i < n; i++) { int x; scanf("%d", &x); sum[i][0] = x; mi[i][0] = x; } for (int j = 1; j <= 35; j++) { for (int i = 0; i < n; i++) { nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; mi[i][j] = min(mi[i][j - 1], mi[nxt[i][j - 1]][j - 1]); sum[i][j] = sum[i][j - 1] + sum[nxt[i][j - 1]][j - 1]; } } for (int i = 0; i < n; i++) { long long ans1 = 0; int ans2 = 1e9; int cur = i; for (int j = 0; j <= 35; j++) { if (k & ((long long)1 << j)) { ans1 += sum[cur][j]; ans2 = min(ans2, mi[cur][j]); cur = nxt[cur][j]; } } cout << ans1 << " " << ans2 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long k; int out[100001], w[100001], dp[100001][40]; long long sum[100001][40]; long long mn[100001][40]; int main() { scanf("%d%lld", &n, &k); for (int i = 0; i < n; ++i) { scanf("%d", &out[i]); dp[i][0] = out[i]; } for (int i = 0; i < n; ++i) { scanf("%d", &w[i]); sum[i][0] = w[i]; mn[i][0] = w[i]; } for (int k = 1; k < 40; ++k) { for (int u = 0; u < n; ++u) { dp[u][k] = dp[dp[u][k - 1]][k - 1]; sum[u][k] = sum[u][k - 1] + sum[dp[u][k - 1]][k - 1]; mn[u][k] = min(mn[u][k - 1], mn[dp[u][k - 1]][k - 1]); } } for (int i = 0; i < n; ++i) { long long si = 0; int cur = i; long long rem = k; long long m = 1e18; for (long long p = 39; p >= 0; --p) { if ((1ll << p) <= rem) { rem -= (1ll << p); m = min(m, mn[cur][p]); si += sum[cur][p]; cur = dp[cur][p]; } } printf("%lld %lld\n", si, m); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; int fa[maxn][35]; long long minval[maxn][35]; long long sum[maxn][35]; void init(int n) { for (int k = 1; k < 34; k++) for (int i = 0; i < n; i++) { fa[i][k] = fa[fa[i][k - 1]][k - 1]; minval[i][k] = min(minval[i][k - 1], minval[fa[i][k - 1]][k - 1]); sum[i][k] = sum[i][k - 1] + sum[fa[i][k - 1]][k - 1]; } } void solve(int r, long long m) { long long ansmin = 1e9 + 6; long long anssum = 0; for (int k = 0; k < 34; k++) if (m & (1LL << k)) { anssum += sum[r][k]; ansmin = min(ansmin, minval[r][k]); r = fa[r][k]; } printf("%lld %lld\n", anssum, ansmin); } int main() { int n; long long m; scanf("%d%lld", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &fa[i][0]); for (int i = 0; i < n; i++) { scanf("%lld", &minval[i][0]); sum[i][0] = minval[i][0]; } init(n); for (int i = 0; i < n; i++) solve(i, m); }