#include <bits/stdc++.h>
using namespace std;
template <class T, class F>
struct CoordinateCompression {
  F comp;
  vector<T> o;
  CoordinateCompression(F comp) : comp(comp) {}
  void push(const T& x) { o.push_back(x); }
  int build() {
    sort(o.begin(), o.end(), comp);
    o.resize(unique(o.begin(), o.end()) - o.begin());
    return o.size();
  }
  int operator()(const T& x) {
    auto it = lower_bound(o.begin(), o.end(), x, comp);
    return it - o.begin();
  }
  T& operator[](int i) { return o[i]; }
};
struct DSU {
  vector<int> par, rank, best;
  DSU(int n) : par(n), rank(n, 0), best(n) {
    iota(par.begin(), par.end(), 0);
    iota(best.begin(), best.end(), 0);
  }
  int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); }
  bool connected(int u, int v) { return find(u) == find(v); }
  void merge(int u, int v) {
    u = find(u), v = find(v);
    if (u != v) {
      if (rank[u] < rank[v])
        swap(u, v);
      else if (rank[u] == rank[v])
        rank[u]++;
      par[v] = u;
      best[u] = max(best[v], best[u]);
    }
  }
};
const double EPS = 1e-9;
int sgn(double t) { return abs(t) < EPS ? 0 : (t > 0) - (t < 0); }
void spit(double t) { printf("%.7f\n", t); }
void suck(complex<double>& p) {
  double x, y;
  scanf("%lf %lf", &x, &y);
  p.real(x), p.imag(y);
}
void spit(complex<double> p) { spit(p.real()), printf(" "), spit(p.imag()); }
double dot(complex<double> u, complex<double> v) {
  return u.real() * v.real() + u.imag() * v.imag();
}
double cross(complex<double> u, complex<double> v) {
  return u.real() * v.imag() - v.real() * u.imag();
}
double orient(complex<double> a, complex<double> b, complex<double> c) {
  return cross(b - a, c - a);
}
struct Line {
  complex<double> v;
  double c;
  Line(complex<double> a, complex<double> b) : v(b - a), c(cross(v, a)) {}
  bool contains(complex<double> p) { return sgn(cross(v, p) - c) == 0; }
  bool compProj(complex<double> p, complex<double> q) {
    return sgn(dot(v, p) - dot(v, q)) < 0;
  }
  double side(complex<double> p) { return cross(v, p) - c; }
  bool oppose(complex<double> a, complex<double> b) {
    return sgn(side(a)) * sgn(side(b)) < 0;
  }
};
complex<double> inter(Line l, Line r) {
  return (r.v * l.c - l.v * r.c) / cross(l.v, r.v);
}
struct Polygon {
  int sides;
  vector<complex<double>> p;
  Polygon(vector<complex<double>>&& v) : p(v), sides(v.size()) {}
  int next(int i) { return i == sides - 1 ? 0 : i + 1; }
  double shoelace() {
    double sum = 0;
    for (int i = 0; i < sides; i++) sum += cross(p[i], p[next(i)]);
    return sum;
  }
  double solve(Line l) {
    CoordinateCompression<complex<double>,
                          function<bool(complex<double>, complex<double>)>>
        cc([&](complex<double> a, complex<double> b) {
          return l.compProj(a, b);
        });
    for (int i = 0; i < sides; i++) {
      if (l.contains(p[i])) {
        cc.push(p[i]);
      } else if (l.contains(p[next(i)])) {
        cc.push(p[next(i)]);
      } else if (l.oppose(p[i], p[next(i)])) {
        cc.push(inter(l, Line(p[i], p[next(i)])));
      }
    }
    int m = cc.build();
    vector<bool> straight(m);
    DSU dsu(m);
    for (int i = 0; i < sides; i++) {
      if (l.contains(p[i]) && l.contains(p[next(i)])) {
        int u = cc(p[i]), v = cc(p[next(i)]);
        dsu.merge(u, v);
        straight[u] = straight[v] = true;
      }
    }
    vector<int> cn(m);
    for (int i = 0; i < sides; i++) {
      complex<double> a = p[i], b = p[next(i)], e = p[next(i)] - p[i];
      int s = sgn(cross(e, l.v));
      if (l.contains(a)) {
        if (s > 0)
          cn[cc(a)]++;
        else if (s < 0)
          cn[cc(a)]--;
      } else if (l.contains(b)) {
        if (s > 0)
          cn[cc(b)]++;
        else if (s < 0)
          cn[cc(b)]--;
      } else if (l.oppose(a, b)) {
        complex<double> q = inter(l, Line(a, b));
        if (s > 0)
          cn[cc(q)]++;
        else if (s < 0)
          cn[cc(q)]--;
      }
    }
    double sum = 0;
    bool in = false;
    complex<double> wait;
    for (int i = 0; i < m; i++) {
      if (straight[i]) {
        if (!in) {
          in = true;
          wait = cc[i];
        } else if (dsu.best[dsu.find(i)] == i && cn[i] < 0) {
          sum += abs(cc[i] - wait);
          in = false;
        }
      } else if (cn[i] > 0) {
        wait = cc[i];
        in = true;
      } else if (cn[i] < 0) {
        sum += abs(cc[i] - wait);
        in = false;
      }
    }
    return sum;
  }
};
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<complex<double>> v(n);
  for (int i = 0; i < n; i++) suck(v[i]);
  Polygon pol(move(v));
  if (sgn(pol.shoelace()) < 0) {
    reverse(pol.p.begin(), pol.p.end());
  }
  while (m--) {
    complex<double> a, b;
    suck(a), suck(b);
    spit(pol.solve(Line(a, b)));
  }
}

### 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;
template <class T, class F>
struct CoordinateCompression {
  F comp;
  vector<T> o;
  CoordinateCompression(F comp) : comp(comp) {}
  void push(const T& x) { o.push_back(x); }
  int build() {
    sort(o.begin(), o.end(), comp);
    o.resize(unique(o.begin(), o.end()) - o.begin());
    return o.size();
  }
  int operator()(const T& x) {
    auto it = lower_bound(o.begin(), o.end(), x, comp);
    return it - o.begin();
  }
  T& operator[](int i) { return o[i]; }
};
struct DSU {
  vector<int> par, rank, best;
  DSU(int n) : par(n), rank(n, 0), best(n) {
    iota(par.begin(), par.end(), 0);
    iota(best.begin(), best.end(), 0);
  }
  int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); }
  bool connected(int u, int v) { return find(u) == find(v); }
  void merge(int u, int v) {
    u = find(u), v = find(v);
    if (u != v) {
      if (rank[u] < rank[v])
        swap(u, v);
      else if (rank[u] == rank[v])
        rank[u]++;
      par[v] = u;
      best[u] = max(best[v], best[u]);
    }
  }
};
const double EPS = 1e-9;
int sgn(double t) { return abs(t) < EPS ? 0 : (t > 0) - (t < 0); }
void spit(double t) { printf("%.7f\n", t); }
void suck(complex<double>& p) {
  double x, y;
  scanf("%lf %lf", &x, &y);
  p.real(x), p.imag(y);
}
void spit(complex<double> p) { spit(p.real()), printf(" "), spit(p.imag()); }
double dot(complex<double> u, complex<double> v) {
  return u.real() * v.real() + u.imag() * v.imag();
}
double cross(complex<double> u, complex<double> v) {
  return u.real() * v.imag() - v.real() * u.imag();
}
double orient(complex<double> a, complex<double> b, complex<double> c) {
  return cross(b - a, c - a);
}
struct Line {
  complex<double> v;
  double c;
  Line(complex<double> a, complex<double> b) : v(b - a), c(cross(v, a)) {}
  bool contains(complex<double> p) { return sgn(cross(v, p) - c) == 0; }
  bool compProj(complex<double> p, complex<double> q) {
    return sgn(dot(v, p) - dot(v, q)) < 0;
  }
  double side(complex<double> p) { return cross(v, p) - c; }
  bool oppose(complex<double> a, complex<double> b) {
    return sgn(side(a)) * sgn(side(b)) < 0;
  }
};
complex<double> inter(Line l, Line r) {
  return (r.v * l.c - l.v * r.c) / cross(l.v, r.v);
}
struct Polygon {
  int sides;
  vector<complex<double>> p;
  Polygon(vector<complex<double>>&& v) : p(v), sides(v.size()) {}
  int next(int i) { return i == sides - 1 ? 0 : i + 1; }
  double shoelace() {
    double sum = 0;
    for (int i = 0; i < sides; i++) sum += cross(p[i], p[next(i)]);
    return sum;
  }
  double solve(Line l) {
    CoordinateCompression<complex<double>,
                          function<bool(complex<double>, complex<double>)>>
        cc([&](complex<double> a, complex<double> b) {
          return l.compProj(a, b);
        });
    for (int i = 0; i < sides; i++) {
      if (l.contains(p[i])) {
        cc.push(p[i]);
      } else if (l.contains(p[next(i)])) {
        cc.push(p[next(i)]);
      } else if (l.oppose(p[i], p[next(i)])) {
        cc.push(inter(l, Line(p[i], p[next(i)])));
      }
    }
    int m = cc.build();
    vector<bool> straight(m);
    DSU dsu(m);
    for (int i = 0; i < sides; i++) {
      if (l.contains(p[i]) && l.contains(p[next(i)])) {
        int u = cc(p[i]), v = cc(p[next(i)]);
        dsu.merge(u, v);
        straight[u] = straight[v] = true;
      }
    }
    vector<int> cn(m);
    for (int i = 0; i < sides; i++) {
      complex<double> a = p[i], b = p[next(i)], e = p[next(i)] - p[i];
      int s = sgn(cross(e, l.v));
      if (l.contains(a)) {
        if (s > 0)
          cn[cc(a)]++;
        else if (s < 0)
          cn[cc(a)]--;
      } else if (l.contains(b)) {
        if (s > 0)
          cn[cc(b)]++;
        else if (s < 0)
          cn[cc(b)]--;
      } else if (l.oppose(a, b)) {
        complex<double> q = inter(l, Line(a, b));
        if (s > 0)
          cn[cc(q)]++;
        else if (s < 0)
          cn[cc(q)]--;
      }
    }
    double sum = 0;
    bool in = false;
    complex<double> wait;
    for (int i = 0; i < m; i++) {
      if (straight[i]) {
        if (!in) {
          in = true;
          wait = cc[i];
        } else if (dsu.best[dsu.find(i)] == i && cn[i] < 0) {
          sum += abs(cc[i] - wait);
          in = false;
        }
      } else if (cn[i] > 0) {
        wait = cc[i];
        in = true;
      } else if (cn[i] < 0) {
        sum += abs(cc[i] - wait);
        in = false;
      }
    }
    return sum;
  }
};
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<complex<double>> v(n);
  for (int i = 0; i < n; i++) suck(v[i]);
  Polygon pol(move(v));
  if (sgn(pol.shoelace()) < 0) {
    reverse(pol.p.begin(), pol.p.end());
  }
  while (m--) {
    complex<double> a, b;
    suck(a), suck(b);
    spit(pol.solve(Line(a, b)));
  }
}

```