#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
struct Point {
  double x, y;
  Point(double _ = 0, double __ = 0) { x = _, y = __; }
  bool operator<(const Point &_) const {
    if (fabs(x - _.x) > 1e-10) return x < _.x;
    return y < _.y;
  }
} po[N], rec[N * 2], stk[N * 2];
struct Line {
  Point a, b;
} L;
struct Mem {
  Point a;
  int type;
  bool operator<(const Mem &_) const {
    if (fabs(a.x - _.a.x) > 1e-10) return a.x < _.a.x;
    if (fabs(a.y - _.a.y) > 1e-10) return a.y < _.a.y;
    return type < _.type;
  }
} pp[N * 2];
int n, m, nxt[N], lst[N];
inline double xmul(Point a, Point b, Point c) {
  return (a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x);
}
inline bool across(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(a, b, c), p2 = xmul(a, b, d);
  if (fabs(p1) < 1e-10 || fabs(p2) < 1e-10) return 1;
  if (p1 * p2 < 1e-10) return 1;
  return 0;
}
inline bool oneline(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(a, b, c), p2 = xmul(a, b, d);
  if (fabs(p1) < 1e-10 && fabs(p2) < 1e-10) return 1;
  return 0;
}
inline Point intersection(Line &u, Line &v) {
  Point ret = u.a;
  double t =
      ((u.a.x - v.a.x) * (v.a.y - v.b.y) - (u.a.y - v.a.y) * (v.a.x - v.b.x)) /
      ((u.a.x - u.b.x) * (v.a.y - v.b.y) - (u.a.y - u.b.y) * (v.a.x - v.b.x));
  ret.x += (u.b.x - u.a.x) * t;
  ret.y += (u.b.y - u.a.y) * t;
  return ret;
}
inline double dis(Point &a, Point &b) {
  return sqrt(((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y)));
}
inline bool onseg(Point &a, Point &b, Point &c) {
  if (fabs(xmul(a, b, c)) < 1e-10) {
    return 1;
  }
  return 0;
}
inline bool seginter(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(c, d, a), p2 = xmul(c, d, b), p3 = xmul(a, b, c),
         p4 = xmul(a, b, d);
  if (p1 * p2 < 0 && p3 * p4 < 0) return 1;
  if (fabs(p1) < 1e-10 && onseg(c, d, a)) return 1;
  if (fabs(p2) < 1e-10 && onseg(c, d, b)) return 1;
  if (fabs(p3) < 1e-10 && onseg(a, b, c)) return 1;
  if (fabs(p4) < 1e-10 && onseg(a, b, d)) return 1;
  return 0;
}
inline double getans() {
  Point a, b;
  int tot = 0, cnt = 0, top = 0;
  double ans = 0;
  for (int i = 1; i <= n; i++) {
    if (oneline(L.a, L.b, po[i], po[nxt[i]])) {
      cnt++;
      pp[cnt].a = po[i];
      pp[cnt].type = 1;
      cnt++;
      pp[cnt].a = po[nxt[i]];
      pp[cnt].type = -1;
      if (pp[cnt].a < pp[cnt - 1].a) swap(pp[cnt].a, pp[cnt - 1].a);
      continue;
    }
    int k1 = onseg(L.a, L.b, po[i]) || xmul(L.a, L.b, po[i]) < 0;
    int k2 = onseg(L.a, L.b, po[nxt[i]]) || xmul(L.a, L.b, po[nxt[i]]) < 0;
    if (k1 + k2 != 1) continue;
    if (onseg(L.a, L.b, po[i])) {
      rec[++tot] = po[i];
      continue;
    }
    if (onseg(L.a, L.b, po[nxt[i]])) {
      rec[++tot] = po[nxt[i]];
      continue;
    }
    if (across(L.a, L.b, po[i], po[i + 1])) {
      Line tmp;
      tmp.a = po[i], tmp.b = po[nxt[i]];
      rec[++tot] = intersection(L, tmp);
    }
  }
  sort(rec + 1, rec + tot + 1);
  for (int i = 1; i < tot; i += 2) {
    cnt++;
    pp[cnt].a = rec[i];
    pp[cnt].type = 1;
    cnt++;
    pp[cnt].a = rec[i + 1];
    pp[cnt].type = -1;
  }
  sort(pp + 1, pp + cnt + 1);
  for (int i = 1; i <= cnt; i++) {
    if (pp[i].type == 1)
      stk[++top] = pp[i].a;
    else {
      if (top == 1) ans += dis(stk[top], pp[i].a);
      top--;
    }
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) nxt[i] = i + 1;
  nxt[n] = 1;
  for (int i = 1; i <= n; i++) lst[i] = i - 1;
  lst[1] = n;
  for (int i = 1; i <= n; i++) scanf("%lf%lf", &po[i].x, &po[i].y);
  po[n + 1] = po[1];
  for (int i = 1; i <= m; i++) {
    scanf("%lf%lf%lf%lf", &L.a.x, &L.a.y, &L.b.x, &L.b.y);
    printf("%.8lf\n", getans());
  }
  return 0;
}

### Prompt

Generate 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;
const int N = 1010;
struct Point {
  double x, y;
  Point(double _ = 0, double __ = 0) { x = _, y = __; }
  bool operator<(const Point &_) const {
    if (fabs(x - _.x) > 1e-10) return x < _.x;
    return y < _.y;
  }
} po[N], rec[N * 2], stk[N * 2];
struct Line {
  Point a, b;
} L;
struct Mem {
  Point a;
  int type;
  bool operator<(const Mem &_) const {
    if (fabs(a.x - _.a.x) > 1e-10) return a.x < _.a.x;
    if (fabs(a.y - _.a.y) > 1e-10) return a.y < _.a.y;
    return type < _.type;
  }
} pp[N * 2];
int n, m, nxt[N], lst[N];
inline double xmul(Point a, Point b, Point c) {
  return (a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x);
}
inline bool across(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(a, b, c), p2 = xmul(a, b, d);
  if (fabs(p1) < 1e-10 || fabs(p2) < 1e-10) return 1;
  if (p1 * p2 < 1e-10) return 1;
  return 0;
}
inline bool oneline(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(a, b, c), p2 = xmul(a, b, d);
  if (fabs(p1) < 1e-10 && fabs(p2) < 1e-10) return 1;
  return 0;
}
inline Point intersection(Line &u, Line &v) {
  Point ret = u.a;
  double t =
      ((u.a.x - v.a.x) * (v.a.y - v.b.y) - (u.a.y - v.a.y) * (v.a.x - v.b.x)) /
      ((u.a.x - u.b.x) * (v.a.y - v.b.y) - (u.a.y - u.b.y) * (v.a.x - v.b.x));
  ret.x += (u.b.x - u.a.x) * t;
  ret.y += (u.b.y - u.a.y) * t;
  return ret;
}
inline double dis(Point &a, Point &b) {
  return sqrt(((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y)));
}
inline bool onseg(Point &a, Point &b, Point &c) {
  if (fabs(xmul(a, b, c)) < 1e-10) {
    return 1;
  }
  return 0;
}
inline bool seginter(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(c, d, a), p2 = xmul(c, d, b), p3 = xmul(a, b, c),
         p4 = xmul(a, b, d);
  if (p1 * p2 < 0 && p3 * p4 < 0) return 1;
  if (fabs(p1) < 1e-10 && onseg(c, d, a)) return 1;
  if (fabs(p2) < 1e-10 && onseg(c, d, b)) return 1;
  if (fabs(p3) < 1e-10 && onseg(a, b, c)) return 1;
  if (fabs(p4) < 1e-10 && onseg(a, b, d)) return 1;
  return 0;
}
inline double getans() {
  Point a, b;
  int tot = 0, cnt = 0, top = 0;
  double ans = 0;
  for (int i = 1; i <= n; i++) {
    if (oneline(L.a, L.b, po[i], po[nxt[i]])) {
      cnt++;
      pp[cnt].a = po[i];
      pp[cnt].type = 1;
      cnt++;
      pp[cnt].a = po[nxt[i]];
      pp[cnt].type = -1;
      if (pp[cnt].a < pp[cnt - 1].a) swap(pp[cnt].a, pp[cnt - 1].a);
      continue;
    }
    int k1 = onseg(L.a, L.b, po[i]) || xmul(L.a, L.b, po[i]) < 0;
    int k2 = onseg(L.a, L.b, po[nxt[i]]) || xmul(L.a, L.b, po[nxt[i]]) < 0;
    if (k1 + k2 != 1) continue;
    if (onseg(L.a, L.b, po[i])) {
      rec[++tot] = po[i];
      continue;
    }
    if (onseg(L.a, L.b, po[nxt[i]])) {
      rec[++tot] = po[nxt[i]];
      continue;
    }
    if (across(L.a, L.b, po[i], po[i + 1])) {
      Line tmp;
      tmp.a = po[i], tmp.b = po[nxt[i]];
      rec[++tot] = intersection(L, tmp);
    }
  }
  sort(rec + 1, rec + tot + 1);
  for (int i = 1; i < tot; i += 2) {
    cnt++;
    pp[cnt].a = rec[i];
    pp[cnt].type = 1;
    cnt++;
    pp[cnt].a = rec[i + 1];
    pp[cnt].type = -1;
  }
  sort(pp + 1, pp + cnt + 1);
  for (int i = 1; i <= cnt; i++) {
    if (pp[i].type == 1)
      stk[++top] = pp[i].a;
    else {
      if (top == 1) ans += dis(stk[top], pp[i].a);
      top--;
    }
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) nxt[i] = i + 1;
  nxt[n] = 1;
  for (int i = 1; i <= n; i++) lst[i] = i - 1;
  lst[1] = n;
  for (int i = 1; i <= n; i++) scanf("%lf%lf", &po[i].x, &po[i].y);
  po[n + 1] = po[1];
  for (int i = 1; i <= m; i++) {
    scanf("%lf%lf%lf%lf", &L.a.x, &L.a.y, &L.b.x, &L.b.y);
    printf("%.8lf\n", getans());
  }
  return 0;
}

```