#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = 2 * acos(0);
struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
  Point(const Point& p) : x(p.x), y(p.y) {}
  bool operator<(const Point& rhs) const {
    return make_pair(y, x) < make_pair(rhs.y, rhs.x);
  }
  bool operator==(const Point& rhs) const {
    return make_pair(y, x) == make_pair(rhs.y, rhs.x);
  }
  Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }
  Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }
  Point operator*(double c) const { return Point(x * c, y * c); }
  Point operator/(double c) const { return Point(x / c, y / c); }
};
double cross(Point p, Point q) { return p.x * q.y - p.y * q.x; }
double area(Point a, Point b, Point c) {
  return fabs(cross(a, b) + cross(b, c) + cross(c, a)) / 2;
}
double area2(Point a, Point b, Point c) {
  return cross(a, b) + cross(b, c) + cross(c, a);
}
double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; }
double dist(Point p, Point q) { return sqrt(dot(p - q, p - q)); }
double dist2(Point p, Point q) { return dot(p - q, p - q); }
Point RotateCCW90(Point p) { return Point(-p.y, p.x); }
Point RotateCW90(Point p) { return Point(p.y, -p.x); }
Point RotateCCW(Point p, double t) {
  return Point(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t));
}
bool LinesParallel(Point a, Point b, Point c, Point d) {
  return fabs(cross(b - a, c - d)) < 1e-9;
}
bool LinesCollinear(Point a, Point b, Point c, Point d) {
  return LinesParallel(a, b, c, d) && fabs(cross(a - b, a - c)) < 1e-9 &&
         fabs(cross(c - d, c - a)) < 1e-9;
}
bool SegmentsIntersect(Point a, Point b, Point c, Point d) {
  if (LinesCollinear(a, b, c, d)) {
    if (dist2(a, c) < 1e-9 || dist2(a, d) < 1e-9 || dist2(b, c) < 1e-9 ||
        dist2(b, d) < 1e-9)
      return true;
    if (dot(c - a, c - b) > 0 && dot(d - a, d - b) > 0 && dot(c - b, d - b) > 0)
      return false;
    return true;
  }
  if (cross(d - a, b - a) * cross(c - a, b - a) > 0) return false;
  if (cross(a - c, d - c) * cross(b - c, d - c) > 0) return false;
  return true;
}
Point ComputeLineIntersection(Point a, Point b, Point c, Point d) {
  b = b - a;
  d = c - d;
  c = c - a;
  assert(dot(b, b) > 1e-9 && dot(d, d) > 1e-9);
  return a + b * cross(c, d) / cross(b, d);
}
const int maxn = 1010;
int n, m;
void solve() {
  cin >> n >> m;
  vector<Point> poly;
  for (int i = (0); i < (n); i++) {
    double x, y;
    cin >> x >> y;
    poly.push_back(Point(x, y));
  }
  for (int i = (0); i < (m); i++) {
    Point a, b;
    cin >> a.x >> a.y >> b.x >> b.y;
    vector<pair<Point, int> > vp;
    for (int j = (0); j < (n); j++) {
      Point u = poly[j], v = poly[(j + 1) % n], w = poly[(j + n - 1) % n];
      if (!LinesParallel(a, b, u, v) &&
          cross(u - a, b - a) * cross(v - a, b - a) < 0) {
        Point p = ComputeLineIntersection(a, b, u, v);
        if (cross(u - a, b - a) > 0)
          vp.push_back(make_pair(p, 2));
        else
          vp.push_back(make_pair(p, -2));
      }
      if (fabs(cross(u - a, b - a)) < 1e-9) {
        int r = 0;
        if (cross(w - a, b - a) > 1e-9)
          r++;
        else if (cross(w - a, b - a) < -1e-9)
          r--;
        if (cross(v - a, b - a) > 1e-9)
          r--;
        else if (cross(v - a, b - a) < -1e-9)
          r++;
        vp.push_back(make_pair(u, r));
      }
    }
    sort((vp).begin(), (vp).end());
    double ans = 0;
    int tot = 0;
    for (int j = (0); j < (int((vp).size()) - 1); j++) {
      tot += vp[j].second;
      if (tot) ans += dist(vp[j].first, vp[j + 1].first);
    }
    cout << fixed << setprecision(9) << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  solve();
  return 0;
}

### Prompt

Your challenge is to write 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;
long long gcd(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = 2 * acos(0);
struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
  Point(const Point& p) : x(p.x), y(p.y) {}
  bool operator<(const Point& rhs) const {
    return make_pair(y, x) < make_pair(rhs.y, rhs.x);
  }
  bool operator==(const Point& rhs) const {
    return make_pair(y, x) == make_pair(rhs.y, rhs.x);
  }
  Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }
  Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }
  Point operator*(double c) const { return Point(x * c, y * c); }
  Point operator/(double c) const { return Point(x / c, y / c); }
};
double cross(Point p, Point q) { return p.x * q.y - p.y * q.x; }
double area(Point a, Point b, Point c) {
  return fabs(cross(a, b) + cross(b, c) + cross(c, a)) / 2;
}
double area2(Point a, Point b, Point c) {
  return cross(a, b) + cross(b, c) + cross(c, a);
}
double dot(Point p, Point q) { return p.x * q.x + p.y * q.y; }
double dist(Point p, Point q) { return sqrt(dot(p - q, p - q)); }
double dist2(Point p, Point q) { return dot(p - q, p - q); }
Point RotateCCW90(Point p) { return Point(-p.y, p.x); }
Point RotateCW90(Point p) { return Point(p.y, -p.x); }
Point RotateCCW(Point p, double t) {
  return Point(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t));
}
bool LinesParallel(Point a, Point b, Point c, Point d) {
  return fabs(cross(b - a, c - d)) < 1e-9;
}
bool LinesCollinear(Point a, Point b, Point c, Point d) {
  return LinesParallel(a, b, c, d) && fabs(cross(a - b, a - c)) < 1e-9 &&
         fabs(cross(c - d, c - a)) < 1e-9;
}
bool SegmentsIntersect(Point a, Point b, Point c, Point d) {
  if (LinesCollinear(a, b, c, d)) {
    if (dist2(a, c) < 1e-9 || dist2(a, d) < 1e-9 || dist2(b, c) < 1e-9 ||
        dist2(b, d) < 1e-9)
      return true;
    if (dot(c - a, c - b) > 0 && dot(d - a, d - b) > 0 && dot(c - b, d - b) > 0)
      return false;
    return true;
  }
  if (cross(d - a, b - a) * cross(c - a, b - a) > 0) return false;
  if (cross(a - c, d - c) * cross(b - c, d - c) > 0) return false;
  return true;
}
Point ComputeLineIntersection(Point a, Point b, Point c, Point d) {
  b = b - a;
  d = c - d;
  c = c - a;
  assert(dot(b, b) > 1e-9 && dot(d, d) > 1e-9);
  return a + b * cross(c, d) / cross(b, d);
}
const int maxn = 1010;
int n, m;
void solve() {
  cin >> n >> m;
  vector<Point> poly;
  for (int i = (0); i < (n); i++) {
    double x, y;
    cin >> x >> y;
    poly.push_back(Point(x, y));
  }
  for (int i = (0); i < (m); i++) {
    Point a, b;
    cin >> a.x >> a.y >> b.x >> b.y;
    vector<pair<Point, int> > vp;
    for (int j = (0); j < (n); j++) {
      Point u = poly[j], v = poly[(j + 1) % n], w = poly[(j + n - 1) % n];
      if (!LinesParallel(a, b, u, v) &&
          cross(u - a, b - a) * cross(v - a, b - a) < 0) {
        Point p = ComputeLineIntersection(a, b, u, v);
        if (cross(u - a, b - a) > 0)
          vp.push_back(make_pair(p, 2));
        else
          vp.push_back(make_pair(p, -2));
      }
      if (fabs(cross(u - a, b - a)) < 1e-9) {
        int r = 0;
        if (cross(w - a, b - a) > 1e-9)
          r++;
        else if (cross(w - a, b - a) < -1e-9)
          r--;
        if (cross(v - a, b - a) > 1e-9)
          r--;
        else if (cross(v - a, b - a) < -1e-9)
          r++;
        vp.push_back(make_pair(u, r));
      }
    }
    sort((vp).begin(), (vp).end());
    double ans = 0;
    int tot = 0;
    for (int j = (0); j < (int((vp).size()) - 1); j++) {
      tot += vp[j].second;
      if (tot) ans += dist(vp[j].first, vp[j + 1].first);
    }
    cout << fixed << setprecision(9) << ans << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  solve();
  return 0;
}

```