#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-12;
bool isZero(long double x) { return abs(x) <= EPS; }
struct P {
  long double x, y;
  P(long double x, long double y) : x(x), y(y) {}
  P() : x(0), y(0) {}
};
struct C {
  long double x, y;
  int ok;
  C(int ok, long double x = 0, long double y = 0) : x(x), y(y), ok(ok) {}
  bool operator<(const C p) const {
    if (x == p.x) return y < p.y;
    return x < p.x;
  }
};
struct Line {
  long double x1, y1, x2, y2;
};
const int N = 1e4 + 10;
P poly[N];
int n, m;
C cross(Line line, P p1, P p2) {
  long double dx = line.x2 - line.x1;
  long double dy = line.y2 - line.y1;
  long double dt = (dx * (p1.y - line.y1) - dy * (p1.x - line.x1)) *
                   (dx * (p2.y - line.y1) - dy * (p2.x - line.x1));
  if (dt > 0 || isZero(dt)) return C(false);
  if (isZero(dx) && isZero(p2.x - p1.x)) return C(false);
  if (isZero(dx)) {
    long double a = (p1.y - p2.y) / (p1.x - p2.x);
    long double b = p1.y - a * p1.x;
    return C(true, line.x1, a * line.x1 + b);
  }
  if (isZero(p2.x - p1.x)) {
    long double a = (line.y1 - line.y2) / (line.x1 - line.x2);
    long double b = line.y1 - a * line.x1;
    return C(true, p1.x, a * p1.x + b);
  }
  long double a1 = (p1.y - p2.y) / (p1.x - p2.x);
  long double b1 = p1.y - a1 * p1.x;
  long double a2 = (line.y1 - line.y2) / (line.x1 - line.x2);
  long double b2 = line.y1 - a2 * line.x1;
  long double x = (b2 - b1) / (a1 - a2);
  long double y = a1 * x + b1;
  return C(true, x, y);
}
long double len(C p1, C p2) {
  long double dx = p2.x - p1.x;
  long double dy = p2.y - p1.y;
  return sqrt(dx * dx + dy * dy);
}
bool onLine(Line line, P p) {
  if (isZero(line.x2 - line.x1)) return isZero(line.x1 - p.x);
  long double a = (line.y1 - line.y2) / (line.x1 - line.x2);
  long double b = line.y1 - a * line.x1;
  return isZero(a * p.x + b - p.y);
}
int diff(int p1, int p2, Line line) {
  P pa =
      P(poly[(p1 - 1 + n) % n].x - line.x1, poly[(p1 - 1 + n) % n].y - line.y1);
  P pb = P(poly[(p1 + 1) % n].x - line.x1, poly[(p1 + 1) % n].y - line.y1);
  long double dx = line.x2 - line.x1;
  long double dy = line.y2 - line.y1;
  if (p1 == p2) {
    long double dt = (dx * pa.y - dy * pa.x) * (dx * pb.y - dy * pb.x);
    if (dt < 0 && !isZero(dt)) return 1;
    return 0;
  } else {
    P pc = P(poly[(p2 - 1 + n) % n].x - line.x1,
             poly[(p2 - 1 + n) % n].y - line.y1);
    P pd = P(poly[(p2 + 1) % n].x - line.x1, poly[(p2 + 1) % n].y - line.y1);
    long double d1 = (dx * pa.y - dy * pa.x) * (dx * pc.y - dy * pc.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    if (d1 > 0 && !isZero(d1)) return 0;
    d1 = (dx * pa.y - dy * pa.x) * (dx * pd.y - dy * pd.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    if (d1 > 0 && !isZero(d1)) return 0;
    d1 = (dx * pb.y - dy * pb.x) * (dx * pd.y - dy * pd.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    if (d1 > 0 && !isZero(d1)) return 0;
    d1 = (dx * pb.y - dy * pb.x) * (dx * pc.y - dy * pc.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    return 0;
  }
}
long double calc(Line line) {
  vector<C> p;
  for (int i = 0; i < n; ++i) {
    C tp = cross(line, poly[i], poly[(i + 1) % n]);
    if (!tp.ok) continue;
    tp.ok = -1;
    p.push_back(tp);
  }
  for (int i = 0; i < n; ++i) {
    if (onLine(line, poly[i])) p.push_back(C(i, poly[i].x, poly[i].y));
  }
  sort(p.begin(), p.end());
  long double w = 0;
  int pts = 0;
  for (int i = 0; i < (int)(p.size()) - 1; ++i) {
    if (p[i].ok == -1) {
      ++pts;
      if (pts % 2 == 1) w += len(p[i], p[i + 1]);
    } else {
      int b = i, e = i;
      while (e + 1 < p.size() && p[e + 1].ok == (p[e].ok + 1) % n) ++e;
      if (b == e)
        while (e + 1 < p.size() && p[e + 1].ok == (p[e].ok - 1 + n) % n) ++e;
      w += len(p[b], p[e]);
      pts += diff(p[b].ok, p[e].ok, line);
      i = e;
      if (pts % 2 == 1 && i + 1 < p.size()) w += len(p[i], p[i + 1]);
    }
  }
  return w;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> poly[i].x >> poly[i].y;
  Line line;
  for (int i = 0; i < m; ++i) {
    cin >> line.x1 >> line.y1 >> line.x2 >> line.y2;
    cout << setprecision(10) << calc(line) << endl;
  }
  return 0;
}

### Prompt

Your task is to create 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;
const long double EPS = 1e-12;
bool isZero(long double x) { return abs(x) <= EPS; }
struct P {
  long double x, y;
  P(long double x, long double y) : x(x), y(y) {}
  P() : x(0), y(0) {}
};
struct C {
  long double x, y;
  int ok;
  C(int ok, long double x = 0, long double y = 0) : x(x), y(y), ok(ok) {}
  bool operator<(const C p) const {
    if (x == p.x) return y < p.y;
    return x < p.x;
  }
};
struct Line {
  long double x1, y1, x2, y2;
};
const int N = 1e4 + 10;
P poly[N];
int n, m;
C cross(Line line, P p1, P p2) {
  long double dx = line.x2 - line.x1;
  long double dy = line.y2 - line.y1;
  long double dt = (dx * (p1.y - line.y1) - dy * (p1.x - line.x1)) *
                   (dx * (p2.y - line.y1) - dy * (p2.x - line.x1));
  if (dt > 0 || isZero(dt)) return C(false);
  if (isZero(dx) && isZero(p2.x - p1.x)) return C(false);
  if (isZero(dx)) {
    long double a = (p1.y - p2.y) / (p1.x - p2.x);
    long double b = p1.y - a * p1.x;
    return C(true, line.x1, a * line.x1 + b);
  }
  if (isZero(p2.x - p1.x)) {
    long double a = (line.y1 - line.y2) / (line.x1 - line.x2);
    long double b = line.y1 - a * line.x1;
    return C(true, p1.x, a * p1.x + b);
  }
  long double a1 = (p1.y - p2.y) / (p1.x - p2.x);
  long double b1 = p1.y - a1 * p1.x;
  long double a2 = (line.y1 - line.y2) / (line.x1 - line.x2);
  long double b2 = line.y1 - a2 * line.x1;
  long double x = (b2 - b1) / (a1 - a2);
  long double y = a1 * x + b1;
  return C(true, x, y);
}
long double len(C p1, C p2) {
  long double dx = p2.x - p1.x;
  long double dy = p2.y - p1.y;
  return sqrt(dx * dx + dy * dy);
}
bool onLine(Line line, P p) {
  if (isZero(line.x2 - line.x1)) return isZero(line.x1 - p.x);
  long double a = (line.y1 - line.y2) / (line.x1 - line.x2);
  long double b = line.y1 - a * line.x1;
  return isZero(a * p.x + b - p.y);
}
int diff(int p1, int p2, Line line) {
  P pa =
      P(poly[(p1 - 1 + n) % n].x - line.x1, poly[(p1 - 1 + n) % n].y - line.y1);
  P pb = P(poly[(p1 + 1) % n].x - line.x1, poly[(p1 + 1) % n].y - line.y1);
  long double dx = line.x2 - line.x1;
  long double dy = line.y2 - line.y1;
  if (p1 == p2) {
    long double dt = (dx * pa.y - dy * pa.x) * (dx * pb.y - dy * pb.x);
    if (dt < 0 && !isZero(dt)) return 1;
    return 0;
  } else {
    P pc = P(poly[(p2 - 1 + n) % n].x - line.x1,
             poly[(p2 - 1 + n) % n].y - line.y1);
    P pd = P(poly[(p2 + 1) % n].x - line.x1, poly[(p2 + 1) % n].y - line.y1);
    long double d1 = (dx * pa.y - dy * pa.x) * (dx * pc.y - dy * pc.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    if (d1 > 0 && !isZero(d1)) return 0;
    d1 = (dx * pa.y - dy * pa.x) * (dx * pd.y - dy * pd.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    if (d1 > 0 && !isZero(d1)) return 0;
    d1 = (dx * pb.y - dy * pb.x) * (dx * pd.y - dy * pd.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    if (d1 > 0 && !isZero(d1)) return 0;
    d1 = (dx * pb.y - dy * pb.x) * (dx * pc.y - dy * pc.x);
    if (d1 < 0 && !isZero(d1)) return 1;
    return 0;
  }
}
long double calc(Line line) {
  vector<C> p;
  for (int i = 0; i < n; ++i) {
    C tp = cross(line, poly[i], poly[(i + 1) % n]);
    if (!tp.ok) continue;
    tp.ok = -1;
    p.push_back(tp);
  }
  for (int i = 0; i < n; ++i) {
    if (onLine(line, poly[i])) p.push_back(C(i, poly[i].x, poly[i].y));
  }
  sort(p.begin(), p.end());
  long double w = 0;
  int pts = 0;
  for (int i = 0; i < (int)(p.size()) - 1; ++i) {
    if (p[i].ok == -1) {
      ++pts;
      if (pts % 2 == 1) w += len(p[i], p[i + 1]);
    } else {
      int b = i, e = i;
      while (e + 1 < p.size() && p[e + 1].ok == (p[e].ok + 1) % n) ++e;
      if (b == e)
        while (e + 1 < p.size() && p[e + 1].ok == (p[e].ok - 1 + n) % n) ++e;
      w += len(p[b], p[e]);
      pts += diff(p[b].ok, p[e].ok, line);
      i = e;
      if (pts % 2 == 1 && i + 1 < p.size()) w += len(p[i], p[i + 1]);
    }
  }
  return w;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> poly[i].x >> poly[i].y;
  Line line;
  for (int i = 0; i < m; ++i) {
    cin >> line.x1 >> line.y1 >> line.x2 >> line.y2;
    cout << setprecision(10) << calc(line) << endl;
  }
  return 0;
}

```