#include <bits/stdc++.h>
using namespace std;
struct PT {
  long long x, y;
  PT() {}
  PT(long long x, long long y) : x(x), y(y) {}
  PT(const PT &p) : x(p.x), y(p.y) {}
  PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
  PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
  PT operator*(long long c) const { return PT(x * c, y * c); }
  PT operator/(long long c) const { return PT(x / c, y / c); }
};
long long dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
long long dist2(PT p, PT q) { return dot(p - q, p - q); }
long long cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }
long long cross(PT a, PT b, PT c) { return cross(b - a, c - a); }
ostream &operator<<(ostream &os, const PT &p) {
  return os << "(" << p.x << "," << p.y << ")";
}
struct Fraction {
  long long a, b;
  Fraction(long long _a, long long _b) {
    a = _a;
    b = _b;
    assert(b);
    if (b < 0) b = -b, a = -a;
  }
  bool operator<(const Fraction &o) const {
    return 1.0L * a / b < 1.0L * o.a / o.b;
  }
};
Fraction distance(PT O, PT X, PT A, PT B) {
  B = B - A;
  A = A - O;
  X = X - O;
  assert(cross(X, B));
  return Fraction(cross(A, B), cross(X, B));
}
int sign(long long a) { return a == 0 ? 0 : (a > 0 ? 1 : -1); }
const int PR = 100;
PT inputPoint() {
  double x, y;
  cin >> x >> y;
  return PT(round(PR * x), round(PR * y));
}
double LinePolygonIntersection(PT A, PT B, const vector<PT> &p) {
  int n = p.size();
  vector<pair<Fraction, int>> special;
  for (int i = 0; i < n; i++) {
    PT X = p[i], Y = p[(i + 1) % n], W = p[(i - 1 + n) % n];
    long long crx = cross(A, B, X), cry = cross(A, B, Y), crw = cross(A, B, W);
    if (crx == 0) {
      Fraction f(0, 1);
      if (B.x != A.x)
        f = Fraction(X.x - A.x, B.x - A.x);
      else
        f = Fraction(X.y - A.y, B.y - A.y);
      if (sign(crw) && sign(cry)) {
        if (sign(crw) != sign(cry)) special.push_back({f, 0});
      } else if (sign(cry))
        special.push_back({f, sign(cry)});
      else if (sign(crw))
        special.push_back({f, sign(crw)});
    } else if (sign(crx) == -sign(cry)) {
      Fraction f = distance(A, B, X, Y);
      special.push_back({f, 0});
    }
  }
  sort(special.begin(), special.end());
  bool active = false;
  int sgn = 0;
  double prv = 0, ans = 0;
  for (auto &pr : special) {
    double d = 1.0 * pr.first.a / pr.first.b;
    int tp = pr.second;
    if (sgn) {
      assert(sgn && tp);
      if (sgn != tp) active = !active;
      ans += d - prv;
      sgn = 0;
    } else {
      if (active) ans += d - prv;
      if (tp == 0)
        active = !active;
      else
        sgn = tp;
    }
    prv = d;
  }
  return ans * sqrt(dot(B - A, B - A));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<PT> p(n);
  for (int i = 0; i < n; i++) p[i] = inputPoint();
  while (m--) {
    PT A, B;
    A = inputPoint();
    B = inputPoint();
    cout << setprecision(15) << fixed << LinePolygonIntersection(A, B, p) / PR
         << endl;
  }
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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 PT {
  long long x, y;
  PT() {}
  PT(long long x, long long y) : x(x), y(y) {}
  PT(const PT &p) : x(p.x), y(p.y) {}
  PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
  PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
  PT operator*(long long c) const { return PT(x * c, y * c); }
  PT operator/(long long c) const { return PT(x / c, y / c); }
};
long long dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
long long dist2(PT p, PT q) { return dot(p - q, p - q); }
long long cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }
long long cross(PT a, PT b, PT c) { return cross(b - a, c - a); }
ostream &operator<<(ostream &os, const PT &p) {
  return os << "(" << p.x << "," << p.y << ")";
}
struct Fraction {
  long long a, b;
  Fraction(long long _a, long long _b) {
    a = _a;
    b = _b;
    assert(b);
    if (b < 0) b = -b, a = -a;
  }
  bool operator<(const Fraction &o) const {
    return 1.0L * a / b < 1.0L * o.a / o.b;
  }
};
Fraction distance(PT O, PT X, PT A, PT B) {
  B = B - A;
  A = A - O;
  X = X - O;
  assert(cross(X, B));
  return Fraction(cross(A, B), cross(X, B));
}
int sign(long long a) { return a == 0 ? 0 : (a > 0 ? 1 : -1); }
const int PR = 100;
PT inputPoint() {
  double x, y;
  cin >> x >> y;
  return PT(round(PR * x), round(PR * y));
}
double LinePolygonIntersection(PT A, PT B, const vector<PT> &p) {
  int n = p.size();
  vector<pair<Fraction, int>> special;
  for (int i = 0; i < n; i++) {
    PT X = p[i], Y = p[(i + 1) % n], W = p[(i - 1 + n) % n];
    long long crx = cross(A, B, X), cry = cross(A, B, Y), crw = cross(A, B, W);
    if (crx == 0) {
      Fraction f(0, 1);
      if (B.x != A.x)
        f = Fraction(X.x - A.x, B.x - A.x);
      else
        f = Fraction(X.y - A.y, B.y - A.y);
      if (sign(crw) && sign(cry)) {
        if (sign(crw) != sign(cry)) special.push_back({f, 0});
      } else if (sign(cry))
        special.push_back({f, sign(cry)});
      else if (sign(crw))
        special.push_back({f, sign(crw)});
    } else if (sign(crx) == -sign(cry)) {
      Fraction f = distance(A, B, X, Y);
      special.push_back({f, 0});
    }
  }
  sort(special.begin(), special.end());
  bool active = false;
  int sgn = 0;
  double prv = 0, ans = 0;
  for (auto &pr : special) {
    double d = 1.0 * pr.first.a / pr.first.b;
    int tp = pr.second;
    if (sgn) {
      assert(sgn && tp);
      if (sgn != tp) active = !active;
      ans += d - prv;
      sgn = 0;
    } else {
      if (active) ans += d - prv;
      if (tp == 0)
        active = !active;
      else
        sgn = tp;
    }
    prv = d;
  }
  return ans * sqrt(dot(B - A, B - A));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<PT> p(n);
  for (int i = 0; i < n; i++) p[i] = inputPoint();
  while (m--) {
    PT A, B;
    A = inputPoint();
    B = inputPoint();
    cout << setprecision(15) << fixed << LinePolygonIntersection(A, B, p) / PR
         << endl;
  }
}

```