#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 1000;
typedef struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
  P() {}
} P;
const P operator-(const P &a, const P &b) { return P(a.x - b.x, a.y - b.y); }
const long long operator^(const P &a, const P &b) {
  return (long long)a.x * b.y - (long long)a.y * b.x;
}
const long long operator*(const P &a, const P &b) {
  return (long long)a.x * b.x + (long long)a.y * b.y;
}
typedef struct R {
  long long num, den;
  int type;
} R;
bool operator<(const R &p, const R &q) {
  if ((p.num < 0) != (q.num < 0)) return p.num < 0;
  long long a = abs(p.num), b = p.den, c = abs(q.num), d = q.den;
  bool inv = p.num < 0;
  while (true) {
    long long aa = a / b, cc = c / d;
    if (aa != cc) return !inv ? aa < cc : cc < aa;
    a %= b, c %= d;
    if (a == 0 || c == 0) break;
    inv = !inv;
    swap(a, b);
    swap(c, d);
  }
  if (a != 0 || c != 0) return !inv ? a == 0 : c == 0;
  return false;
}
P p[MAXN + 1];
int n, nq;
R r[2 * MAXN];
int nr;
char buff[100];
int read() {
  scanf("%s", buff);
  int at = 0, sgn = +1, ret = 0, flen = 0;
  if (buff[at] == '-') sgn = -sgn, ++at;
  while (isdigit(buff[at])) ret = ret * 10 + buff[at++] - '0';
  if (buff[at] == '.') ++at;
  while (isdigit(buff[at])) ret = ret * 10 + buff[at++] - '0', ++flen;
  for (int i = (0); i < (2 - flen); ++i) ret = ret * 10;
  return sgn * ret;
}
void intersect(const P &a, const P &b, const P &c, const P &d) {
  long long den = (d - c) ^ (b - a);
  if (den == 0) {
    long long side = (a - c) ^ (d - c);
    bool hor = d.x == c.x;
    if (side != 0) return;
    r[nr].num = !hor ? a.x - c.x : a.y - c.y,
    r[nr].den = !hor ? d.x - c.x : d.y - c.y, r[nr].type = 2, ++nr;
    r[nr].num = !hor ? b.x - c.x : b.y - c.y,
    r[nr].den = !hor ? d.x - c.x : d.y - c.y, r[nr].type = 2, ++nr;
  } else {
    long long num1 = (a - c) ^ (d - c), num2 = (a - c) ^ (b - a);
    if (den < 0) num1 = -num1, num2 = -num2, den = -den;
    if (num1 < 0 || num1 > den) return;
    r[nr].num = num2, r[nr].den = den,
    r[nr].type = num1 == 0     ? (((b - c) ^ (d - c)) > 0 ? +1 : -1)
                 : num1 == den ? (((a - c) ^ (d - c)) > 0 ? +1 : -1)
                               : 0,
    ++nr;
  }
}
void run() {
  scanf("%d%d", &n, &nq);
  for (int i = (0); i < (n); ++i) p[i].x = read(), p[i].y = read();
  p[n] = p[0];
  for (int qi = (0); qi < (nq); ++qi) {
    P a, b;
    a.x = read(), a.y = read(), b.x = read(), b.y = read();
    nr = 0;
    for (int i = (0); i < (n); ++i) intersect(p[i], p[i + 1], a, b);
    for (int i = (0); i < (n); ++i)
      if (r[i].den < 0) r[i].den = -r[i].den, r[i].num = -r[i].num;
    sort(r, r + nr);
    double ret = 0;
    int ctype = 0;
    for (int at = 0, to = at; at < nr; at = to++) {
      if (ctype != 0)
        ret +=
            1.0 * r[at].num / r[at].den - 1.0 * r[at - 1].num / r[at - 1].den;
      while (to < nr && !(r[at] < r[to])) ++to;
      if (to - at == 1) {
        assert(ctype == 0 || ctype == 2);
        assert(r[at].type == 0);
        ctype = 2 - ctype;
      } else if (to - at == 2) {
        assert(r[at].type != 0 && r[at + 1].type != 0);
        if (ctype == 0 || ctype == 2) {
          assert(r[at].type != 2 || r[at + 1].type != 2);
          if (r[at].type == r[at + 1].type)
            ctype = ctype;
          else if (r[at].type == -r[at + 1].type)
            ctype = 2 - ctype;
          else
            ctype = ctype == 0 ? r[at].type + r[at + 1].type - 2
                               : 2 - r[at].type - r[at + 1].type;
        } else {
          assert(r[at].type == 2 || r[at + 1].type == 2);
          if (r[at].type == 2 && r[at + 1].type == 2)
            ctype = ctype;
          else if (ctype == r[at].type || ctype == r[at + 1].type)
            ctype = 0;
          else
            ctype = 2;
        }
      } else
        assert(false);
    }
    assert(ctype == 0);
    ret *= sqrt((b - a) * (b - a)) / 100;
    printf("%.9lf\n", ret);
  }
}
int main() {
  run();
  return 0;
}

### Prompt

Please create a solution in Cpp 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;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 1000;
typedef struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
  P() {}
} P;
const P operator-(const P &a, const P &b) { return P(a.x - b.x, a.y - b.y); }
const long long operator^(const P &a, const P &b) {
  return (long long)a.x * b.y - (long long)a.y * b.x;
}
const long long operator*(const P &a, const P &b) {
  return (long long)a.x * b.x + (long long)a.y * b.y;
}
typedef struct R {
  long long num, den;
  int type;
} R;
bool operator<(const R &p, const R &q) {
  if ((p.num < 0) != (q.num < 0)) return p.num < 0;
  long long a = abs(p.num), b = p.den, c = abs(q.num), d = q.den;
  bool inv = p.num < 0;
  while (true) {
    long long aa = a / b, cc = c / d;
    if (aa != cc) return !inv ? aa < cc : cc < aa;
    a %= b, c %= d;
    if (a == 0 || c == 0) break;
    inv = !inv;
    swap(a, b);
    swap(c, d);
  }
  if (a != 0 || c != 0) return !inv ? a == 0 : c == 0;
  return false;
}
P p[MAXN + 1];
int n, nq;
R r[2 * MAXN];
int nr;
char buff[100];
int read() {
  scanf("%s", buff);
  int at = 0, sgn = +1, ret = 0, flen = 0;
  if (buff[at] == '-') sgn = -sgn, ++at;
  while (isdigit(buff[at])) ret = ret * 10 + buff[at++] - '0';
  if (buff[at] == '.') ++at;
  while (isdigit(buff[at])) ret = ret * 10 + buff[at++] - '0', ++flen;
  for (int i = (0); i < (2 - flen); ++i) ret = ret * 10;
  return sgn * ret;
}
void intersect(const P &a, const P &b, const P &c, const P &d) {
  long long den = (d - c) ^ (b - a);
  if (den == 0) {
    long long side = (a - c) ^ (d - c);
    bool hor = d.x == c.x;
    if (side != 0) return;
    r[nr].num = !hor ? a.x - c.x : a.y - c.y,
    r[nr].den = !hor ? d.x - c.x : d.y - c.y, r[nr].type = 2, ++nr;
    r[nr].num = !hor ? b.x - c.x : b.y - c.y,
    r[nr].den = !hor ? d.x - c.x : d.y - c.y, r[nr].type = 2, ++nr;
  } else {
    long long num1 = (a - c) ^ (d - c), num2 = (a - c) ^ (b - a);
    if (den < 0) num1 = -num1, num2 = -num2, den = -den;
    if (num1 < 0 || num1 > den) return;
    r[nr].num = num2, r[nr].den = den,
    r[nr].type = num1 == 0     ? (((b - c) ^ (d - c)) > 0 ? +1 : -1)
                 : num1 == den ? (((a - c) ^ (d - c)) > 0 ? +1 : -1)
                               : 0,
    ++nr;
  }
}
void run() {
  scanf("%d%d", &n, &nq);
  for (int i = (0); i < (n); ++i) p[i].x = read(), p[i].y = read();
  p[n] = p[0];
  for (int qi = (0); qi < (nq); ++qi) {
    P a, b;
    a.x = read(), a.y = read(), b.x = read(), b.y = read();
    nr = 0;
    for (int i = (0); i < (n); ++i) intersect(p[i], p[i + 1], a, b);
    for (int i = (0); i < (n); ++i)
      if (r[i].den < 0) r[i].den = -r[i].den, r[i].num = -r[i].num;
    sort(r, r + nr);
    double ret = 0;
    int ctype = 0;
    for (int at = 0, to = at; at < nr; at = to++) {
      if (ctype != 0)
        ret +=
            1.0 * r[at].num / r[at].den - 1.0 * r[at - 1].num / r[at - 1].den;
      while (to < nr && !(r[at] < r[to])) ++to;
      if (to - at == 1) {
        assert(ctype == 0 || ctype == 2);
        assert(r[at].type == 0);
        ctype = 2 - ctype;
      } else if (to - at == 2) {
        assert(r[at].type != 0 && r[at + 1].type != 0);
        if (ctype == 0 || ctype == 2) {
          assert(r[at].type != 2 || r[at + 1].type != 2);
          if (r[at].type == r[at + 1].type)
            ctype = ctype;
          else if (r[at].type == -r[at + 1].type)
            ctype = 2 - ctype;
          else
            ctype = ctype == 0 ? r[at].type + r[at + 1].type - 2
                               : 2 - r[at].type - r[at + 1].type;
        } else {
          assert(r[at].type == 2 || r[at + 1].type == 2);
          if (r[at].type == 2 && r[at + 1].type == 2)
            ctype = ctype;
          else if (ctype == r[at].type || ctype == r[at + 1].type)
            ctype = 0;
          else
            ctype = 2;
        }
      } else
        assert(false);
    }
    assert(ctype == 0);
    ret *= sqrt((b - a) * (b - a)) / 100;
    printf("%.9lf\n", ret);
  }
}
int main() {
  run();
  return 0;
}

```