結果
問題 | No.2376 障害物競プロ |
ユーザー |
![]() |
提出日時 | 2023-07-07 22:41:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 527 ms / 4,000 ms |
コード長 | 25,817 bytes |
コンパイル時間 | 5,615 ms |
コンパイル使用メモリ | 292,988 KB |
最終ジャッジ日時 | 2025-02-15 07:59:13 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
#define MOD_TYPE 1#include <bits/stdc++.h>using namespace std;#include <atcoder/all>// #include <atcoder/lazysegtree>// #include <atcoder/modint>// #include <atcoder/segtree>using namespace atcoder;#if 0#include <boost/multiprecision/cpp_dec_float.hpp>#include <boost/multiprecision/cpp_int.hpp>using Int = boost::multiprecision::cpp_int;using lld = boost::multiprecision::cpp_dec_float_100;#endif#if 0#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tag_and_trait.hpp>#include <ext/pb_ds/tree_policy.hpp>#include <ext/rope>using namespace __gnu_pbds;using namespace __gnu_cxx;template <typename T>using extset =tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;#endif#if 0#pragma GCC target("avx2")#pragma GCC optimize("O3")#pragma GCC optimize("unroll-loops")#endif#pragma region Macrosusing ll = long long int;using ld = long double;using pii = pair<int, int>;using pll = pair<ll, ll>;using pld = pair<ld, ld>;template <typename Q_type>using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;#if MOD_TYPE == 1constexpr ll MOD = ll(1e9 + 7);#else#if MOD_TYPE == 2constexpr ll MOD = 998244353;#elseconstexpr ll MOD = 1000003;#endif#endifusing mint = static_modint<MOD>;constexpr int INF = (int)1e9 + 10;constexpr ll LINF = (ll)4e18;const double PI = acos(-1.0);constexpr ld EPS = 1e-10;constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)#define rep(i, n) REP(i, 0, n)#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)#define repi(i, n) REPI(i, 0, n)#define RREP(i, m, n) for (ll i = n - 1; i >= m; i--)#define rrep(i, n) RREP(i, 0, n)#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"#define all(v) v.begin(), v.end()#define NP(v) next_permutation(all(v))#define dbg(x) cerr << #x << ":" << x << "\n";#define UNIQUE(v) v.erase(unique(all(v)), v.end())struct io_init {io_init() {cin.tie(nullptr);ios::sync_with_stdio(false);cout << setprecision(20) << setiosflags(ios::fixed);};} io_init;template <typename T>inline bool chmin(T &a, T b) {if (a > b) {a = b;return true;}return false;}template <typename T>inline bool chmax(T &a, T b) {if (a < b) {a = b;return true;}return false;}inline ll floor(ll a, ll b) {if (b < 0) a *= -1, b *= -1;if (a >= 0) return a / b;return -((-a + b - 1) / b);}inline ll ceil(ll a, ll b) { return floor(a + b - 1, b); }template <typename A, size_t N, typename T>inline void Fill(A (&array)[N], const T &val) {fill((T *)array, (T *)(array + N), val);}template <typename T>vector<T> compress(vector<T> &v) {vector<T> val = v;sort(all(val)), val.erase(unique(all(val)), val.end());for (auto &&vi : v) vi = lower_bound(all(val), vi) - val.begin();return val;}template <typename T, typename U>constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept {is >> p.first >> p.second;return is;}template <typename T, typename U>constexpr ostream &operator<<(ostream &os, pair<T, U> p) noexcept {os << p.first << " " << p.second;return os;}ostream &operator<<(ostream &os, mint m) {os << m.val();return os;}ostream &operator<<(ostream &os, modint m) {os << m.val();return os;}template <typename T>constexpr istream &operator>>(istream &is, vector<T> &v) noexcept {for (int i = 0; i < v.size(); i++) is >> v[i];return is;}template <typename T>constexpr ostream &operator<<(ostream &os, vector<T> &v) noexcept {for (int i = 0; i < v.size(); i++)os << v[i] << (i + 1 == v.size() ? "" : " ");return os;}template <typename T>constexpr void operator--(vector<T> &v, int) noexcept {for (int i = 0; i < v.size(); i++) v[i]--;}random_device seed_gen;mt19937_64 engine(seed_gen());inline ll randInt(ll l, ll r) { return engine() % (r - l + 1) + l; }struct BiCoef {vector<mint> fact_, inv_, finv_;BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);for (int i = 2; i < n; i++) {fact_[i] = fact_[i - 1] * i;inv_[i] = -inv_[MOD % i] * (MOD / i);finv_[i] = finv_[i - 1] * inv_[i];}}mint C(ll n, ll k) const noexcept {if (n < k || n < 0 || k < 0) return 0;return fact_[n] * finv_[k] * finv_[n - k];}mint P(ll n, ll k) const noexcept { return C(n, k) * fact_[k]; }mint H(ll n, ll k) const noexcept { return C(n + k - 1, k); }mint Ch1(ll n, ll k) const noexcept {if (n < 0 || k < 0) return 0;mint res = 0;for (int i = 0; i < n; i++)res += C(n, i) * mint(n - i).pow(k) * (i & 1 ? -1 : 1);return res;}mint fact(ll n) const noexcept {if (n < 0) return 0;return fact_[n];}mint inv(ll n) const noexcept {if (n < 0) return 0;return inv_[n];}mint finv(ll n) const noexcept {if (n < 0) return 0;return finv_[n];}};BiCoef bc(200010);#pragma endregion// -------------------------------#pragma region Geometryusing Real = long double;using Point = complex<Real>;inline bool eq(Real a, Real b) { return fabs(b - a) < EPS; }inline bool eq(Point a, Point b) { return fabs(b - a) < EPS; }Point operator*(const Point &p, const Real &d) {return Point(real(p) * d, imag(p) * d);}istream &operator>>(istream &is, Point &p) {Real a, b;is >> a >> b;p = Point(a, b);return is;}ostream &operator<<(ostream &os, Point &p) {return os << fixed << setprecision(20) << p.real() << " " << p.imag();}// 点 p を原点を中心として反時計回りに theta 回転inline Point rotate(const Point &p, Real theta) {return Point(cos(theta) * p.real() - sin(theta) * p.imag(),sin(theta) * p.real() + cos(theta) * p.imag());}// 点 p を点 c を中心として反時計回りに theta 回転inline Point rotate(Point p, Real theta, const Point &c) {p -= c;return rotate(p, theta) + c;}Real radian_to_degree(Real r) { return (r * 180.0 / PI); }Real degree_to_radian(Real d) { return (d * PI / 180.0); }// a-b-c の角度のうち小さい方を返すReal get_angle(Point a, Point b, Point c) {a -= b, c -= b;Real alpha = atan2(a.imag(), a.real()), beta = atan2(c.imag(), c.real());if (alpha > beta) swap(alpha, beta);Real theta = (beta - alpha);return min(theta, 2 * acos(-1) - theta);}// a-b-c の角度([0,2π)、a を反時計回りに回転させてcに重ねる角度)Real get_angle2(Point a, Point b, Point c) {a -= b, c -= b;Real theta = atan2(imag(c), real(c)) - atan2(imag(a), real(a));while (theta < 0) theta += PI * 2;while (theta > PI * 2) theta -= PI * 2;return theta;}// a-b-c の角度([0,2π)、p を間に含む方)Real get_angle2(const Point &a, const Point &b, const Point &c,const Point &p) {if (get_angle2(a, b, p) + get_angle2(p, b, c) < PI * 2)return get_angle2(a, b, c);elsereturn get_angle2(c, b, a);}namespace std {bool operator<(const Point &a, const Point &b) {return !eq(a.real(), b.real()) ? a.real() < b.real() : a.imag() < b.imag();}} // namespace stdstruct Line {Point a, b;Line() = default;Line(Point a, Point b) : a(a), b(b) {}Line(Real A, Real B, Real C) // Ax + By = C{if (eq(A, 0))a = Point(0, C / B), b = Point(1, C / B);else if (eq(B, 0))b = Point(C / A, 0), b = Point(C / A, 1);elsea = Point(0, C / B), b = Point(C / A, 0);}friend ostream &operator<<(ostream &os, Line &p) {return os << p.a << " to " << p.b;}friend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }};// Ax + By = Ctuple<Real, Real, Real> parameter(const Line &l) {Real A = imag(l.b) - imag(l.a);Real B = real(l.a) - real(l.b);Real C = real(l.a) * A + imag(l.a) * B;return {A, B, C};}struct Segment : Line {Segment() = default;Segment(Point a, Point b) : Line(a, b) {}};struct Circle {Point p;Real r;Circle() = default;Circle(Point p, Real r) : p(p), r(r) {}};using Points = vector<Point>;using Polygon = vector<Point>;using Segments = vector<Segment>;using Lines = vector<Line>;using Circles = vector<Circle>;inline Real cross(const Point &a, const Point &b) {return real(a) * imag(b) - imag(a) * real(b);}inline Real dot(const Point &a, const Point &b) {return real(a) * real(b) + imag(a) * imag(b);}// 直線がx軸となす角 [0, π)// to do: verifyinline Real get_angle(const Line &l) {Point p = l.a - l.b;if (imag(p) < 0) p *= -1;return get_angle2(Point(1, 0), Point(0, 0), p);}// 2直線がなす角 [0, π/2]// to do: verifyinline Real get_angle(const Line &l1, const Line &l2) {Real theta = get_angle(l1) - get_angle(l2);if (theta < 0) theta += PI;return theta >= PI / 2.0 ? theta - PI / 2.0 : theta;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C// 点の回転方向int ccw(const Point &a, Point b, Point c) {b = b - a, c = c - a;if (cross(b, c) > EPS) return +1; // "COUNTER_CLOCKWISE"if (cross(b, c) < -EPS) return -1; // "CLOCKWISE"if (dot(b, c) < 0) return +2; // "ONLINE_BACK"if (norm(b) < norm(c)) return -2; // "ONLINE_FRONT"return 0; // "ON_SEGMENT"}// p, q を m : n に内分する点inline Point internal_point(const Point &p, const Point &q, Real m, Real n) {return (n * p + m * q) / (m + n);}// p, q を m : n に外分する点inline Point external_point(const Point &p, const Point &q, Real m, Real n) {return internal_point(p, q, m, -n);}// 垂直ベクトルinline Point orthvector(const Point p) { return Point(imag(p), -real(p)); }// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A// 平行判定inline bool parallel(const Line &a, const Line &b) {return eq(cross(a.b - a.a, b.b - b.a), 0.0);}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A// 垂直判定inline bool orthogonal(const Line &a, const Line &b) {return eq(dot(a.a - a.b, b.a - b.b), 0.0);}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A// 射影// 直線 l に p から垂線を引いた交点を求めるinline Point projection(const Line &l, const Point &p) {double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);return l.a + (l.a - l.b) * t;}inline Point projection(const Segment &l, const Point &p) {double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);return l.a + (l.a - l.b) * t;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B// 反射// 直線 l を対称軸として点 p と線対称にある点を求めるinline Point reflection(const Line &l, const Point &p) {return p + (projection(l, p) - p) * 2.0;}// 点 p を通り直線 l に垂直な直線inline Line verticalline(const Line &l, const Point &p) {return Line(p, p + orthvector(l.a - l.b));}// 点 p, q の垂直二等分線inline Line bisector(const Point &p, const Point &q) {Line l(p, q);Point m = internal_point(p, q, 1, 1);return verticalline(l, m);}inline Line bisector(const Segment &sg) { return bisector(sg.a, sg.b); }inline bool intersect(const Line &l, const Point &p) {return abs(ccw(l.a, l.b, p)) != 1;}inline bool intersect(const Line &l, const Line &m) {return abs(cross(l.b - l.a, m.b - m.a)) > EPS ||abs(cross(l.b - l.a, m.b - l.a)) < EPS;}inline bool intersect(const Segment &s, const Point &p) {return ccw(s.a, s.b, p) == 0;}inline bool intersect(const Line &l, const Segment &s) {return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;}inline Real distance(const Line &l, const Point &p);inline bool intersect(const Circle &c, const Line &l) {return distance(l, c.p) <= c.r + EPS;}inline bool intersect(const Circle &c, const Point &p) {return abs(abs(p - c.p) - c.r) < EPS;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_Bbool intersect(const Segment &s, const Segment &t) {return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;}int intersect(const Circle &c, const Segment &l) {if (norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0;auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);if (d1 < c.r + EPS && d2 < c.r + EPS) return 0;if (d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS)return 1;const Point h = projection(l, c.p);if (dot(l.a - h, l.b - h) < 0) return 2;return 0;}// 共通接戦の本数// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jpint intersect(Circle c1, Circle c2) {if (c1.r < c2.r) swap(c1, c2);Real d = abs(c1.p - c2.p);if (c1.r + c2.r < d) return 4;if (eq(c1.r + c2.r, d)) return 3;if (c1.r - c2.r < d) return 2;if (eq(c1.r - c2.r, d)) return 1;return 0;}inline Real distance(const Point &a, const Point &b) { return abs(a - b); }inline Real distance(const Line &l, const Point &p) {return abs(p - projection(l, p));}inline Real distance(const Line &l, const Line &m) {return intersect(l, m) ? 0 : distance(l, m.a);}inline Real distance(const Segment &s, const Point &p) {Point r = projection(s, p);if (intersect(s, r)) return abs(r - p);return min(abs(s.a - p), abs(s.b - p));}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_DReal distance(const Segment &a, const Segment &b) {if (intersect(a, b)) return 0;return min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});}Real distance(const Line &l, const Segment &s) {if (intersect(l, s)) return 0;return min(distance(l, s.a), distance(l, s.b));}Point crosspoint(const Line &l, const Line &m) {Real A = cross(l.b - l.a, m.b - m.a);Real B = cross(l.b - l.a, l.b - m.a);if (eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;return m.a + (m.b - m.a) * B / A;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_CPoint crosspoint(const Segment &l, const Segment &m) {return crosspoint(Line(l), Line(m));}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_Dpair<Point, Point> crosspoint(const Circle &c, const Line l) {Point pr = projection(l, c.p);Point e = (l.b - l.a) / abs(l.b - l.a);if (eq(distance(l, c.p), c.r)) return {pr, pr};double base = sqrt(c.r * c.r - norm(pr - c.p));return {pr - e * base, pr + e * base};}pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {Line aa = Line(l.a, l.b);if (intersect(c, l) == 2) return crosspoint(c, aa);auto ret = crosspoint(c, aa);if (dot(l.a - ret.first, l.b - ret.first) < 0)ret.second = ret.first;elseret.first = ret.second;return ret;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_Epair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {Real d = abs(c1.p - c2.p);Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);return {p1, p2};}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F// 点 p を通る円 c の接線(接点を返す)pair<Point, Point> tangent(const Circle &c1, const Point &p2) {return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G// 円 c1, c2 の共通接線Lines tangent(Circle c1, Circle c2) {Lines ret;if (c1.r < c2.r) swap(c1, c2);Real g = norm(c1.p - c2.p);if (eq(g, 0)) return ret;Point u = (c2.p - c1.p) / sqrt(g);Point v = rotate(u, PI * 0.5);for (int s : {-1, 1}) {Real h = (c1.r + s * c2.r) / sqrt(g);if (eq(1 - h * h, 0)) {ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);} else if (1 - h * h > 0) {Point uu = u * h, vv = v * sqrt(1 - h * h);ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);}}return ret;}// 2点 p, q からの距離が m : n (m != n)となる点の軌跡Circle Apollonius(const Point &p, const Point &q, Real m, Real n) {assert(!eq(m, n));Circle res;res.p = external_point(p, q, m * m, n * n);res.r = sqrt(fabs(p - res.p) * fabs(q - res.p));Point pp = res.p + Point(res.r, 0);assert(eq(distance(pp, p) * n, distance(pp, q) * m));return res;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B// 凸性判定bool is_convex(const Polygon &p) {int n = (int)p.size();for (int i = 0; i < n; i++) {if (ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false;}return true;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A// 凸包Polygon convex_hull(Polygon &p, bool strict = true) {int n = (int)p.size(), k = 0;if (n <= 2) return p;sort(p.begin(), p.end());vector<Point> ch(2 * n);Real EPS2 = (strict ? EPS : -EPS);for (int i = 0; i < n; ch[k++] = p[i++]) {while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS2) --k;}for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < EPS2) --k;}ch.resize(k - 1);return ch;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C// 多角形と点の包含判定enum { OUT, ON, IN };int contains(const Polygon &Q, const Point &p) {bool in = false;for (int i = 0; i < Q.size(); i++) {Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;if (a.imag() > b.imag()) swap(a, b);if (a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in;if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;}return in ? IN : OUT;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033// 線分の重複除去void merge_segments(vector<Segment> &segs) {auto merge_if_able = [](Segment &s1, const Segment &s2) {if (abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false;if (ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false;if (ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2)return false;s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));return true;};for (int i = 0; i < segs.size(); i++) {if (segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b);}for (int i = 0; i < segs.size(); i++) {for (int j = i + 1; j < segs.size(); j++) {if (merge_if_able(segs[i], segs[j])) {segs[j--] = segs.back(), segs.pop_back();}}}}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033// 線分アレンジメント// 任意の2線分の交点を頂点としたグラフを構築するvector<vector<int>> segment_arrangement(vector<Segment> &segs,vector<Point> &ps) {vector<vector<int>> g;int N = (int)segs.size();for (int i = 0; i < N; i++) {ps.emplace_back(segs[i].a);ps.emplace_back(segs[i].b);for (int j = i + 1; j < N; j++) {const Point p1 = segs[i].b - segs[i].a;const Point p2 = segs[j].b - segs[j].a;if (cross(p1, p2) == 0) continue;if (intersect(segs[i], segs[j])) {ps.emplace_back(crosspoint(segs[i], segs[j]));}}}sort(begin(ps), end(ps));ps.erase(unique(begin(ps), end(ps)), end(ps));int M = (int)ps.size();g.resize(M);for (int i = 0; i < N; i++) {vector<int> vec;for (int j = 0; j < M; j++) {if (intersect(segs[i], ps[j])) {vec.emplace_back(j);}}for (int j = 1; j < vec.size(); j++) {g[vec[j - 1]].push_back(vec[j]);g[vec[j]].push_back(vec[j - 1]);}}return (g);}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C// 凸多角形の切断// 直線 l.a-l.b で切断しその左側にできる凸多角形を返すPolygon convex_cut(const Polygon &U, Line l) {Polygon ret;for (int i = 0; i < U.size(); i++) {Point now = U[i], nxt = U[(i + 1) % U.size()];if (ccw(l.a, l.b, now) != -1) ret.push_back(now);if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {ret.push_back(crosspoint(Line(now, nxt), l));}}return (ret);}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A// 多角形の面積Real area(const Polygon &p) {Real A = 0;for (int i = 0; i < p.size(); ++i) {A += cross(p[i], p[(i + 1) % p.size()]);}return A * 0.5;}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H// 円と多角形の共通部分の面積Real area(const Polygon &p, const Circle &c) {if (p.size() < 3) return 0.0;function<Real(Circle, Point, Point)> cross_area =[&](const Circle &c, const Point &a, const Point &b) {Point va = c.p - a, vb = c.p - b;Real f = cross(va, vb), ret = 0.0;if (eq(f, 0.0)) return ret;if (max(abs(va), abs(vb)) < c.r + EPS) return f;if (distance(Segment(a, b), c.p) > c.r - EPS)return c.r * c.r * arg(vb * conj(va));auto u = crosspoint(c, Segment(a, b));vector<Point> tot{a, u.first, u.second, b};for (int i = 0; i + 1 < tot.size(); i++) {ret += cross_area(c, tot[i], tot[i + 1]);}return ret;};Real A = 0;for (int i = 0; i < p.size(); i++) {A += cross_area(c, p[i], p[(i + 1) % p.size()]);}return A / 2.0;}// 2円の共通部分の面積Real area(const Circle &c1, const Circle &c2) {int t = intersect(c1, c2);if (t > 2) return 0.0;if (t < 2) return min(c1.r * c1.r * PI, c2.r * c2.r * PI);Real res = 0.0;auto [p1, p2] = crosspoint(c1, c2);Real theta1 = get_angle2(p1, c1.p, p2, c2.p);res += c1.r * c1.r * 0.5 * (theta1 - sin(theta1));Real theta2 = get_angle2(p2, c2.p, p1, c1.p);res += c2.r * c2.r * 0.5 * (theta2 - sin(theta2));return fabs(res);}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B// 凸多角形の直径(最遠頂点対間距離)Real convex_diameter(const Polygon &p) {int N = (int)p.size();int is = 0, js = 0;for (int i = 1; i < N; i++) {if (p[i].imag() > p[is].imag()) is = i;if (p[i].imag() < p[js].imag()) js = i;}Real maxdis = norm(p[is] - p[js]);int maxi, maxj, i, j;i = maxi = is;j = maxj = js;do {if (cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) {j = (j + 1) % N;} else {i = (i + 1) % N;}if (norm(p[i] - p[j]) > maxdis) {maxdis = norm(p[i] - p[j]);maxi = i;maxj = j;}} while (i != is || j != js);return sqrt(maxdis);}// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A// 最近点対Real closest_pair(Points ps) {if (ps.size() <= 1) throw(0);sort(begin(ps), end(ps));auto compare_y = [&](const Point &a, const Point &b) {return imag(a) < imag(b);};vector<Point> beet(ps.size());const Real INF = 1e18;function<Real(int, int)> rec = [&](int left, int right) {if (right - left <= 1) return INF;int mid = (left + right) >> 1;auto x = real(ps[mid]);auto ret = min(rec(left, mid), rec(mid, right));inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right,compare_y);int ptr = 0;for (int i = left; i < right; i++) {if (abs(real(ps[i]) - x) >= ret) continue;for (int j = 0; j < ptr; j++) {auto luz = ps[i] - beet[ptr - j - 1];if (imag(luz) >= ret) break;ret = min(ret, abs(luz));}beet[ptr++] = ps[i];}return ret;};return rec(0, (int)ps.size());}// 凸多角形の共通部分Polygon intersection(const Polygon &p1, const Polygon &p2) {int n1 = p1.size(), n2 = p2.size();Polygon res;rep(i, n1) {if (contains(p2, p1[i]) != OUT) res.push_back(p1[i]);}rep(i, n2) {if (contains(p1, p2[i]) != OUT) res.push_back(p2[i]);}rep(i, n1) rep(j, n2) {Segment s1(p1[i], p1[(i + 1) % n1]);Segment s2(p2[j], p2[(j + 1) % n2]);if (intersect(s1, s2)) res.push_back(crosspoint(s1, s2));}return convex_hull(res);}#pragma endregionvoid solve() {int n, m;cin >> n >> m;vector<double> x(n * 2), y(n * 2);vector<Segment> s(n);rep(i, n) {cin >> x[i * 2] >> y[i * 2] >> x[i * 2 + 1] >> y[i * 2 + 1];s[i] = Segment({x[i * 2], y[i * 2]}, {x[i * 2 + 1], y[i * 2 + 1]});}auto can = [&](int i, int type1, int j, int type2) {int num1 = i * 2 + type1;int num2 = j * 2 + type2;Point p(x[num1], y[num1]);Point q(x[num2], y[num2]);Segment pq(p, q);rep(k, n) {if (k == i or k == j) continue;if (intersect(pq, s[k])) return false;}return true;};double d[300][300];rep(i, n * 2) rep(j, n * 2) d[i][j] = (i == j ? 0.0 : 1e18);rep(i, n) REP(j, i, n) rep(t1, 2) rep(t2, 2) {if (can(i, t1, j, t2)) {int num1 = i * 2 + t1;int num2 = j * 2 + t2;d[num1][num2] = d[num2][num1] =distance(Point(x[num1], y[num1]), Point(x[num2], y[num2]));}}rep(k, n * 2) rep(i, n * 2) rep(j, n * 2) {chmin(d[i][j], d[i][k] + d[k][j]);}rep(_, m) {int i, j, type1, type2;cin >> i >> type1 >> j >> type2;i--, j--;type1--, type2--;int num1 = i * 2 + type1;int num2 = j * 2 + type2;cout << d[num1][num2] << "\n";}}int main() { solve(); }