#include <bits/stdc++.h>
using namespace std;
template <class T>
struct Point {
  T x, y;
  explicit Point(T x = 0, T y = 0) : x(x), y(y) {}
  bool operator<(Point p) const { return tie(x, y) < tie(p.x, p.y); }
  bool operator==(Point p) const { return tie(x, y) == tie(p.x, p.y); }
  Point operator+(Point p) const { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) const { return Point(x - p.x, y - p.y); }
  Point operator*(T d) const { return Point(x * d, y * d); }
  Point operator/(T d) const { return Point(x / d, y / d); }
  T dot(Point p) const { return x * p.x + y * p.y; }
  T cross(Point p) const { return x * p.y - y * p.x; }
  T cross(Point a, Point b) const { return (a - *this).cross(b - *this); }
  T dist2() const { return x * x + y * y; }
  double dist() const { return sqrt((double)dist2()); }
  double angle() const { return atan2(y, x); }
  Point unit() const { return *this / dist(); }
  Point perp() const { return Point(-y, x); }
  Point normal() const { return perp().unit(); }
  Point rotate(double a) const {
    return Point(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));
  }
};
int n, m;
const int MAXN = 1001;
Point<long double> p[MAXN];
const long double eps = 1e-9;
template <class Point>
long double coeff(const Point& s1, const Point& e1, const Point& s2,
                  const Point& e2) {
  return -(((e2 - s1).cross(e2 - s2)) / ((e2 - s2).cross(e1 - s1)));
}
int signum(long double x) {
  if (x > eps) return 1;
  if (x < -eps) return -1;
  return 0;
}
long double solve(Point<long double> s, Point<long double> t) {
  vector<pair<long double, int>> coeffs;
  for (int i = 0; i < n; i++) {
    int lef = signum((p[i] - t).cross(t - s));
    int rig = signum((p[(i + 1) % n] - t).cross(t - s));
    if (lef == rig) continue;
    long double coef = coeff(s, t, p[i], p[(i + 1) % n]);
    coeffs.push_back({coef, rig - lef});
  }
  sort(coeffs.begin(), coeffs.end());
  int cnt = 0;
  long double sum = 0, pre = 0;
  for (int i = 0; i < coeffs.size(); i++) {
    if (cnt > 0) sum += coeffs[i].first - pre;
    pre = coeffs[i].first;
    cnt += coeffs[i].second;
  }
  return sum * (t - s).dist();
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }
  long double area_sign = 0;
  for (int i = 0; i < n; i++) {
    area_sign += p[i].cross(p[(i + 1) % n]);
  }
  if (area_sign < 0) reverse(p, p + n);
  for (int i = 0; i < m; i++) {
    Point<long double> s, t;
    cin >> s.x >> s.y >> t.x >> t.y;
    cout << fixed << setprecision(12) << solve(s, t) << endl;
    ;
  }
  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;
template <class T>
struct Point {
  T x, y;
  explicit Point(T x = 0, T y = 0) : x(x), y(y) {}
  bool operator<(Point p) const { return tie(x, y) < tie(p.x, p.y); }
  bool operator==(Point p) const { return tie(x, y) == tie(p.x, p.y); }
  Point operator+(Point p) const { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) const { return Point(x - p.x, y - p.y); }
  Point operator*(T d) const { return Point(x * d, y * d); }
  Point operator/(T d) const { return Point(x / d, y / d); }
  T dot(Point p) const { return x * p.x + y * p.y; }
  T cross(Point p) const { return x * p.y - y * p.x; }
  T cross(Point a, Point b) const { return (a - *this).cross(b - *this); }
  T dist2() const { return x * x + y * y; }
  double dist() const { return sqrt((double)dist2()); }
  double angle() const { return atan2(y, x); }
  Point unit() const { return *this / dist(); }
  Point perp() const { return Point(-y, x); }
  Point normal() const { return perp().unit(); }
  Point rotate(double a) const {
    return Point(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));
  }
};
int n, m;
const int MAXN = 1001;
Point<long double> p[MAXN];
const long double eps = 1e-9;
template <class Point>
long double coeff(const Point& s1, const Point& e1, const Point& s2,
                  const Point& e2) {
  return -(((e2 - s1).cross(e2 - s2)) / ((e2 - s2).cross(e1 - s1)));
}
int signum(long double x) {
  if (x > eps) return 1;
  if (x < -eps) return -1;
  return 0;
}
long double solve(Point<long double> s, Point<long double> t) {
  vector<pair<long double, int>> coeffs;
  for (int i = 0; i < n; i++) {
    int lef = signum((p[i] - t).cross(t - s));
    int rig = signum((p[(i + 1) % n] - t).cross(t - s));
    if (lef == rig) continue;
    long double coef = coeff(s, t, p[i], p[(i + 1) % n]);
    coeffs.push_back({coef, rig - lef});
  }
  sort(coeffs.begin(), coeffs.end());
  int cnt = 0;
  long double sum = 0, pre = 0;
  for (int i = 0; i < coeffs.size(); i++) {
    if (cnt > 0) sum += coeffs[i].first - pre;
    pre = coeffs[i].first;
    cnt += coeffs[i].second;
  }
  return sum * (t - s).dist();
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }
  long double area_sign = 0;
  for (int i = 0; i < n; i++) {
    area_sign += p[i].cross(p[(i + 1) % n]);
  }
  if (area_sign < 0) reverse(p, p + n);
  for (int i = 0; i < m; i++) {
    Point<long double> s, t;
    cin >> s.x >> s.y >> t.x >> t.y;
    cout << fixed << setprecision(12) << solve(s, t) << endl;
    ;
  }
  return 0;
}

```