#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
const long double inf = 1e100;
const long double eps = 1e-9;
const long double PI = acos(-1.0);
int sign(long double x) { return (x > eps) - (x < -eps); }
struct PT {
  long double x, y;
  PT() {}
  PT(long double x, long double y) : x(x), y(y) {}
  PT(const PT &p) : x(p.x), y(p.y) {}
  PT operator+(const PT &a) const { return PT(x + a.x, y + a.y); }
  PT operator-(const PT &a) const { return PT(x - a.x, y - a.y); }
  PT operator*(const long double a) const { return PT(x * a, y * a); }
  friend PT operator*(const long double &a, const PT &b) {
    return PT(a * b.x, a * b.y);
  }
  PT operator/(const long double a) const { return PT(x / a, y / a); }
  bool operator==(PT a) const {
    return sign(a.x - x) == 0 && sign(a.y - y) == 0;
  }
  bool operator<(PT a) const { return sign(a.x - x) == 0 ? y < a.y : x < a.x; }
  bool operator>(PT a) const { return sign(a.x - x) == 0 ? y > a.y : x > a.x; }
  long double norm() { return sqrt(x * x + y * y); }
  long double norm2() { return x * x + y * y; }
  long double arg() { return atan2(y, x); }
  PT truncate(long double r) {
    long double k = norm();
    if (!sign(k)) return *this;
    r /= k;
    return PT(x * r, y * r);
  }
};
long double dot(PT a, PT b) { return a.x * b.x + a.y * b.y; }
long double dist2(PT a, PT b) { return dot(a - b, a - b); }
long double dist(PT a, PT b) { return sqrt(dot(a - b, a - b)); }
long double cross(PT a, PT b) { return a.x * b.y - a.y * b.x; }
int orientation(PT a, PT b, PT c) { return sign(cross(b - a, c - a)); }
struct line {
  PT a, b;
  PT v;
  long double c;
  line() {}
  line(PT v, long double c) : v(v), c(c) {
    auto p = get_points();
    a = p.first;
    b = p.second;
  }
  line(long double _a, long double _b, long double _c) : v({_b, -_a}), c(_c) {
    auto p = get_points();
    a = p.first;
    b = p.second;
  }
  line(PT p, PT q) : v(q - p), c(cross(v, p)), a(p), b(q) {}
  pair<PT, PT> get_points() {
    PT p, q;
    long double a = -v.y, b = v.x;
    if (sign(a) == 0) {
      p = PT(0, -c / b);
      q = PT(1, -c / b);
    } else if (sign(b) == 0) {
      p = PT(-c / a, 0);
      q = PT(-c / a, 1);
    } else {
      p = PT(0, -c / b);
      q = PT(1, (-c - a) / b);
    }
    return {p, q};
  }
  array<long double, 3> get_abc() {
    long double a = -v.y, b = v.x;
    return {a, b, c};
  }
  bool side(PT p) { return sign(cross(v, p) - c); }
  line translate(PT t) { return {v, c + cross(v, t)}; }
  bool cmp_by_projection(PT p, PT q) { return dot(v, p) < dot(v, q); }
  line shift_left(long double d) { return {v, c + d * v.norm()}; }
};
long double polygon_line_intersection(vector<PT> &p, PT a, PT b) {
  int n = p.size();
  p.push_back(p[0]);
  line l = line(a, b);
  long double ans = 0.0;
  vector<pair<long double, int> > vec;
  for (int i = 0; i < n; i++) {
    int s1 = sign(cross(b - a, p[i] - a));
    int s2 = sign(cross(b - a, p[i + 1] - a));
    if (s1 == s2) continue;
    line t = line(p[i], p[i + 1]);
    PT inter = (t.v * l.c - l.v * t.c) / cross(l.v, t.v);
    long double tmp = dot(inter, l.v);
    int f;
    if (s1 > s2)
      f = s1 && s2 ? 2 : 1;
    else
      f = s1 && s2 ? -2 : -1;
    vec.push_back(make_pair(tmp, f));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0, j = 0; i + 1 < (int)vec.size(); i++) {
    j += vec[i].second;
    if (j) ans += vec[i + 1].first - vec[i].first;
  }
  ans = ans / sqrt(dot(l.v, l.v));
  p.pop_back();
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<PT> p(n);
  for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;
  while (m--) {
    PT a, b;
    cin >> a.x >> a.y >> b.x >> b.y;
    auto ans = polygon_line_intersection(p, a, b);
    cout << fixed << setprecision(10) << ans << '\n';
  }
  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 int N = 3e5 + 9;
const long double inf = 1e100;
const long double eps = 1e-9;
const long double PI = acos(-1.0);
int sign(long double x) { return (x > eps) - (x < -eps); }
struct PT {
  long double x, y;
  PT() {}
  PT(long double x, long double y) : x(x), y(y) {}
  PT(const PT &p) : x(p.x), y(p.y) {}
  PT operator+(const PT &a) const { return PT(x + a.x, y + a.y); }
  PT operator-(const PT &a) const { return PT(x - a.x, y - a.y); }
  PT operator*(const long double a) const { return PT(x * a, y * a); }
  friend PT operator*(const long double &a, const PT &b) {
    return PT(a * b.x, a * b.y);
  }
  PT operator/(const long double a) const { return PT(x / a, y / a); }
  bool operator==(PT a) const {
    return sign(a.x - x) == 0 && sign(a.y - y) == 0;
  }
  bool operator<(PT a) const { return sign(a.x - x) == 0 ? y < a.y : x < a.x; }
  bool operator>(PT a) const { return sign(a.x - x) == 0 ? y > a.y : x > a.x; }
  long double norm() { return sqrt(x * x + y * y); }
  long double norm2() { return x * x + y * y; }
  long double arg() { return atan2(y, x); }
  PT truncate(long double r) {
    long double k = norm();
    if (!sign(k)) return *this;
    r /= k;
    return PT(x * r, y * r);
  }
};
long double dot(PT a, PT b) { return a.x * b.x + a.y * b.y; }
long double dist2(PT a, PT b) { return dot(a - b, a - b); }
long double dist(PT a, PT b) { return sqrt(dot(a - b, a - b)); }
long double cross(PT a, PT b) { return a.x * b.y - a.y * b.x; }
int orientation(PT a, PT b, PT c) { return sign(cross(b - a, c - a)); }
struct line {
  PT a, b;
  PT v;
  long double c;
  line() {}
  line(PT v, long double c) : v(v), c(c) {
    auto p = get_points();
    a = p.first;
    b = p.second;
  }
  line(long double _a, long double _b, long double _c) : v({_b, -_a}), c(_c) {
    auto p = get_points();
    a = p.first;
    b = p.second;
  }
  line(PT p, PT q) : v(q - p), c(cross(v, p)), a(p), b(q) {}
  pair<PT, PT> get_points() {
    PT p, q;
    long double a = -v.y, b = v.x;
    if (sign(a) == 0) {
      p = PT(0, -c / b);
      q = PT(1, -c / b);
    } else if (sign(b) == 0) {
      p = PT(-c / a, 0);
      q = PT(-c / a, 1);
    } else {
      p = PT(0, -c / b);
      q = PT(1, (-c - a) / b);
    }
    return {p, q};
  }
  array<long double, 3> get_abc() {
    long double a = -v.y, b = v.x;
    return {a, b, c};
  }
  bool side(PT p) { return sign(cross(v, p) - c); }
  line translate(PT t) { return {v, c + cross(v, t)}; }
  bool cmp_by_projection(PT p, PT q) { return dot(v, p) < dot(v, q); }
  line shift_left(long double d) { return {v, c + d * v.norm()}; }
};
long double polygon_line_intersection(vector<PT> &p, PT a, PT b) {
  int n = p.size();
  p.push_back(p[0]);
  line l = line(a, b);
  long double ans = 0.0;
  vector<pair<long double, int> > vec;
  for (int i = 0; i < n; i++) {
    int s1 = sign(cross(b - a, p[i] - a));
    int s2 = sign(cross(b - a, p[i + 1] - a));
    if (s1 == s2) continue;
    line t = line(p[i], p[i + 1]);
    PT inter = (t.v * l.c - l.v * t.c) / cross(l.v, t.v);
    long double tmp = dot(inter, l.v);
    int f;
    if (s1 > s2)
      f = s1 && s2 ? 2 : 1;
    else
      f = s1 && s2 ? -2 : -1;
    vec.push_back(make_pair(tmp, f));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0, j = 0; i + 1 < (int)vec.size(); i++) {
    j += vec[i].second;
    if (j) ans += vec[i + 1].first - vec[i].first;
  }
  ans = ans / sqrt(dot(l.v, l.v));
  p.pop_back();
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<PT> p(n);
  for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;
  while (m--) {
    PT a, b;
    cin >> a.x >> a.y >> b.x >> b.y;
    auto ans = polygon_line_intersection(p, a, b);
    cout << fixed << setprecision(10) << ans << '\n';
  }
  return 0;
}

```