#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
struct Point {
  double x, y;
  Point(const double& _x = 0, const double& _y = 0) : x(_x), y(_y) {}
  void read() { cin >> x >> y; }
  Point operator-(const Point& other) const {
    return Point(x - other.x, y - other.y);
  }
};
int sign(const double& x) {
  if (x > eps) return 1;
  if (x < -eps) return -1;
  return 0;
}
double cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }
double cross(const Point& a, const Point& b, const Point& c) {
  return cross(b - a, c - a);
}
double dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }
int segcross(const Point& a, const Point& b, const Point& c, const Point& d,
             double& p) {
  double s1, s2, s3, s4;
  int d1, d2, d3, d4;
  d1 = sign(s1 = cross(a, b, c));
  d2 = sign(s2 = cross(a, b, d));
  d3 = sign(s3 = cross(c, d, a));
  d4 = sign(s4 = cross(c, d, b));
  if (d3 != d4) {
    p = cross(a, b, c) / cross(b - a, d - c);
    return d3 - d4;
  }
  return 0;
}
int tot = 1;
double solve(const Point& a, const Point& b, const vector<Point>& vs) {
  vector<pair<double, int> > ps;
  int n = (int)vs.size();
  double temp;
  for (int i = 0; i < n; ++i) {
    int d = segcross(vs[i], vs[(i + 1) % n], a, b, temp);
    if (d != 0) ps.emplace_back(temp, d);
  }
  sort(ps.begin(), ps.end());
  int sig = 0;
  double ans = 0;
  for (int i = 0; i < (int)ps.size(); ++i) {
    sig += ps[i].second;
    if (sig != 0) ans += ps[i + 1].first - ps[i].first;
  }
  return ans * sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<Point> vs;
  for (int i = 0; i < n; ++i) {
    Point a;
    a.read();
    vs.push_back(a);
  }
  while (m--) {
    Point a, b;
    a.read();
    b.read();
    cout << fixed << setprecision(20) << solve(a, b, vs) << '\n';
  }
  return 0;
}

### Prompt

Create a solution in CPP for 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 double eps = 1e-7;
struct Point {
  double x, y;
  Point(const double& _x = 0, const double& _y = 0) : x(_x), y(_y) {}
  void read() { cin >> x >> y; }
  Point operator-(const Point& other) const {
    return Point(x - other.x, y - other.y);
  }
};
int sign(const double& x) {
  if (x > eps) return 1;
  if (x < -eps) return -1;
  return 0;
}
double cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }
double cross(const Point& a, const Point& b, const Point& c) {
  return cross(b - a, c - a);
}
double dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; }
int segcross(const Point& a, const Point& b, const Point& c, const Point& d,
             double& p) {
  double s1, s2, s3, s4;
  int d1, d2, d3, d4;
  d1 = sign(s1 = cross(a, b, c));
  d2 = sign(s2 = cross(a, b, d));
  d3 = sign(s3 = cross(c, d, a));
  d4 = sign(s4 = cross(c, d, b));
  if (d3 != d4) {
    p = cross(a, b, c) / cross(b - a, d - c);
    return d3 - d4;
  }
  return 0;
}
int tot = 1;
double solve(const Point& a, const Point& b, const vector<Point>& vs) {
  vector<pair<double, int> > ps;
  int n = (int)vs.size();
  double temp;
  for (int i = 0; i < n; ++i) {
    int d = segcross(vs[i], vs[(i + 1) % n], a, b, temp);
    if (d != 0) ps.emplace_back(temp, d);
  }
  sort(ps.begin(), ps.end());
  int sig = 0;
  double ans = 0;
  for (int i = 0; i < (int)ps.size(); ++i) {
    sig += ps[i].second;
    if (sig != 0) ans += ps[i + 1].first - ps[i].first;
  }
  return ans * sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<Point> vs;
  for (int i = 0; i < n; ++i) {
    Point a;
    a.read();
    vs.push_back(a);
  }
  while (m--) {
    Point a, b;
    a.read();
    b.read();
    cout << fixed << setprecision(20) << solve(a, b, vs) << '\n';
  }
  return 0;
}

```