#include <bits/stdc++.h>
using namespace std;
pair<long double, long double> operator+(
    const pair<long double, long double> &p1,
    const pair<long double, long double> &p2) {
  return {p1.first + p2.first, p1.second + p2.second};
}
pair<long double, long double> operator-(
    const pair<long double, long double> &p1,
    const pair<long double, long double> &p2) {
  return {p1.first - p2.first, p1.second - p2.second};
}
pair<long double, long double> operator*(
    const pair<long double, long double> &p1, long double c) {
  return {p1.first * c, p1.second * c};
}
long double operator*(const pair<long double, long double> &p1,
                      const pair<long double, long double> &p2) {
  return p1.first * p2.second - p1.second * p2.first;
}
long double dist(const pair<long double, long double> &p1,
                 const pair<long double, long double> &p2) {
  long double dx = p1.first - p2.first;
  long double dy = p1.second - p2.second;
  return sqrt(dx * dx + dy * dy);
}
pair<long double, long double> intersect(
    const pair<long double, long double> &p1,
    const pair<long double, long double> &p2,
    const pair<long double, long double> &p3,
    const pair<long double, long double> &p4) {
  if (fabs((p1 - p2) * (p3 - p4)) < 1e-9) return {123456, 0};
  long double ilo1 = (p2 - p1) * (p3 - p1);
  long double ilo2 = (p2 - p1) * (p4 - p1);
  if (ilo1 > 1e-9 && ilo2 < -1e-9)
    return p3 + (p4 - p3) * (ilo1 / (ilo1 - ilo2));
  if (ilo2 > 1e-9 && ilo1 < -1e-9)
    return p4 + (p3 - p4) * (ilo2 / (ilo2 - ilo1));
  return {123456, 0};
}
bool isValid(const pair<long double, long double> &p) {
  return fabs(p.first) < 100001 && fabs(p.second) < 100001;
}
int n, m;
pair<long double, long double> points[1234];
pair<long double, long double> lines[123][2];
void load() {
  cin >> n >> m;
  long double x, y;
  for (int i = 0; i < n; ++i) {
    cin >> x >> y;
    points[i] = {x, y};
  }
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < 2; ++j) {
      cin >> x >> y;
      lines[i][j] = {x, y};
    }
}
void makeClockwise() {
  long double field = 0;
  points[n] = points[0];
  for (int i = 0; i < n; ++i)
    field += (points[i + 1].second + points[i].second) *
             (points[i + 1].first - points[i].first);
  if (field < -1e-9) {
    int i = 0;
    while (i < n - 1 - i) {
      swap(points[i], points[n - 1 - i]);
      ++i;
    }
  }
}
void makeLong() {
  for (int i = 0; i < m; ++i) {
    pair<long double, long double> azym = lines[i][1] - lines[i][0];
    long double ratio = 200000 / max(fabs(azym.first), fabs(azym.second));
    lines[i][1] = lines[i][0] + azym * ratio;
    lines[i][0] = lines[i][0] - azym * ratio;
  }
}
int inAngle(pair<long double, long double> a1,
            pair<long double, long double> a2,
            pair<long double, long double> a3,
            pair<long double, long double> l) {
  complex<long double> A((a1 - a2).first, (a1 - a2).second);
  complex<long double> B((a3 - a2).first, (a3 - a2).second);
  B /= A;
  complex<long double> C((l - a2).first, (l - a2).second);
  C /= A;
  long double arg1 = arg(B);
  long double arg2 = arg(C);
  if (arg1 < -1e-9) arg1 += 2 * 3.14159265359;
  if (arg2 < -1e-9) arg2 += 2 * 3.14159265359;
  return (arg1 + 1e-9 > arg2) ? 1 : 0;
}
int main() {
  cout << setprecision(20) << fixed;
  load();
  makeClockwise();
  makeLong();
  vector<pair<long double, long double> > poi;
  points[n] = points[0];
  for (int iter = 0; iter < m; ++iter) {
    pair<long double, long double> l1 = lines[iter][0];
    pair<long double, long double> l2 = lines[iter][1];
    poi.clear();
    for (int i = 0; i < n; ++i) {
      pair<long double, long double> inter =
          intersect(l1, l2, points[i], points[i + 1]);
      if (isValid(inter)) poi.push_back(inter);
    }
    for (int i = 0; i < n; ++i)
      if (fabs((points[i] - l1) * (l2 - l1)) < 1e-9)
        for (int j = 0;
             j <
             inAngle(points[(i + n - 1) % n], points[i], points[i + 1], l1) +
                 inAngle(points[(i + n - 1) % n], points[i], points[i + 1], l2);
             ++j)
          poi.push_back(points[i]);
    sort(poi.begin(), poi.end());
    long double out = 0;
    for (int i = 0; i + 1 < poi.size();) {
      out += dist(poi[i], poi[i + 1]);
      if (dist(poi[i], poi[i + 1]) > 1e-9)
        i += 2;
      else
        i += 1;
    }
    cout << out << endl;
  }
}

### Prompt

Your challenge is to write 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;
pair<long double, long double> operator+(
    const pair<long double, long double> &p1,
    const pair<long double, long double> &p2) {
  return {p1.first + p2.first, p1.second + p2.second};
}
pair<long double, long double> operator-(
    const pair<long double, long double> &p1,
    const pair<long double, long double> &p2) {
  return {p1.first - p2.first, p1.second - p2.second};
}
pair<long double, long double> operator*(
    const pair<long double, long double> &p1, long double c) {
  return {p1.first * c, p1.second * c};
}
long double operator*(const pair<long double, long double> &p1,
                      const pair<long double, long double> &p2) {
  return p1.first * p2.second - p1.second * p2.first;
}
long double dist(const pair<long double, long double> &p1,
                 const pair<long double, long double> &p2) {
  long double dx = p1.first - p2.first;
  long double dy = p1.second - p2.second;
  return sqrt(dx * dx + dy * dy);
}
pair<long double, long double> intersect(
    const pair<long double, long double> &p1,
    const pair<long double, long double> &p2,
    const pair<long double, long double> &p3,
    const pair<long double, long double> &p4) {
  if (fabs((p1 - p2) * (p3 - p4)) < 1e-9) return {123456, 0};
  long double ilo1 = (p2 - p1) * (p3 - p1);
  long double ilo2 = (p2 - p1) * (p4 - p1);
  if (ilo1 > 1e-9 && ilo2 < -1e-9)
    return p3 + (p4 - p3) * (ilo1 / (ilo1 - ilo2));
  if (ilo2 > 1e-9 && ilo1 < -1e-9)
    return p4 + (p3 - p4) * (ilo2 / (ilo2 - ilo1));
  return {123456, 0};
}
bool isValid(const pair<long double, long double> &p) {
  return fabs(p.first) < 100001 && fabs(p.second) < 100001;
}
int n, m;
pair<long double, long double> points[1234];
pair<long double, long double> lines[123][2];
void load() {
  cin >> n >> m;
  long double x, y;
  for (int i = 0; i < n; ++i) {
    cin >> x >> y;
    points[i] = {x, y};
  }
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < 2; ++j) {
      cin >> x >> y;
      lines[i][j] = {x, y};
    }
}
void makeClockwise() {
  long double field = 0;
  points[n] = points[0];
  for (int i = 0; i < n; ++i)
    field += (points[i + 1].second + points[i].second) *
             (points[i + 1].first - points[i].first);
  if (field < -1e-9) {
    int i = 0;
    while (i < n - 1 - i) {
      swap(points[i], points[n - 1 - i]);
      ++i;
    }
  }
}
void makeLong() {
  for (int i = 0; i < m; ++i) {
    pair<long double, long double> azym = lines[i][1] - lines[i][0];
    long double ratio = 200000 / max(fabs(azym.first), fabs(azym.second));
    lines[i][1] = lines[i][0] + azym * ratio;
    lines[i][0] = lines[i][0] - azym * ratio;
  }
}
int inAngle(pair<long double, long double> a1,
            pair<long double, long double> a2,
            pair<long double, long double> a3,
            pair<long double, long double> l) {
  complex<long double> A((a1 - a2).first, (a1 - a2).second);
  complex<long double> B((a3 - a2).first, (a3 - a2).second);
  B /= A;
  complex<long double> C((l - a2).first, (l - a2).second);
  C /= A;
  long double arg1 = arg(B);
  long double arg2 = arg(C);
  if (arg1 < -1e-9) arg1 += 2 * 3.14159265359;
  if (arg2 < -1e-9) arg2 += 2 * 3.14159265359;
  return (arg1 + 1e-9 > arg2) ? 1 : 0;
}
int main() {
  cout << setprecision(20) << fixed;
  load();
  makeClockwise();
  makeLong();
  vector<pair<long double, long double> > poi;
  points[n] = points[0];
  for (int iter = 0; iter < m; ++iter) {
    pair<long double, long double> l1 = lines[iter][0];
    pair<long double, long double> l2 = lines[iter][1];
    poi.clear();
    for (int i = 0; i < n; ++i) {
      pair<long double, long double> inter =
          intersect(l1, l2, points[i], points[i + 1]);
      if (isValid(inter)) poi.push_back(inter);
    }
    for (int i = 0; i < n; ++i)
      if (fabs((points[i] - l1) * (l2 - l1)) < 1e-9)
        for (int j = 0;
             j <
             inAngle(points[(i + n - 1) % n], points[i], points[i + 1], l1) +
                 inAngle(points[(i + n - 1) % n], points[i], points[i + 1], l2);
             ++j)
          poi.push_back(points[i]);
    sort(poi.begin(), poi.end());
    long double out = 0;
    for (int i = 0; i + 1 < poi.size();) {
      out += dist(poi[i], poi[i + 1]);
      if (dist(poi[i], poi[i + 1]) > 1e-9)
        i += 2;
      else
        i += 1;
    }
    cout << out << endl;
  }
}

```