#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-8;
const long double INF = 1e12;
const long double PI = acos(-1);
struct L : array<complex<long double>, 2> {
  L(const complex<long double>& a, const complex<long double>& b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};
namespace std {
bool operator<(const complex<long double>& a, const complex<long double>& b) {
  return !(abs((a.real()) - (b.real())) < EPS) ? a.real() < b.real()
                                               : a.imag() + EPS < b.imag();
}
bool operator==(const complex<long double>& a, const complex<long double>& b) {
  return abs(a - b) < EPS;
}
}  // namespace std
long double dot(complex<long double> a, complex<long double> b) {
  return (conj(a) * b).real();
}
long double cross(complex<long double> a, complex<long double> b) {
  return (conj(a) * b).imag();
}
bool intersectLP(const L& l, const complex<long double>& p) {
  return abs(cross(l[1] - p, l[0] - p)) < EPS;
}
bool strictItsLS(const L& l, const L& s) {
  return cross(l[1] - l[0], s[0] - l[0]) * cross(l[1] - l[0], s[1] - l[0]) <
         -EPS;
}
bool isParallel(const complex<long double>& a, const complex<long double>& b) {
  return abs(cross(a, b)) < EPS;
}
bool isParallel(const L& a, const L& b) {
  return isParallel(a[1] - a[0], b[1] - b[0]);
}
complex<long double> crosspointLL(const L& l, const L& m) {
  long double A = cross(l[1] - l[0], m[1] - m[0]);
  long double B = cross(l[1] - l[0], l[1] - m[0]);
  return m[0] + B / A * (m[1] - m[0]);
}
long double getarea(const vector<complex<long double> >& poly) {
  long double ret = 0;
  for (int i = 0; i < (int)poly.size(); i++) {
    ret += cross(poly[i], poly[(i + 1) % poly.size()]);
  }
  return ret * 0.5;
}
bool in_corner(const complex<long double>& p,
               const vector<complex<long double> >& v) {
  bool s1 = cross(v[1] - v[0], p - v[0]) > -EPS;
  bool s2 = cross(v[2] - v[1], p - v[1]) > -EPS;
  return (cross(v[1] - v[0], v[2] - v[0]) > -EPS) ? s1 and s2 : s1 or s2;
}
long double solve(vector<complex<long double> >& v, L cut) {
  int n = v.size();
  complex<long double> cdir = cut[1] - cut[0];
  vector<complex<long double> > cps;
  for (int i = 0; i < n; i++) {
    L edge(v[i], v[(i + 1) % n]);
    if (strictItsLS(cut, edge) and !isParallel(cut, edge)) {
      cps.push_back(crosspointLL(cut, edge));
    }
    if (intersectLP(cut, v[i])) {
      vector<complex<long double> > p{v[(i - 1 + n) % n], v[i], v[(i + 1) % n]};
      if (in_corner(p[1] + cdir, p) != in_corner(p[1] - cdir, p)) {
        cps.push_back(v[i]);
      }
    }
  }
  sort(cps.begin(), cps.end());
  long double res = 0;
  for (int i = 0; i < (int)cps.size() - 1; i += 2) {
    res += abs(cps[i + 1] - cps[i]);
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<complex<long double> > v(n);
  for (int i = 0; i < n; i++) {
    long double x, y;
    cin >> x >> y;
    v[i] = complex<long double>(x, y);
  }
  if (getarea(v) < 0) {
    reverse(v.begin(), v.end());
  }
  cout << fixed << setprecision(10);
  for (int i = 0; i < m; i++) {
    long double xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt;
    cout << solve(v,
                  L(complex<long double>(xs, ys), complex<long double>(xt, yt)))
         << endl;
  }
  return 0;
}

### Prompt

Generate a cpp solution to the following problem:

Given simple (without self-intersections) n-gon. It is not necessary convex. Also you are given m lines. For each line find the length of common part of the line and the n-gon.

The boundary of n-gon belongs to polygon. It is possible that n-gon contains 180-degree angles.

Input

The first line contains integers n and m (3 ≤ n ≤ 1000;1 ≤ m ≤ 100). The following n lines contain coordinates of polygon vertices (in clockwise or counterclockwise direction). All vertices are distinct.

The following m lines contain line descriptions. Each of them contains two distict points of a line by their coordinates.

All given in the input coordinates are real numbers, given with at most two digits after decimal point. They do not exceed 105 by absolute values.

Output

Print m lines, the i-th line should contain the length of common part of the given n-gon and the i-th line. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

Examples

Input

4 3
0 0
1 0
1 1
0 1
0 0 1 1
0 0 0 1
0 0 1 -1


Output

1.41421356237309514547
1.00000000000000000000
0.00000000000000000000

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-8;
const long double INF = 1e12;
const long double PI = acos(-1);
struct L : array<complex<long double>, 2> {
  L(const complex<long double>& a, const complex<long double>& b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};
namespace std {
bool operator<(const complex<long double>& a, const complex<long double>& b) {
  return !(abs((a.real()) - (b.real())) < EPS) ? a.real() < b.real()
                                               : a.imag() + EPS < b.imag();
}
bool operator==(const complex<long double>& a, const complex<long double>& b) {
  return abs(a - b) < EPS;
}
}  // namespace std
long double dot(complex<long double> a, complex<long double> b) {
  return (conj(a) * b).real();
}
long double cross(complex<long double> a, complex<long double> b) {
  return (conj(a) * b).imag();
}
bool intersectLP(const L& l, const complex<long double>& p) {
  return abs(cross(l[1] - p, l[0] - p)) < EPS;
}
bool strictItsLS(const L& l, const L& s) {
  return cross(l[1] - l[0], s[0] - l[0]) * cross(l[1] - l[0], s[1] - l[0]) <
         -EPS;
}
bool isParallel(const complex<long double>& a, const complex<long double>& b) {
  return abs(cross(a, b)) < EPS;
}
bool isParallel(const L& a, const L& b) {
  return isParallel(a[1] - a[0], b[1] - b[0]);
}
complex<long double> crosspointLL(const L& l, const L& m) {
  long double A = cross(l[1] - l[0], m[1] - m[0]);
  long double B = cross(l[1] - l[0], l[1] - m[0]);
  return m[0] + B / A * (m[1] - m[0]);
}
long double getarea(const vector<complex<long double> >& poly) {
  long double ret = 0;
  for (int i = 0; i < (int)poly.size(); i++) {
    ret += cross(poly[i], poly[(i + 1) % poly.size()]);
  }
  return ret * 0.5;
}
bool in_corner(const complex<long double>& p,
               const vector<complex<long double> >& v) {
  bool s1 = cross(v[1] - v[0], p - v[0]) > -EPS;
  bool s2 = cross(v[2] - v[1], p - v[1]) > -EPS;
  return (cross(v[1] - v[0], v[2] - v[0]) > -EPS) ? s1 and s2 : s1 or s2;
}
long double solve(vector<complex<long double> >& v, L cut) {
  int n = v.size();
  complex<long double> cdir = cut[1] - cut[0];
  vector<complex<long double> > cps;
  for (int i = 0; i < n; i++) {
    L edge(v[i], v[(i + 1) % n]);
    if (strictItsLS(cut, edge) and !isParallel(cut, edge)) {
      cps.push_back(crosspointLL(cut, edge));
    }
    if (intersectLP(cut, v[i])) {
      vector<complex<long double> > p{v[(i - 1 + n) % n], v[i], v[(i + 1) % n]};
      if (in_corner(p[1] + cdir, p) != in_corner(p[1] - cdir, p)) {
        cps.push_back(v[i]);
      }
    }
  }
  sort(cps.begin(), cps.end());
  long double res = 0;
  for (int i = 0; i < (int)cps.size() - 1; i += 2) {
    res += abs(cps[i + 1] - cps[i]);
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<complex<long double> > v(n);
  for (int i = 0; i < n; i++) {
    long double x, y;
    cin >> x >> y;
    v[i] = complex<long double>(x, y);
  }
  if (getarea(v) < 0) {
    reverse(v.begin(), v.end());
  }
  cout << fixed << setprecision(10);
  for (int i = 0; i < m; i++) {
    long double xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt;
    cout << solve(v,
                  L(complex<long double>(xs, ys), complex<long double>(xt, yt)))
         << endl;
  }
  return 0;
}

```