#include <bits/stdc++.h>
using namespace std;
struct P {
  double first, second;
  bool sp;
  P(double first, double second, bool sp)
      : first(first), second(second), sp(sp) {}
};
int n, m;
int x[4000], y[4000];
int sx, sy, tx, ty;
long long a, b, c;
vector<pair<double, double> > p;
vector<bool> sp;
vector<P> pp;
const double EPS = 1e-9;
inline bool cmpX(const P &lhs, const P &rhs) { return lhs.first < rhs.first; }
inline bool cmpY(const P &lhs, const P &rhs) { return lhs.second < rhs.second; }
int sgn(long long x, long long y, long long xx, long long yy) {
  long long val = x * yy - xx * y;
  if (val > 0) return 1;
  if (val < 0) return -1;
  return 0;
}
void readNumber(int &x) {
  double d;
  scanf("%lf", &d);
  x = abs(d) * 100 + 0.5;
  if (d < 0) x *= -1;
}
bool check(long long x, long long y, long long xx, long long yy, long long xxx,
           long long yyy) {
  int sg = sgn(x, y, xxx, yyy);
  return !sg || sgn(x, y, xx, yy) == sg && sgn(xx, yy, xxx, yyy) == sg;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(n); ++i) readNumber(x[i]), readNumber(y[i]);
  for (int i = 0; i < (int)(3 * n); ++i) x[n + i] = x[i], y[n + i] = y[i];
  for (int query = 0; query < (int)(m); ++query) {
    readNumber(sx), readNumber(sy);
    readNumber(tx), readNumber(ty);
    a = sy - ty;
    b = tx - sx;
    c = a * sx + b * sy;
    p.clear();
    sp.clear();
    for (int i = 0; i < (int)(n); ++i)
      if (a * x[i] + b * y[i] == c && a * x[i + 1] + b * y[i + 1] == c) {
        p.push_back(make_pair(x[i], y[i]));
        p.push_back(make_pair(x[i + 1], y[i + 1]));
        sp.push_back(true), sp.push_back(true);
      } else if (a * x[i] + b * y[i] != c && a * x[i + 1] + b * y[i + 1] != c) {
        long long aa = y[i + 1] - y[i];
        long long bb = x[i] - x[i + 1];
        long long cc = aa * x[i] + bb * y[i];
        long long det = a * bb - aa * b;
        if (det == 0) continue;
        double ix = ((double)c * bb - (double)cc * b) / det;
        double iy = ((double)a * cc - (double)aa * c) / det;
        int minX = min(x[i], x[i + 1]);
        int maxX = max(x[i], x[i + 1]);
        int minY = min(y[i], y[i + 1]);
        int maxY = max(y[i], y[i + 1]);
        if (ix > minX - EPS && ix < maxX + EPS && iy > minY - EPS &&
            iy < maxY + EPS) {
          p.push_back(make_pair(ix, iy));
          sp.push_back(false);
        }
      }
    for (int i = 1; i <= n; ++i)
      if (a * x[i] + b * y[i] == c && a * x[i - 1] + b * y[i - 1] != c &&
          a * x[i + 1] + b * y[i + 1] != c) {
        if (check(x[i - 1] - x[i], y[i - 1] - y[i], -b, a, x[i + 1] - x[i],
                  y[i + 1] - y[i]) ||
            check(x[i - 1] - x[i], y[i - 1] - y[i], b, -a, x[i + 1] - x[i],
                  y[i + 1] - y[i])) {
          p.push_back(make_pair(x[i], y[i]));
          sp.push_back(false);
        }
      }
    int ed = -1;
    for (int beg = n; beg < 2 * n;)
      if (a * x[beg] + b * y[beg] == c) {
        int end = beg + 1;
        while (a * x[beg - 1] + b * y[beg - 1] == c) --beg;
        while (a * x[end] + b * y[end] == c) ++end;
        --end;
        if (ed != -1 && end == ed + n) break;
        if (sgn(x[beg] - x[beg - 1], y[beg] - y[beg - 1], x[beg + 1] - x[beg],
                y[beg + 1] - y[beg]) *
                sgn(x[end] - x[end - 1], y[end] - y[end - 1],
                    x[end + 1] - x[end], y[end + 1] - y[end]) <
            0) {
          p.push_back(make_pair(x[end], y[end]));
          sp.push_back(false);
        }
        if (ed == -1) ed = end;
        beg = end + 1;
      } else {
        ++beg;
      }
    pp.clear();
    for (int i = 0; i < (int)(p.size()); ++i)
      pp.push_back(P(p[i].first, p[i].second, sp[i]));
    if (b == 0)
      sort(pp.begin(), pp.end(), cmpY);
    else
      sort(pp.begin(), pp.end(), cmpX);
    for (int i = 0; i < (int)(p.size()); ++i)
      p[i].first = pp[i].first, p[i].second = pp[i].second, sp[i] = pp[i].sp;
    double ans = 0;
    bool curSp = false;
    pair<double, double> spPoint;
    for (int i = 0; i < (int)(p.size()); ++i) {
      if (!curSp && (i & 1)) {
        ans += sqrt(
            (p[i].first - p[i - 1].first) * (p[i].first - p[i - 1].first) +
            (p[i].second - p[i - 1].second) * (p[i].second - p[i - 1].second));
      }
      if (sp[i]) {
        if (curSp) {
          ans += sqrt(
              (spPoint.first - p[i].first) * (spPoint.first - p[i].first) +
              (spPoint.second - p[i].second) * (spPoint.second - p[i].second));
        } else {
          spPoint = p[i];
        }
        curSp = !curSp;
      }
    }
    printf("%.18f\n", ans * 0.01);
  }
  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;
struct P {
  double first, second;
  bool sp;
  P(double first, double second, bool sp)
      : first(first), second(second), sp(sp) {}
};
int n, m;
int x[4000], y[4000];
int sx, sy, tx, ty;
long long a, b, c;
vector<pair<double, double> > p;
vector<bool> sp;
vector<P> pp;
const double EPS = 1e-9;
inline bool cmpX(const P &lhs, const P &rhs) { return lhs.first < rhs.first; }
inline bool cmpY(const P &lhs, const P &rhs) { return lhs.second < rhs.second; }
int sgn(long long x, long long y, long long xx, long long yy) {
  long long val = x * yy - xx * y;
  if (val > 0) return 1;
  if (val < 0) return -1;
  return 0;
}
void readNumber(int &x) {
  double d;
  scanf("%lf", &d);
  x = abs(d) * 100 + 0.5;
  if (d < 0) x *= -1;
}
bool check(long long x, long long y, long long xx, long long yy, long long xxx,
           long long yyy) {
  int sg = sgn(x, y, xxx, yyy);
  return !sg || sgn(x, y, xx, yy) == sg && sgn(xx, yy, xxx, yyy) == sg;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(n); ++i) readNumber(x[i]), readNumber(y[i]);
  for (int i = 0; i < (int)(3 * n); ++i) x[n + i] = x[i], y[n + i] = y[i];
  for (int query = 0; query < (int)(m); ++query) {
    readNumber(sx), readNumber(sy);
    readNumber(tx), readNumber(ty);
    a = sy - ty;
    b = tx - sx;
    c = a * sx + b * sy;
    p.clear();
    sp.clear();
    for (int i = 0; i < (int)(n); ++i)
      if (a * x[i] + b * y[i] == c && a * x[i + 1] + b * y[i + 1] == c) {
        p.push_back(make_pair(x[i], y[i]));
        p.push_back(make_pair(x[i + 1], y[i + 1]));
        sp.push_back(true), sp.push_back(true);
      } else if (a * x[i] + b * y[i] != c && a * x[i + 1] + b * y[i + 1] != c) {
        long long aa = y[i + 1] - y[i];
        long long bb = x[i] - x[i + 1];
        long long cc = aa * x[i] + bb * y[i];
        long long det = a * bb - aa * b;
        if (det == 0) continue;
        double ix = ((double)c * bb - (double)cc * b) / det;
        double iy = ((double)a * cc - (double)aa * c) / det;
        int minX = min(x[i], x[i + 1]);
        int maxX = max(x[i], x[i + 1]);
        int minY = min(y[i], y[i + 1]);
        int maxY = max(y[i], y[i + 1]);
        if (ix > minX - EPS && ix < maxX + EPS && iy > minY - EPS &&
            iy < maxY + EPS) {
          p.push_back(make_pair(ix, iy));
          sp.push_back(false);
        }
      }
    for (int i = 1; i <= n; ++i)
      if (a * x[i] + b * y[i] == c && a * x[i - 1] + b * y[i - 1] != c &&
          a * x[i + 1] + b * y[i + 1] != c) {
        if (check(x[i - 1] - x[i], y[i - 1] - y[i], -b, a, x[i + 1] - x[i],
                  y[i + 1] - y[i]) ||
            check(x[i - 1] - x[i], y[i - 1] - y[i], b, -a, x[i + 1] - x[i],
                  y[i + 1] - y[i])) {
          p.push_back(make_pair(x[i], y[i]));
          sp.push_back(false);
        }
      }
    int ed = -1;
    for (int beg = n; beg < 2 * n;)
      if (a * x[beg] + b * y[beg] == c) {
        int end = beg + 1;
        while (a * x[beg - 1] + b * y[beg - 1] == c) --beg;
        while (a * x[end] + b * y[end] == c) ++end;
        --end;
        if (ed != -1 && end == ed + n) break;
        if (sgn(x[beg] - x[beg - 1], y[beg] - y[beg - 1], x[beg + 1] - x[beg],
                y[beg + 1] - y[beg]) *
                sgn(x[end] - x[end - 1], y[end] - y[end - 1],
                    x[end + 1] - x[end], y[end + 1] - y[end]) <
            0) {
          p.push_back(make_pair(x[end], y[end]));
          sp.push_back(false);
        }
        if (ed == -1) ed = end;
        beg = end + 1;
      } else {
        ++beg;
      }
    pp.clear();
    for (int i = 0; i < (int)(p.size()); ++i)
      pp.push_back(P(p[i].first, p[i].second, sp[i]));
    if (b == 0)
      sort(pp.begin(), pp.end(), cmpY);
    else
      sort(pp.begin(), pp.end(), cmpX);
    for (int i = 0; i < (int)(p.size()); ++i)
      p[i].first = pp[i].first, p[i].second = pp[i].second, sp[i] = pp[i].sp;
    double ans = 0;
    bool curSp = false;
    pair<double, double> spPoint;
    for (int i = 0; i < (int)(p.size()); ++i) {
      if (!curSp && (i & 1)) {
        ans += sqrt(
            (p[i].first - p[i - 1].first) * (p[i].first - p[i - 1].first) +
            (p[i].second - p[i - 1].second) * (p[i].second - p[i - 1].second));
      }
      if (sp[i]) {
        if (curSp) {
          ans += sqrt(
              (spPoint.first - p[i].first) * (spPoint.first - p[i].first) +
              (spPoint.second - p[i].second) * (spPoint.second - p[i].second));
        } else {
          spPoint = p[i];
        }
        curSp = !curSp;
      }
    }
    printf("%.18f\n", ans * 0.01);
  }
  return 0;
}

```