#include <bits/stdc++.h>
using namespace std;
struct Point {
  double x, y;
  Point() {}
  Point(double a, double b) {
    x = a;
    y = b;
  }
};
bool operator==(const Point &a, const Point &b) {
  return abs(a.x - b.x) < 1e-8 && abs(a.y - b.y) < 1e-8;
}
bool operator<(const Point &a, const Point &b) {
  if (abs(a.x - b.x) > 1e-8) return a.x < b.x;
  return a.y + 1e-8 < b.y;
}
Point operator+(const Point &a, const Point &b) {
  return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b) {
  return Point(a.x - b.x, a.y - b.y);
}
Point operator/(const Point &a, double k) { return Point(a.x / k, a.y / k); }
Point operator*(const Point &a, double k) { return Point(a.x * k, a.y * k); }
double dist(const Point &A, const Point &B) {
  return hypot(A.x - B.x, A.y - B.y);
}
double cross(const Point &A, const Point &B) { return A.x * B.y - A.y * B.x; }
bool between(const Point *A, const Point *B, const Point *P) {
  return P->x >= min(A->x, B->x) && P->x <= max(A->x, B->x) && P->y <= B->y;
}
bool pointInPoly(Point P[], int n, const Point *A) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    const Point *pinf = P + i;
    const Point *psup = pinf + 1;
    if (pinf->y > psup->y) swap(pinf, psup);
    double aa = cross(*pinf - *A, *psup - *A);
    if (pinf->y <= A->y) {
      if (aa > 1e-8 && A->y < psup->y)
        cnt++;
      else if (abs(aa) < 1e-8 && between(pinf, psup, A))
        return true;
    }
  }
  return cnt & 1;
}
double f(const Point &A, const Point &B, Point P[], int n) {
  vector<Point> v;
  for (int i = 0; i < n; i++) {
    Point P1 = P[i], P2 = P[i + 1];
    double cp1 = cross(B - A, P2 - P1);
    double cp2 = cross(P1 - A, P2 - P1);
    if (cp1 != 0) {
      Point Q = A + (B - A) * (cp2 / cp1);
      v.push_back(Q);
    } else if (cp2 == 0) {
      v.push_back(P1);
      v.push_back(P2);
    }
  }
  sort(v.begin(), v.end());
  int L = unique(v.begin(), v.end()) - v.begin() - 1;
  assert(L <= 1000);
  double ans = 0;
  for (int i = 0; i < L; i++) {
    Point X = (v[i] + v[i + 1]) / 2;
    if (pointInPoly(P, n, &X)) ans += dist(v[i], v[i + 1]);
  }
  return ans;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  Point P[n + 1];
  for (int i = 0; i < n; i++) scanf("%lf %lf", &P[i].x, &P[i].y);
  P[n] = P[0];
  for (int i = 0; i < m; i++) {
    Point A, B;
    scanf("%lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y);
    printf("%.10lf\n", f(A, B, P, n));
  }
  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;
struct Point {
  double x, y;
  Point() {}
  Point(double a, double b) {
    x = a;
    y = b;
  }
};
bool operator==(const Point &a, const Point &b) {
  return abs(a.x - b.x) < 1e-8 && abs(a.y - b.y) < 1e-8;
}
bool operator<(const Point &a, const Point &b) {
  if (abs(a.x - b.x) > 1e-8) return a.x < b.x;
  return a.y + 1e-8 < b.y;
}
Point operator+(const Point &a, const Point &b) {
  return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b) {
  return Point(a.x - b.x, a.y - b.y);
}
Point operator/(const Point &a, double k) { return Point(a.x / k, a.y / k); }
Point operator*(const Point &a, double k) { return Point(a.x * k, a.y * k); }
double dist(const Point &A, const Point &B) {
  return hypot(A.x - B.x, A.y - B.y);
}
double cross(const Point &A, const Point &B) { return A.x * B.y - A.y * B.x; }
bool between(const Point *A, const Point *B, const Point *P) {
  return P->x >= min(A->x, B->x) && P->x <= max(A->x, B->x) && P->y <= B->y;
}
bool pointInPoly(Point P[], int n, const Point *A) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    const Point *pinf = P + i;
    const Point *psup = pinf + 1;
    if (pinf->y > psup->y) swap(pinf, psup);
    double aa = cross(*pinf - *A, *psup - *A);
    if (pinf->y <= A->y) {
      if (aa > 1e-8 && A->y < psup->y)
        cnt++;
      else if (abs(aa) < 1e-8 && between(pinf, psup, A))
        return true;
    }
  }
  return cnt & 1;
}
double f(const Point &A, const Point &B, Point P[], int n) {
  vector<Point> v;
  for (int i = 0; i < n; i++) {
    Point P1 = P[i], P2 = P[i + 1];
    double cp1 = cross(B - A, P2 - P1);
    double cp2 = cross(P1 - A, P2 - P1);
    if (cp1 != 0) {
      Point Q = A + (B - A) * (cp2 / cp1);
      v.push_back(Q);
    } else if (cp2 == 0) {
      v.push_back(P1);
      v.push_back(P2);
    }
  }
  sort(v.begin(), v.end());
  int L = unique(v.begin(), v.end()) - v.begin() - 1;
  assert(L <= 1000);
  double ans = 0;
  for (int i = 0; i < L; i++) {
    Point X = (v[i] + v[i + 1]) / 2;
    if (pointInPoly(P, n, &X)) ans += dist(v[i], v[i + 1]);
  }
  return ans;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  Point P[n + 1];
  for (int i = 0; i < n; i++) scanf("%lf %lf", &P[i].x, &P[i].y);
  P[n] = P[0];
  for (int i = 0; i < m; i++) {
    Point A, B;
    scanf("%lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y);
    printf("%.10lf\n", f(A, B, P, n));
  }
  return 0;
}

```