#include <bits/stdc++.h>
using namespace std;
constexpr long double eps = 1e-6;
struct Point;
bool Eq(long double x, long double y) { return abs(x - y) < eps; }
bool Less(long double x, long double y) { return x + eps < y; }
long double sqr(long double x) { return x * x; }
struct Point {
  long double x, y;
  Point() {}
  Point(long double x, long double y) : x(x), y(y) {}
  bool operator<(const Point &other) const {
    if (!Eq(x, other.x)) {
      return Less(x, other.x);
    }
    return Less(y, other.y);
  }
  Point operator+(const Point &other) const {
    return Point(x + other.x, y + other.y);
  }
  Point operator-(const Point &other) const {
    return Point(x - other.x, y - other.y);
  }
  friend Point operator*(long double k, const Point &p) {
    return Point(p.x * k, p.y * k);
  }
  long double operator^(const Point &other) const {
    return x * other.y - y * other.x;
  }
  long double operator*(const Point &other) const {
    return x * other.x + y * other.y;
  }
  long double Len(const Point &other) const {
    return sqrt(sqr(x - other.x) + sqr(y - other.y));
  }
  bool Line(const Point &first, const Point &second) const {
    return Eq(0, (*this - first) ^ (*this - second));
  }
  friend istream &operator>>(istream &is, Point &e) { return is >> e.x >> e.y; }
  friend ostream &operator<<(ostream &os, Point &e) {
    return os << e.x << ' ' << e.y;
  }
};
bool Eq(const Point &first, const Point &second) {
  return Eq(first.x, second.x) && Eq(first.y, second.y);
}
struct Line {
  Point s, v;
  Line() {}
  Line(Point &s, Point &v) : s(s), v(v) {}
  bool Parallel(const Line &other) const {
    return Eq((v - s) ^ (other.v - other.s), 0);
  }
  bool operator==(const Line &other) const {
    return this->Parallel(other) && this->Own(other.s);
  }
  bool Own(const Point &point) const { return point.Line(s, v); }
  Point Intersection(const Line &other) const {
    long double d = (v - s) ^ (other.v - other.s);
    long double t = (other.s - s) ^ (other.v - other.s);
    t /= d;
    return s + t * (v - s);
  }
  friend istream &operator>>(istream &is, Line &e) { return is >> e.s >> e.v; }
  friend ostream &operator<<(ostream &os, Line &e) {
    return os << e.s << ' ' << e.v;
  }
};
vector<Point> arr;
void Del180() {
  vector<Point> res;
  res.reserve(arr.size());
  res.push_back(arr.back());
  res.push_back(arr.front());
  size_t size = 2;
  for (size_t i = 1; i + 1 < arr.size(); ++i) {
    if (!arr[i].Line(res[size - 2], res[size - 1])) {
      res.push_back(arr[i]);
      ++size;
    } else {
      res.back() = arr[i];
    }
  }
  if (res.back().Line(res[0], res[1])) {
    res.erase(res.begin());
  }
  arr = res;
}
int sign(long double x) {
  if (Eq(x, 0)) {
    return 0;
  } else if (x > 0) {
    return 1;
  } else {
    return -1;
  }
}
vector<int> flag;
vector<Point> Intersection(const Line &l) {
  Line L(arr[0], arr[1]);
  if (L == l) {
    rotate(arr.begin(), arr.begin() + 1, arr.end());
  }
  Point p = arr.back();
  arr.push_back(arr[0]);
  arr.push_back(arr[1]);
  arr.insert(arr.begin(), p);
  vector<Point> res;
  for (size_t i = 1; i + 2 < arr.size(); ++i) {
    Line l2(arr[i], arr[i + 1]);
    if (l.Parallel(l2)) {
      if (l == l2) {
        flag.back() = 2;
        res.push_back(arr[i + 1]);
        if (sign((l.v - l.s) ^ (arr[i - 1] - l.v)) !=
            sign((l.v - l.s) ^ (arr[i + 2] - l.v))) {
          flag.push_back(1);
        } else {
          flag.push_back(0);
        }
        if (arr[i + 1] < arr[i]) {
          swap(flag.back(), flag[flag.size() - 2]);
        }
      }
    } else {
      Point p = l.Intersection(l2);
      if (Eq(p, arr[i])) {
        continue;
      }
      if (Eq(p, arr[i + 1])) {
        if (sign((l.v - l.s) ^ (arr[i] - l.v)) !=
            sign((l.v - l.s) ^ (arr[i + 2] - l.v))) {
          res.push_back(arr[i + 1]);
          flag.push_back(1);
        }
      } else {
        if (sign((p - l2.s) * (l2.v - p)) == 1) {
          res.push_back(p);
          flag.push_back(1);
        }
      }
    }
  }
  arr.pop_back();
  arr.pop_back();
  arr.erase(arr.begin());
  return res;
}
long double Solve(Line &l) {
  flag.clear();
  vector<Point> v = Intersection(l);
  vector<pair<Point, int>> pr(v.size());
  for (size_t i = 0; i < pr.size(); ++i) {
    pr[i] = {v[i], flag[i]};
  }
  sort(pr.begin(), pr.end());
  for (size_t i = 0; i < pr.size(); ++i) {
    v[i] = pr[i].first;
    flag[i] = pr[i].second;
  }
  long double ans = 0;
  bool fl = false;
  for (size_t i = 0; i + 1 < v.size(); i++) {
    if (flag[i] == 2) {
      ans += v[i].Len(v[i + 1]);
      continue;
    }
    fl ^= flag[i];
    if (!fl) {
      continue;
    }
    ans += v[i].Len(v[i + 1]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  size_t n, m;
  cin >> n >> m;
  arr.resize(n);
  for (auto &e : arr) {
    cin >> e;
  }
  Del180();
  for (size_t i = 0; i < m; ++i) {
    Line l;
    cin >> l;
    cout << fixed << setprecision(10) << Solve(l) << endl;
  }
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

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;
constexpr long double eps = 1e-6;
struct Point;
bool Eq(long double x, long double y) { return abs(x - y) < eps; }
bool Less(long double x, long double y) { return x + eps < y; }
long double sqr(long double x) { return x * x; }
struct Point {
  long double x, y;
  Point() {}
  Point(long double x, long double y) : x(x), y(y) {}
  bool operator<(const Point &other) const {
    if (!Eq(x, other.x)) {
      return Less(x, other.x);
    }
    return Less(y, other.y);
  }
  Point operator+(const Point &other) const {
    return Point(x + other.x, y + other.y);
  }
  Point operator-(const Point &other) const {
    return Point(x - other.x, y - other.y);
  }
  friend Point operator*(long double k, const Point &p) {
    return Point(p.x * k, p.y * k);
  }
  long double operator^(const Point &other) const {
    return x * other.y - y * other.x;
  }
  long double operator*(const Point &other) const {
    return x * other.x + y * other.y;
  }
  long double Len(const Point &other) const {
    return sqrt(sqr(x - other.x) + sqr(y - other.y));
  }
  bool Line(const Point &first, const Point &second) const {
    return Eq(0, (*this - first) ^ (*this - second));
  }
  friend istream &operator>>(istream &is, Point &e) { return is >> e.x >> e.y; }
  friend ostream &operator<<(ostream &os, Point &e) {
    return os << e.x << ' ' << e.y;
  }
};
bool Eq(const Point &first, const Point &second) {
  return Eq(first.x, second.x) && Eq(first.y, second.y);
}
struct Line {
  Point s, v;
  Line() {}
  Line(Point &s, Point &v) : s(s), v(v) {}
  bool Parallel(const Line &other) const {
    return Eq((v - s) ^ (other.v - other.s), 0);
  }
  bool operator==(const Line &other) const {
    return this->Parallel(other) && this->Own(other.s);
  }
  bool Own(const Point &point) const { return point.Line(s, v); }
  Point Intersection(const Line &other) const {
    long double d = (v - s) ^ (other.v - other.s);
    long double t = (other.s - s) ^ (other.v - other.s);
    t /= d;
    return s + t * (v - s);
  }
  friend istream &operator>>(istream &is, Line &e) { return is >> e.s >> e.v; }
  friend ostream &operator<<(ostream &os, Line &e) {
    return os << e.s << ' ' << e.v;
  }
};
vector<Point> arr;
void Del180() {
  vector<Point> res;
  res.reserve(arr.size());
  res.push_back(arr.back());
  res.push_back(arr.front());
  size_t size = 2;
  for (size_t i = 1; i + 1 < arr.size(); ++i) {
    if (!arr[i].Line(res[size - 2], res[size - 1])) {
      res.push_back(arr[i]);
      ++size;
    } else {
      res.back() = arr[i];
    }
  }
  if (res.back().Line(res[0], res[1])) {
    res.erase(res.begin());
  }
  arr = res;
}
int sign(long double x) {
  if (Eq(x, 0)) {
    return 0;
  } else if (x > 0) {
    return 1;
  } else {
    return -1;
  }
}
vector<int> flag;
vector<Point> Intersection(const Line &l) {
  Line L(arr[0], arr[1]);
  if (L == l) {
    rotate(arr.begin(), arr.begin() + 1, arr.end());
  }
  Point p = arr.back();
  arr.push_back(arr[0]);
  arr.push_back(arr[1]);
  arr.insert(arr.begin(), p);
  vector<Point> res;
  for (size_t i = 1; i + 2 < arr.size(); ++i) {
    Line l2(arr[i], arr[i + 1]);
    if (l.Parallel(l2)) {
      if (l == l2) {
        flag.back() = 2;
        res.push_back(arr[i + 1]);
        if (sign((l.v - l.s) ^ (arr[i - 1] - l.v)) !=
            sign((l.v - l.s) ^ (arr[i + 2] - l.v))) {
          flag.push_back(1);
        } else {
          flag.push_back(0);
        }
        if (arr[i + 1] < arr[i]) {
          swap(flag.back(), flag[flag.size() - 2]);
        }
      }
    } else {
      Point p = l.Intersection(l2);
      if (Eq(p, arr[i])) {
        continue;
      }
      if (Eq(p, arr[i + 1])) {
        if (sign((l.v - l.s) ^ (arr[i] - l.v)) !=
            sign((l.v - l.s) ^ (arr[i + 2] - l.v))) {
          res.push_back(arr[i + 1]);
          flag.push_back(1);
        }
      } else {
        if (sign((p - l2.s) * (l2.v - p)) == 1) {
          res.push_back(p);
          flag.push_back(1);
        }
      }
    }
  }
  arr.pop_back();
  arr.pop_back();
  arr.erase(arr.begin());
  return res;
}
long double Solve(Line &l) {
  flag.clear();
  vector<Point> v = Intersection(l);
  vector<pair<Point, int>> pr(v.size());
  for (size_t i = 0; i < pr.size(); ++i) {
    pr[i] = {v[i], flag[i]};
  }
  sort(pr.begin(), pr.end());
  for (size_t i = 0; i < pr.size(); ++i) {
    v[i] = pr[i].first;
    flag[i] = pr[i].second;
  }
  long double ans = 0;
  bool fl = false;
  for (size_t i = 0; i + 1 < v.size(); i++) {
    if (flag[i] == 2) {
      ans += v[i].Len(v[i + 1]);
      continue;
    }
    fl ^= flag[i];
    if (!fl) {
      continue;
    }
    ans += v[i].Len(v[i + 1]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  size_t n, m;
  cin >> n >> m;
  arr.resize(n);
  for (auto &e : arr) {
    cin >> e;
  }
  Del180();
  for (size_t i = 0; i < m; ++i) {
    Line l;
    cin >> l;
    cout << fixed << setprecision(10) << Solve(l) << endl;
  }
  return 0;
}

```