#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const double INF = 1e12;
const double PI = acos(-1);
struct L : array<complex<double>, 2> {
  L(const complex<double>& a, const complex<double>& b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};
namespace std {
bool operator<(const complex<double>& a, const complex<double>& b) {
  return !(abs((a.real()) - (b.real())) < EPS) ? a.real() < b.real()
                                               : a.imag() + EPS < b.imag();
}
bool operator==(const complex<double>& a, const complex<double>& b) {
  return abs(a - b) < EPS;
}
}  // namespace std
double dot(complex<double> a, complex<double> b) {
  return (conj(a) * b).real();
}
double cross(complex<double> a, complex<double> b) {
  return (conj(a) * b).imag();
}
bool intersectLP(const L& l, const complex<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<double>& a, const complex<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<double> crosspointLL(const L& l, const L& m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  return m[0] + B / A * (m[1] - m[0]);
}
double getarea(const vector<complex<double> >& poly) {
  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(complex<double> p, const vector<complex<double> >& v) {
  if (p == v[0]) p = v[0];
  if (p == v[1]) p = v[1];
  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;
}
double solve(vector<complex<double> >& v, L cut) {
  int n = v.size();
  complex<double> cdir = cut[1] - cut[0];
  vector<complex<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<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());
  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<double> > v(n);
  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    v[i] = complex<double>(x, y);
  }
  if (getarea(v) < 0) {
    reverse(v.begin(), v.end());
  }
  cout << fixed << setprecision(10);
  for (int i = 0; i < m; i++) {
    double xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt;
    cout << solve(v, L(complex<double>(xs, ys), complex<double>(xt, yt)))
         << endl;
  }
  return 0;
}

### Prompt

Create a solution in cpp for 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 double EPS = 1e-8;
const double INF = 1e12;
const double PI = acos(-1);
struct L : array<complex<double>, 2> {
  L(const complex<double>& a, const complex<double>& b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};
namespace std {
bool operator<(const complex<double>& a, const complex<double>& b) {
  return !(abs((a.real()) - (b.real())) < EPS) ? a.real() < b.real()
                                               : a.imag() + EPS < b.imag();
}
bool operator==(const complex<double>& a, const complex<double>& b) {
  return abs(a - b) < EPS;
}
}  // namespace std
double dot(complex<double> a, complex<double> b) {
  return (conj(a) * b).real();
}
double cross(complex<double> a, complex<double> b) {
  return (conj(a) * b).imag();
}
bool intersectLP(const L& l, const complex<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<double>& a, const complex<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<double> crosspointLL(const L& l, const L& m) {
  double A = cross(l[1] - l[0], m[1] - m[0]);
  double B = cross(l[1] - l[0], l[1] - m[0]);
  return m[0] + B / A * (m[1] - m[0]);
}
double getarea(const vector<complex<double> >& poly) {
  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(complex<double> p, const vector<complex<double> >& v) {
  if (p == v[0]) p = v[0];
  if (p == v[1]) p = v[1];
  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;
}
double solve(vector<complex<double> >& v, L cut) {
  int n = v.size();
  complex<double> cdir = cut[1] - cut[0];
  vector<complex<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<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());
  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<double> > v(n);
  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    v[i] = complex<double>(x, y);
  }
  if (getarea(v) < 0) {
    reverse(v.begin(), v.end());
  }
  cout << fixed << setprecision(10);
  for (int i = 0; i < m; i++) {
    double xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt;
    cout << solve(v, L(complex<double>(xs, ys), complex<double>(xt, yt)))
         << endl;
  }
  return 0;
}

```