#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
class Point {
 public:
  long double x, y;
  Point() {}
  Point(double x_, double y_) : x(x_), y(y_) {}
  Point operator-(const Point &oth) const {
    return Point(x - oth.x, y - oth.y);
  }
  long double operator^(const Point &oth) const {
    return x * oth.y - y * oth.x;
  }
};
Point O;
Point A[maxn];
inline long double abs(Point P) { return sqrt(P.x * P.x + P.y * P.y); }
inline long double dis(Point P, Point Q) { return abs(P - Q); }
inline bool same(Point X1, Point X2, Point Y1, Point Y2) {
  long double a1 = X1.y - X2.y, b1 = X2.x - X1.x, c1 = (X2 ^ X1);
  long double a2 = Y1.y - Y2.y, b2 = Y2.x - Y1.x, c2 = (Y2 ^ Y1);
  if (a1 * b2 == a2 * b1 && c1 * a2 == c2 * a1 && c1 * b2 == c2 * b1) {
    return true;
  }
  return false;
}
inline Point conj(Point X1, Point X2, Point Y1, Point Y2, bool &flag) {
  long double a1 = X1.y - X2.y, b1 = X2.x - X1.x, c1 = (X2 ^ X1);
  long double a2 = Y1.y - Y2.y, b2 = Y2.x - Y1.x, c2 = (Y2 ^ Y1);
  if (a1 * b2 == a2 * b1) {
    flag = 0;
    return Point();
  }
  return Point((c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1),
               (c1 * a2 - c2 * a1) / (b1 * a2 - b2 * a1));
}
inline bool in(Point P, Point X, Point Y) {
  return (P.x >= min(X.x, Y.x) && P.x <= max(X.x, Y.x)) &&
         (P.y >= min(X.y, Y.y) && P.y <= max(X.y, Y.y));
}
inline long double query(Point P, Point Q, Point X, Point Y) {
  assert(!same(O, X, O, Y));
  int f = ((X - O) ^ (Y - O)) <= 0 ? 1 : -1;
  if (same(P, Q, O, X)) {
    return dis(O, X) * f;
  } else if (same(P, Q, O, Y)) {
    return 0;
  } else if (same(P, Q, X, Y)) {
    return (f == -1 ? dis(X, Y) * f : 0);
  }
  bool r = true, s = true, t = true;
  Point R = conj(P, Q, O, X, r), S = conj(P, Q, O, Y, s),
        T = conj(P, Q, X, Y, t);
  if (r) r &= in(R, O, X);
  if (s) s &= in(S, O, Y);
  if (t) t &= in(T, X, Y);
  if (s && t && (S.x != T.x || S.y != T.y))
    return dis(S, T) * f;
  else if (r && t && (R.x != T.x || R.y != T.y))
    return dis(R, T) * f;
  else if (r && s && (R.x != S.x || R.y != S.y))
    return dis(R, S) * f;
  return 0;
}
int n, m;
inline bool cmp(const Point &P, const Point &Q) {
  return ((P - O) ^ (Q - O)) > 0;
}
inline void doSort() {
  long double area = 0;
  for (int i = (0); i < (n); ++i) area += ((A[i] - O) ^ (A[(i + 1) % n] - O));
  if (area < 0) reverse(A, A + n);
  return;
}
int main() {
  cin >> n >> m;
  srand(unsigned(time(0)));
  O.x = 19224.000000, O.y = 25672.000000;
  for (int i = (0); i < (n); ++i) {
    long double x, y;
    cin >> x >> y;
    x *= 100;
    y *= 100;
    A[i].x = x, A[i].y = y;
  }
  doSort();
  for (int i = (0); i < (m); ++i) {
    Point P, Q;
    long double px, py, qx, qy;
    cin >> px >> py >> qx >> qy;
    px *= 100;
    py *= 100;
    qx *= 100;
    qy *= 100;
    P.x = px, P.y = py, Q.x = qx, Q.y = qy;
    long double ans = 0;
    for (int j = (0); j < (n); ++j) {
      long double res = query(P, Q, A[j], A[(j + 1) % n]);
      ans += res;
    }
    cout << fixed << setprecision(10) << abs(ans) / 100.0 << 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;
const int maxn = 1005;
class Point {
 public:
  long double x, y;
  Point() {}
  Point(double x_, double y_) : x(x_), y(y_) {}
  Point operator-(const Point &oth) const {
    return Point(x - oth.x, y - oth.y);
  }
  long double operator^(const Point &oth) const {
    return x * oth.y - y * oth.x;
  }
};
Point O;
Point A[maxn];
inline long double abs(Point P) { return sqrt(P.x * P.x + P.y * P.y); }
inline long double dis(Point P, Point Q) { return abs(P - Q); }
inline bool same(Point X1, Point X2, Point Y1, Point Y2) {
  long double a1 = X1.y - X2.y, b1 = X2.x - X1.x, c1 = (X2 ^ X1);
  long double a2 = Y1.y - Y2.y, b2 = Y2.x - Y1.x, c2 = (Y2 ^ Y1);
  if (a1 * b2 == a2 * b1 && c1 * a2 == c2 * a1 && c1 * b2 == c2 * b1) {
    return true;
  }
  return false;
}
inline Point conj(Point X1, Point X2, Point Y1, Point Y2, bool &flag) {
  long double a1 = X1.y - X2.y, b1 = X2.x - X1.x, c1 = (X2 ^ X1);
  long double a2 = Y1.y - Y2.y, b2 = Y2.x - Y1.x, c2 = (Y2 ^ Y1);
  if (a1 * b2 == a2 * b1) {
    flag = 0;
    return Point();
  }
  return Point((c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1),
               (c1 * a2 - c2 * a1) / (b1 * a2 - b2 * a1));
}
inline bool in(Point P, Point X, Point Y) {
  return (P.x >= min(X.x, Y.x) && P.x <= max(X.x, Y.x)) &&
         (P.y >= min(X.y, Y.y) && P.y <= max(X.y, Y.y));
}
inline long double query(Point P, Point Q, Point X, Point Y) {
  assert(!same(O, X, O, Y));
  int f = ((X - O) ^ (Y - O)) <= 0 ? 1 : -1;
  if (same(P, Q, O, X)) {
    return dis(O, X) * f;
  } else if (same(P, Q, O, Y)) {
    return 0;
  } else if (same(P, Q, X, Y)) {
    return (f == -1 ? dis(X, Y) * f : 0);
  }
  bool r = true, s = true, t = true;
  Point R = conj(P, Q, O, X, r), S = conj(P, Q, O, Y, s),
        T = conj(P, Q, X, Y, t);
  if (r) r &= in(R, O, X);
  if (s) s &= in(S, O, Y);
  if (t) t &= in(T, X, Y);
  if (s && t && (S.x != T.x || S.y != T.y))
    return dis(S, T) * f;
  else if (r && t && (R.x != T.x || R.y != T.y))
    return dis(R, T) * f;
  else if (r && s && (R.x != S.x || R.y != S.y))
    return dis(R, S) * f;
  return 0;
}
int n, m;
inline bool cmp(const Point &P, const Point &Q) {
  return ((P - O) ^ (Q - O)) > 0;
}
inline void doSort() {
  long double area = 0;
  for (int i = (0); i < (n); ++i) area += ((A[i] - O) ^ (A[(i + 1) % n] - O));
  if (area < 0) reverse(A, A + n);
  return;
}
int main() {
  cin >> n >> m;
  srand(unsigned(time(0)));
  O.x = 19224.000000, O.y = 25672.000000;
  for (int i = (0); i < (n); ++i) {
    long double x, y;
    cin >> x >> y;
    x *= 100;
    y *= 100;
    A[i].x = x, A[i].y = y;
  }
  doSort();
  for (int i = (0); i < (m); ++i) {
    Point P, Q;
    long double px, py, qx, qy;
    cin >> px >> py >> qx >> qy;
    px *= 100;
    py *= 100;
    qx *= 100;
    qy *= 100;
    P.x = px, P.y = py, Q.x = qx, Q.y = qy;
    long double ans = 0;
    for (int j = (0); j < (n); ++j) {
      long double res = query(P, Q, A[j], A[(j + 1) % n]);
      ans += res;
    }
    cout << fixed << setprecision(10) << abs(ans) / 100.0 << endl;
  }
  return 0;
}

```