#include <bits/stdc++.h>
using namespace std;
struct Line {
  complex<double> a, b;
  Line(const complex<double> &a, const complex<double> &b) : a(a), b(b) {
    Rep();
  };
  void Rep() {
    if (a.real() == b.real()) {
      a = complex<double>(a.real(), 0);
      b = complex<double>(b.real(), 1);
      return;
    }
    complex<double> c, d;
    d = complex<double>(1, (b.imag() - a.imag()) / (b.real() - a.real()));
    c = complex<double>(0, b.imag() - b.real() * d.imag());
    d = complex<double>(1, d.imag() + c.imag());
    a = c;
    b = d;
  };
};
complex<double> CrossPoint(const Line &l, const Line &r) {
  complex<double> p, ld = l.b - l.a, rd = r.b - r.a, d3 = l.b - r.a;
  double aa = ld.real() * rd.imag() - ld.imag() * rd.real();
  double bb = ld.real() * d3.imag() - ld.imag() * d3.real();
  if (abs(aa) < 1e-9 && abs(bb) < 1e-9) return complex<double>(1e9, -1e9);
  if (abs(aa) < 1e-9) return complex<double>(1e9, 1e9);
  return r.a + bb / aa * (r.b - r.a);
};
int N, M;
int X[1010], Y[1010];
pair<double, double> cross(signed long long x1, signed long long y1,
                           signed long long x2, signed long long y2,
                           signed long long x3, signed long long y3,
                           signed long long x4, signed long long y4) {
  signed long long XX[3], YY[3];
  XX[0] = x2 - x1;
  YY[0] = y2 - y1;
  XX[1] = x3 - x1;
  YY[1] = y3 - y1;
  XX[2] = x4 - x1;
  YY[2] = y4 - y1;
  signed long long c1 = XX[0] * YY[1] - XX[1] * YY[0];
  signed long long c2 = XX[0] * YY[2] - XX[2] * YY[0];
  if ((c1 < 0 && c2 < 0) || (c1 > 0 && c2 > 0)) return {-1e9, -1e9};
  XX[0] = x4 - x3;
  YY[0] = y4 - y3;
  XX[1] = x1 - x3;
  YY[1] = y1 - y3;
  XX[2] = x2 - x3;
  YY[2] = y2 - y3;
  c1 = XX[0] * YY[1] - XX[1] * YY[0];
  c2 = XX[0] * YY[2] - XX[2] * YY[0];
  if ((c1 < 0 && c2 < 0) || (c1 > 0 && c2 > 0)) return {-1e9, -1e9};
  if ((x1 - x3) * (x2 - x3) + (y1 - y3) * (y2 - y3) == 0) return {-1e9, -1e9};
  if ((x1 - x4) * (x2 - x4) + (y1 - y4) * (y2 - y4) == 0) return {-1e9, -1e9};
  complex<double> p1(x1, y1);
  complex<double> p2(x2, y2);
  complex<double> p3(x3, y3);
  complex<double> p4(x4, y4);
  Line L1(p1, p2), L2(p3, p4);
  complex<double> c = CrossPoint(L1, L2);
  return {c.real(), c.imag()};
}
signed long long comb(double a) {
  if (a == 0) return 0;
  if (a > 0) return a * 100 + 0.1;
  if (a < 0) return -(-a * 100 + 0.1);
}
double hoge(signed long long sx, signed long long sy, signed long long dx,
            signed long long dy) {
  if (dx < 0 || (dx == 0 && dy < 0)) sx += dx, sy += dy, dx = -dx, dy = -dy;
  pair<double, double> st = {sx, sy};
  pair<double, double> ed = {sx + dx, sy + dy};
  signed long long L = max(abs(dx), abs(dy));
  signed long long mul = 100000000 / L + 2;
  pair<double, double> lst = {sx - dx * mul, sy - dy * mul};
  pair<double, double> rst = {sx + dx * mul, sy + dy * mul};
  vector<pair<pair<double, double>, int> > V;
  int i;
  for (i = 0; i < N; i++) {
    pair<double, double> a = {X[i], Y[i]}, b = {X[i + 1], Y[i + 1]};
    signed long long dx2 = X[i + 1] - X[i], dy2 = Y[i + 1] - Y[i];
    signed long long ad = (X[i] - lst.first) * (rst.second - lst.second) -
                          (Y[i] - lst.second) * (rst.first - lst.first);
    signed long long bd = (X[i + 1] - lst.first) * (rst.second - lst.second) -
                          (Y[i + 1] - lst.second) * (rst.first - lst.first);
    if (dy * dx2 == dx * dy2) {
      if (ad == 0 && bd == 0) {
        auto lef = max(st, min(a, b));
        auto ri = min(ed, max(a, b));
        V.push_back({lef, 0});
        V.push_back({ri, 0});
      }
    } else {
      if (ad > 0 && bd > 0)
        continue;
      else if (ad < 0 && bd < 0)
        continue;
      else if (ad == 0)
        V.push_back({{X[i], Y[i]}, (bd > 0) ? 1 : -1});
      else if (bd == 0)
        V.push_back({{X[i + 1], Y[i + 1]}, (ad > 0) ? -1 : 1});
      else {
        auto c = cross(lst.first, lst.second, rst.first, rst.second, X[i], Y[i],
                       X[i + 1], Y[i + 1]);
        V.push_back({c, (ad > 0) ? -2 : 2});
      }
      assert(ad || bd);
    }
  }
  sort((V.begin()), (V.end()));
  pair<double, double> pre = lst;
  int status = 0;
  double len = 0;
  for (auto &r : V) {
    if (status)
      len += hypot(pre.first - r.first.first, pre.second - r.first.second);
    status += r.second;
    pre = r.first;
  }
  return len;
}
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N >> M;
  for (i = 0; i < N; i++) {
    double dx, dy;
    cin >> dx >> dy;
    X[i] = comb(dx);
    Y[i] = comb(dy);
  }
  X[N] = X[0];
  Y[N] = Y[0];
  for (r = 0; r < M; r++) {
    double tx1, ty1, tx2, ty2;
    cin >> tx1 >> ty1 >> tx2 >> ty2;
    (void)printf("%.12lf\n", hoge(comb(tx1), comb(ty1), comb(tx2) - comb(tx1),
                                  comb(ty2) - comb(ty1)) /
                                 100.0);
  }
}
int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false);
  for (i = 0; i < argc - 1; i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin);
  solve();
  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;
struct Line {
  complex<double> a, b;
  Line(const complex<double> &a, const complex<double> &b) : a(a), b(b) {
    Rep();
  };
  void Rep() {
    if (a.real() == b.real()) {
      a = complex<double>(a.real(), 0);
      b = complex<double>(b.real(), 1);
      return;
    }
    complex<double> c, d;
    d = complex<double>(1, (b.imag() - a.imag()) / (b.real() - a.real()));
    c = complex<double>(0, b.imag() - b.real() * d.imag());
    d = complex<double>(1, d.imag() + c.imag());
    a = c;
    b = d;
  };
};
complex<double> CrossPoint(const Line &l, const Line &r) {
  complex<double> p, ld = l.b - l.a, rd = r.b - r.a, d3 = l.b - r.a;
  double aa = ld.real() * rd.imag() - ld.imag() * rd.real();
  double bb = ld.real() * d3.imag() - ld.imag() * d3.real();
  if (abs(aa) < 1e-9 && abs(bb) < 1e-9) return complex<double>(1e9, -1e9);
  if (abs(aa) < 1e-9) return complex<double>(1e9, 1e9);
  return r.a + bb / aa * (r.b - r.a);
};
int N, M;
int X[1010], Y[1010];
pair<double, double> cross(signed long long x1, signed long long y1,
                           signed long long x2, signed long long y2,
                           signed long long x3, signed long long y3,
                           signed long long x4, signed long long y4) {
  signed long long XX[3], YY[3];
  XX[0] = x2 - x1;
  YY[0] = y2 - y1;
  XX[1] = x3 - x1;
  YY[1] = y3 - y1;
  XX[2] = x4 - x1;
  YY[2] = y4 - y1;
  signed long long c1 = XX[0] * YY[1] - XX[1] * YY[0];
  signed long long c2 = XX[0] * YY[2] - XX[2] * YY[0];
  if ((c1 < 0 && c2 < 0) || (c1 > 0 && c2 > 0)) return {-1e9, -1e9};
  XX[0] = x4 - x3;
  YY[0] = y4 - y3;
  XX[1] = x1 - x3;
  YY[1] = y1 - y3;
  XX[2] = x2 - x3;
  YY[2] = y2 - y3;
  c1 = XX[0] * YY[1] - XX[1] * YY[0];
  c2 = XX[0] * YY[2] - XX[2] * YY[0];
  if ((c1 < 0 && c2 < 0) || (c1 > 0 && c2 > 0)) return {-1e9, -1e9};
  if ((x1 - x3) * (x2 - x3) + (y1 - y3) * (y2 - y3) == 0) return {-1e9, -1e9};
  if ((x1 - x4) * (x2 - x4) + (y1 - y4) * (y2 - y4) == 0) return {-1e9, -1e9};
  complex<double> p1(x1, y1);
  complex<double> p2(x2, y2);
  complex<double> p3(x3, y3);
  complex<double> p4(x4, y4);
  Line L1(p1, p2), L2(p3, p4);
  complex<double> c = CrossPoint(L1, L2);
  return {c.real(), c.imag()};
}
signed long long comb(double a) {
  if (a == 0) return 0;
  if (a > 0) return a * 100 + 0.1;
  if (a < 0) return -(-a * 100 + 0.1);
}
double hoge(signed long long sx, signed long long sy, signed long long dx,
            signed long long dy) {
  if (dx < 0 || (dx == 0 && dy < 0)) sx += dx, sy += dy, dx = -dx, dy = -dy;
  pair<double, double> st = {sx, sy};
  pair<double, double> ed = {sx + dx, sy + dy};
  signed long long L = max(abs(dx), abs(dy));
  signed long long mul = 100000000 / L + 2;
  pair<double, double> lst = {sx - dx * mul, sy - dy * mul};
  pair<double, double> rst = {sx + dx * mul, sy + dy * mul};
  vector<pair<pair<double, double>, int> > V;
  int i;
  for (i = 0; i < N; i++) {
    pair<double, double> a = {X[i], Y[i]}, b = {X[i + 1], Y[i + 1]};
    signed long long dx2 = X[i + 1] - X[i], dy2 = Y[i + 1] - Y[i];
    signed long long ad = (X[i] - lst.first) * (rst.second - lst.second) -
                          (Y[i] - lst.second) * (rst.first - lst.first);
    signed long long bd = (X[i + 1] - lst.first) * (rst.second - lst.second) -
                          (Y[i + 1] - lst.second) * (rst.first - lst.first);
    if (dy * dx2 == dx * dy2) {
      if (ad == 0 && bd == 0) {
        auto lef = max(st, min(a, b));
        auto ri = min(ed, max(a, b));
        V.push_back({lef, 0});
        V.push_back({ri, 0});
      }
    } else {
      if (ad > 0 && bd > 0)
        continue;
      else if (ad < 0 && bd < 0)
        continue;
      else if (ad == 0)
        V.push_back({{X[i], Y[i]}, (bd > 0) ? 1 : -1});
      else if (bd == 0)
        V.push_back({{X[i + 1], Y[i + 1]}, (ad > 0) ? -1 : 1});
      else {
        auto c = cross(lst.first, lst.second, rst.first, rst.second, X[i], Y[i],
                       X[i + 1], Y[i + 1]);
        V.push_back({c, (ad > 0) ? -2 : 2});
      }
      assert(ad || bd);
    }
  }
  sort((V.begin()), (V.end()));
  pair<double, double> pre = lst;
  int status = 0;
  double len = 0;
  for (auto &r : V) {
    if (status)
      len += hypot(pre.first - r.first.first, pre.second - r.first.second);
    status += r.second;
    pre = r.first;
  }
  return len;
}
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N >> M;
  for (i = 0; i < N; i++) {
    double dx, dy;
    cin >> dx >> dy;
    X[i] = comb(dx);
    Y[i] = comb(dy);
  }
  X[N] = X[0];
  Y[N] = Y[0];
  for (r = 0; r < M; r++) {
    double tx1, ty1, tx2, ty2;
    cin >> tx1 >> ty1 >> tx2 >> ty2;
    (void)printf("%.12lf\n", hoge(comb(tx1), comb(ty1), comb(tx2) - comb(tx1),
                                  comb(ty2) - comb(ty1)) /
                                 100.0);
  }
}
int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false);
  for (i = 0; i < argc - 1; i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin);
  solve();
  return 0;
}

```