#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
  return (a / gcd<T>(a, b) * b);
}
template <class T>
inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-') sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
const int INF = 2000000009;
const int MX = 100005;
const long double EPS = 1e-9;
const int MOD = 1000000007;
struct PT {
  long double x, y;
  PT() {}
  PT(long double x, long double y) : x(x), y(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 double c) const { return PT(x * c, y * c); }
  PT operator/(long double c) const { return PT(x / c, y / c); }
};
long double cross(PT a, PT b) { return a.x * b.y - a.y * b.x; }
long double dot(PT a, PT b) { return a.x * b.x + a.y * b.y; }
long double area2(PT a, PT b, PT c) {
  return cross(a, b) + cross(b, c) + cross(c, a);
}
long double dist2(PT a, PT b) { return dot(b - a, b - a); }
inline int sign(long double a) {
  int ret = (a < -EPS) ? -1 : ((a > EPS) ? 1 : 0);
  return ret;
}
struct Line {
  PT v;
  long double a, b, c;
  Line() {}
  Line(PT p, PT q) : v(q - p), a(-v.y), b(v.x), c(cross(v, p)) {}
  long double side(PT p) { return cross(v, p) - c; }
};
long double commonLength(vector<PT> &p, PT a, PT b) {
  Line l = Line(a, b);
  long double ans = 0.0;
  int n = p.size();
  p.push_back(p[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;
}
int main() {
  int n = ({
    int a;
    read(a);
    a;
  }),
      m = ({
        int a;
        read(a);
        a;
      });
  vector<PT> pol;
  for (int i = 0; i < n; i++) {
    long double x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
                y = ({
                  long double a;
                  scanf("%Lf", &a);
                  a;
                });
    pol.push_back(PT(x, y));
  }
  while (m--) {
    PT a, b;
    a.x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    a.y = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    b.x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    b.y = ({
      long double a;
      scanf("%Lf", &a);
      a;
    });
    printf("%.8Lf\n", commonLength(pol, a, b));
  }
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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;
template <class T>
T gcd(T a, T b) {
  return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
  return (a / gcd<T>(a, b) * b);
}
template <class T>
inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-') sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
const int INF = 2000000009;
const int MX = 100005;
const long double EPS = 1e-9;
const int MOD = 1000000007;
struct PT {
  long double x, y;
  PT() {}
  PT(long double x, long double y) : x(x), y(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 double c) const { return PT(x * c, y * c); }
  PT operator/(long double c) const { return PT(x / c, y / c); }
};
long double cross(PT a, PT b) { return a.x * b.y - a.y * b.x; }
long double dot(PT a, PT b) { return a.x * b.x + a.y * b.y; }
long double area2(PT a, PT b, PT c) {
  return cross(a, b) + cross(b, c) + cross(c, a);
}
long double dist2(PT a, PT b) { return dot(b - a, b - a); }
inline int sign(long double a) {
  int ret = (a < -EPS) ? -1 : ((a > EPS) ? 1 : 0);
  return ret;
}
struct Line {
  PT v;
  long double a, b, c;
  Line() {}
  Line(PT p, PT q) : v(q - p), a(-v.y), b(v.x), c(cross(v, p)) {}
  long double side(PT p) { return cross(v, p) - c; }
};
long double commonLength(vector<PT> &p, PT a, PT b) {
  Line l = Line(a, b);
  long double ans = 0.0;
  int n = p.size();
  p.push_back(p[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;
}
int main() {
  int n = ({
    int a;
    read(a);
    a;
  }),
      m = ({
        int a;
        read(a);
        a;
      });
  vector<PT> pol;
  for (int i = 0; i < n; i++) {
    long double x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
                y = ({
                  long double a;
                  scanf("%Lf", &a);
                  a;
                });
    pol.push_back(PT(x, y));
  }
  while (m--) {
    PT a, b;
    a.x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    a.y = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    b.x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    b.y = ({
      long double a;
      scanf("%Lf", &a);
      a;
    });
    printf("%.8Lf\n", commonLength(pol, a, b));
  }
  return 0;
}

```