#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
bool eq(long double a, long double b) { return abs(a - b) <= eps; }
struct pt {
  long double x, y;
  pt() {}
  pt(long double x_, long double y_) : x(x_), y(y_) {}
  bool operator<(const pt p) const {
    if (!eq(x, p.x)) return x < p.x;
    if (!eq(y, p.y)) return y < p.y;
    return false;
  }
  pt operator-(const pt p) const { return pt(x - p.x, y - p.y); }
  long double operator^(const pt p) const { return x * p.y - y * p.x; }
};
struct line {
  pt p, q;
  line(pt p_, pt q_) : p(p_), q(q_) {}
};
long double sarea(pt p, pt q, pt r) { return ((q - p) ^ (r - q)) / 2.0; }
int side(pt p, pt q, pt r) {
  long double area = sarea(p, q, r);
  if (area + eps < 0.0) return -1;
  if (area - eps > 0.0) return 1;
  return 0;
}
long double inter(line r, line s) {
  pt dirs = s.q - s.p, dirr = r.q - r.p;
  assert(!eq((dirr ^ dirs), 0));
  return (((r.p - s.p) ^ (dirs)) * sqrt(dirr.x * dirr.x + dirr.y * dirr.y)) /
         ((dirr) ^ (dirs));
}
pt pol[1010];
int n;
void solve(vector<pair<long double, int>>& sline, line r) {
  for (int i = 0; i < n; i++) {
    line s(pol[i], pol[(i + 1) % n]);
    int sidep = side(r.p, r.q, s.p);
    int sideq = side(r.p, r.q, s.q);
    if (sidep == sideq) continue;
    long double p_inter = inter(r, s);
    int val = 2 * (sidep < sideq ? 1 : -1);
    if (sidep == 0 or sideq == 0) val /= 2;
    sline.push_back({p_inter, val});
  }
}
int main() {
  int m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> pol[i].x >> pol[i].y;
  for (int j = 0; j < m; j++) {
    pt p, q;
    cin >> p.x >> p.y;
    cin >> q.x >> q.y;
    line r(p, q);
    vector<pair<long double, int>> sline;
    solve(sline, r);
    sort(sline.begin(), sline.end());
    long double ans = 0;
    int cur = 0;
    for (int i = 0; i < int(sline.size()) - 1; i++) {
      cur += sline[i].second;
      if (cur != 0) ans += sline[i + 1].first - sline[i].first;
    }
    printf("%.12Lf\n", ans);
  }
  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;
const long double eps = 1e-9;
bool eq(long double a, long double b) { return abs(a - b) <= eps; }
struct pt {
  long double x, y;
  pt() {}
  pt(long double x_, long double y_) : x(x_), y(y_) {}
  bool operator<(const pt p) const {
    if (!eq(x, p.x)) return x < p.x;
    if (!eq(y, p.y)) return y < p.y;
    return false;
  }
  pt operator-(const pt p) const { return pt(x - p.x, y - p.y); }
  long double operator^(const pt p) const { return x * p.y - y * p.x; }
};
struct line {
  pt p, q;
  line(pt p_, pt q_) : p(p_), q(q_) {}
};
long double sarea(pt p, pt q, pt r) { return ((q - p) ^ (r - q)) / 2.0; }
int side(pt p, pt q, pt r) {
  long double area = sarea(p, q, r);
  if (area + eps < 0.0) return -1;
  if (area - eps > 0.0) return 1;
  return 0;
}
long double inter(line r, line s) {
  pt dirs = s.q - s.p, dirr = r.q - r.p;
  assert(!eq((dirr ^ dirs), 0));
  return (((r.p - s.p) ^ (dirs)) * sqrt(dirr.x * dirr.x + dirr.y * dirr.y)) /
         ((dirr) ^ (dirs));
}
pt pol[1010];
int n;
void solve(vector<pair<long double, int>>& sline, line r) {
  for (int i = 0; i < n; i++) {
    line s(pol[i], pol[(i + 1) % n]);
    int sidep = side(r.p, r.q, s.p);
    int sideq = side(r.p, r.q, s.q);
    if (sidep == sideq) continue;
    long double p_inter = inter(r, s);
    int val = 2 * (sidep < sideq ? 1 : -1);
    if (sidep == 0 or sideq == 0) val /= 2;
    sline.push_back({p_inter, val});
  }
}
int main() {
  int m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> pol[i].x >> pol[i].y;
  for (int j = 0; j < m; j++) {
    pt p, q;
    cin >> p.x >> p.y;
    cin >> q.x >> q.y;
    line r(p, q);
    vector<pair<long double, int>> sline;
    solve(sline, r);
    sort(sline.begin(), sline.end());
    long double ans = 0;
    int cur = 0;
    for (int i = 0; i < int(sline.size()) - 1; i++) {
      cur += sline[i].second;
      if (cur != 0) ans += sline[i + 1].first - sline[i].first;
    }
    printf("%.12Lf\n", ans);
  }
  return 0;
}

```