#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1E9;
int N;
const double PI = acos(-1.0);
class pt2 {
 public:
  long long x, y;
  pt2() {}
  pt2(long long _x, long long _y) {
    x = _x;
    y = _y;
  }
  pt2 vec2(const pt2 &p) { return pt2(p.x - x, p.y - y); }
  double dist(const pt2 &p) {
    long long dx = x - p.x, dy = y - p.y;
    return sqrt(dx * dx + dy * dy);
  }
  pair<double, double> getPDD() { return pair<double, double>(x, y); }
  long long dot(const pt2 &p) { return x * p.x + y * p.y; }
  long long cross(const pt2 &p) { return x * p.y - y * p.x; }
  pt2 operator+(const pt2 &p) { return pt2(x + p.x, y + p.y); }
  pt2 operator-(const pt2 &p) { return pt2(x - p.x, y - p.y); }
  pt2 operator*(const double &t) { return pt2(x * t, y * t); }
  pt2 operator/(const double &t) { return pt2(x / t, y / t); }
  bool operator==(const pt2 &p) { return x == p.x && y == p.y; }
} P[1005];
long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }
class line2 {
 public:
  long long a, b, c;
  line2() {}
  line2(long long _a, long long _b, long long _c) {
    a = _a;
    b = _b;
    c = _c;
  }
  line2(const pt2 &p1, const pt2 &p2) {
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x * p1.y - p1.x * p2.y;
    long long g = gcd(gcd(a, b), c);
    a /= g;
    b /= g;
    c /= g;
  }
  bool passPoint(const pt2 &p) { return p.x * a + p.y * b + c == 0; }
  pair<double, double> intersect(const line2 &l) {
    return pair<double, double>(
        (double)(b * l.c - c * l.b) / (a * l.b - b * l.a),
        (double)(a * l.c - c * l.a) / (b * l.a - a * l.b));
  }
  bool same(const line2 &l) {
    if (!parallel(l)) return 0;
    if (a != 0) return a * l.c == c * l.a;
    return b * l.c == c * l.b;
  }
  bool parallel(const line2 &l) { return a * l.b == b * l.a; }
};
int side(pt2 o, pt2 &a, pt2 &b) {
  pt2 va = o.vec2(a), vb = o.vec2(b);
  long long d = va.cross(vb);
  if (d == 0) return 0;
  return d > 0 ? 1 : -1;
}
bool intersectLineSeg(pt2 &p1, pt2 &p2, pt2 &a, pt2 &b) {
  return side(p1, p2, a) != side(p1, p2, b);
}
int M, K;
line2 lines[1005];
pair<double, pair<pair<double, double>, int> > pts[1005 * 2];
long long getValue() {
  char str[15];
  scanf("%s", str);
  long long x = 0;
  int neg = 1, needFloat = -1;
  for (int i = (0); i < (strlen(str)); i++) {
    if (str[i] == '-')
      neg = -1;
    else if (str[i] == '.')
      needFloat = 2;
    else {
      if (needFloat > 0) needFloat--;
      x = x * 10 + (str[i] - '0');
    }
  }
  if (needFloat == -1) return neg * x * 100;
  while (needFloat--) x *= 10;
  return x * neg;
}
int main() {
  cin >> N >> M;
  for (int i = (0); i < (N); i++) P[i] = pt2(getValue(), getValue());
  bool change = 1;
  while (change) {
    change = 0;
    for (int i = (0); i < (N); i++) {
      int pi = (i + N - 1) % N, ni = (i + 1) % N;
      line2 l1(P[pi], P[i]), l2(P[i], P[ni]);
      if (l1.parallel(l2)) {
        for (int j = (i); j < (N - 1); j++) P[j] = P[j + 1];
        change = 1;
        N--;
        break;
      }
    }
  }
  for (int i = (0); i < (N); i++) lines[i] = line2(P[i], P[(i + 1) % N]);
  while (M--) {
    pt2 p1(getValue(), getValue()), p2(getValue(), getValue());
    line2 l(p1, p2);
    bool vert = l.b == 0;
    K = 0;
    double ans = 0;
    for (int i = (0); i < (N); i++) {
      int pi = (i + N - 1) % N, ni = (i + 1) % N;
      line2 &l2 = lines[i];
      if (l.same(l2)) {
        ans += P[i].dist(P[ni]);
        int nni = (i + 2) % N,
            t2 = intersectLineSeg(p1, p2, P[pi], P[nni]) ? 2 : 1;
        pts[K++] =
            make_pair(vert ? P[i].y : P[i].x, make_pair(P[i].getPDD(), 1));
        pts[K++] =
            make_pair(vert ? P[ni].y : P[ni].x, make_pair(P[ni].getPDD(), t2));
        continue;
      }
      if (l.passPoint(P[i]) || l.passPoint(P[ni])) {
        if (l.passPoint(P[pi])) continue;
        if (l.passPoint(P[i])) {
          if (intersectLineSeg(p1, p2, P[pi], P[ni])) {
            pts[K++] =
                make_pair(vert ? P[i].y : P[i].x, make_pair(P[i].getPDD(), 0));
          }
        }
        continue;
      }
      if (!intersectLineSeg(p1, p2, P[i], P[ni])) continue;
      pair<double, double> p = l.intersect(l2);
      pts[K++] = make_pair(vert ? p.second : p.first, make_pair(p, 0));
    }
    sort(pts, pts + K);
    bool inner = 0;
    pair<double, double> plast;
    for (int i = (0); i < (K); i++) {
      pair<double, double> &p = pts[i].second.first;
      inner = !inner;
      if (!inner) {
        double dx = p.first - plast.first, dy = p.second - plast.second;
        ans += sqrt(dx * dx + dy * dy);
      }
      plast = pts[i].second.first;
      if (pts[i].second.second > 0) {
        if (max(pts[i].second.second, pts[i + 1].second.second) == 1)
          inner = !inner;
        plast = pts[i + 1].second.first;
        i++;
      }
    }
    printf("%.12f\n", ans / 100.0);
  }
  return 0;
}

### Prompt

Please formulate 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 int INF = (int)1E9;
int N;
const double PI = acos(-1.0);
class pt2 {
 public:
  long long x, y;
  pt2() {}
  pt2(long long _x, long long _y) {
    x = _x;
    y = _y;
  }
  pt2 vec2(const pt2 &p) { return pt2(p.x - x, p.y - y); }
  double dist(const pt2 &p) {
    long long dx = x - p.x, dy = y - p.y;
    return sqrt(dx * dx + dy * dy);
  }
  pair<double, double> getPDD() { return pair<double, double>(x, y); }
  long long dot(const pt2 &p) { return x * p.x + y * p.y; }
  long long cross(const pt2 &p) { return x * p.y - y * p.x; }
  pt2 operator+(const pt2 &p) { return pt2(x + p.x, y + p.y); }
  pt2 operator-(const pt2 &p) { return pt2(x - p.x, y - p.y); }
  pt2 operator*(const double &t) { return pt2(x * t, y * t); }
  pt2 operator/(const double &t) { return pt2(x / t, y / t); }
  bool operator==(const pt2 &p) { return x == p.x && y == p.y; }
} P[1005];
long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }
class line2 {
 public:
  long long a, b, c;
  line2() {}
  line2(long long _a, long long _b, long long _c) {
    a = _a;
    b = _b;
    c = _c;
  }
  line2(const pt2 &p1, const pt2 &p2) {
    a = p2.y - p1.y;
    b = p1.x - p2.x;
    c = p2.x * p1.y - p1.x * p2.y;
    long long g = gcd(gcd(a, b), c);
    a /= g;
    b /= g;
    c /= g;
  }
  bool passPoint(const pt2 &p) { return p.x * a + p.y * b + c == 0; }
  pair<double, double> intersect(const line2 &l) {
    return pair<double, double>(
        (double)(b * l.c - c * l.b) / (a * l.b - b * l.a),
        (double)(a * l.c - c * l.a) / (b * l.a - a * l.b));
  }
  bool same(const line2 &l) {
    if (!parallel(l)) return 0;
    if (a != 0) return a * l.c == c * l.a;
    return b * l.c == c * l.b;
  }
  bool parallel(const line2 &l) { return a * l.b == b * l.a; }
};
int side(pt2 o, pt2 &a, pt2 &b) {
  pt2 va = o.vec2(a), vb = o.vec2(b);
  long long d = va.cross(vb);
  if (d == 0) return 0;
  return d > 0 ? 1 : -1;
}
bool intersectLineSeg(pt2 &p1, pt2 &p2, pt2 &a, pt2 &b) {
  return side(p1, p2, a) != side(p1, p2, b);
}
int M, K;
line2 lines[1005];
pair<double, pair<pair<double, double>, int> > pts[1005 * 2];
long long getValue() {
  char str[15];
  scanf("%s", str);
  long long x = 0;
  int neg = 1, needFloat = -1;
  for (int i = (0); i < (strlen(str)); i++) {
    if (str[i] == '-')
      neg = -1;
    else if (str[i] == '.')
      needFloat = 2;
    else {
      if (needFloat > 0) needFloat--;
      x = x * 10 + (str[i] - '0');
    }
  }
  if (needFloat == -1) return neg * x * 100;
  while (needFloat--) x *= 10;
  return x * neg;
}
int main() {
  cin >> N >> M;
  for (int i = (0); i < (N); i++) P[i] = pt2(getValue(), getValue());
  bool change = 1;
  while (change) {
    change = 0;
    for (int i = (0); i < (N); i++) {
      int pi = (i + N - 1) % N, ni = (i + 1) % N;
      line2 l1(P[pi], P[i]), l2(P[i], P[ni]);
      if (l1.parallel(l2)) {
        for (int j = (i); j < (N - 1); j++) P[j] = P[j + 1];
        change = 1;
        N--;
        break;
      }
    }
  }
  for (int i = (0); i < (N); i++) lines[i] = line2(P[i], P[(i + 1) % N]);
  while (M--) {
    pt2 p1(getValue(), getValue()), p2(getValue(), getValue());
    line2 l(p1, p2);
    bool vert = l.b == 0;
    K = 0;
    double ans = 0;
    for (int i = (0); i < (N); i++) {
      int pi = (i + N - 1) % N, ni = (i + 1) % N;
      line2 &l2 = lines[i];
      if (l.same(l2)) {
        ans += P[i].dist(P[ni]);
        int nni = (i + 2) % N,
            t2 = intersectLineSeg(p1, p2, P[pi], P[nni]) ? 2 : 1;
        pts[K++] =
            make_pair(vert ? P[i].y : P[i].x, make_pair(P[i].getPDD(), 1));
        pts[K++] =
            make_pair(vert ? P[ni].y : P[ni].x, make_pair(P[ni].getPDD(), t2));
        continue;
      }
      if (l.passPoint(P[i]) || l.passPoint(P[ni])) {
        if (l.passPoint(P[pi])) continue;
        if (l.passPoint(P[i])) {
          if (intersectLineSeg(p1, p2, P[pi], P[ni])) {
            pts[K++] =
                make_pair(vert ? P[i].y : P[i].x, make_pair(P[i].getPDD(), 0));
          }
        }
        continue;
      }
      if (!intersectLineSeg(p1, p2, P[i], P[ni])) continue;
      pair<double, double> p = l.intersect(l2);
      pts[K++] = make_pair(vert ? p.second : p.first, make_pair(p, 0));
    }
    sort(pts, pts + K);
    bool inner = 0;
    pair<double, double> plast;
    for (int i = (0); i < (K); i++) {
      pair<double, double> &p = pts[i].second.first;
      inner = !inner;
      if (!inner) {
        double dx = p.first - plast.first, dy = p.second - plast.second;
        ans += sqrt(dx * dx + dy * dy);
      }
      plast = pts[i].second.first;
      if (pts[i].second.second > 0) {
        if (max(pts[i].second.second, pts[i + 1].second.second) == 1)
          inner = !inner;
        plast = pts[i + 1].second.first;
        i++;
      }
    }
    printf("%.12f\n", ans / 100.0);
  }
  return 0;
}

```