#include #include #include #include using namespace std; typedef complex VEC; struct LINE{ VEC a, b; }; const double EPS = 0.000000001; int CCW(const VEC& from, const VEC& to, const VEC& target) { VEC a = to - from; VEC b = target - from; double c = a.real() * b.imag() - b.real() * a.imag(); if (abs(c) < EPS){ return 0; } return (c > 0.0) ? 1 : -1; } bool IsCross(const LINE& a, const LINE& b) { const VEC& p1 = a.a; const VEC& p2 = a.b; const VEC& q1 = b.a; const VEC& q2 = b.b; bool res = (CCW(p1, p2, q1) * CCW(p1, p2, q2) < 0) && (CCW(q1, q2, p1) * CCW(q1, q2, p2) < 0); //cerr << p1 << p2 << q1 << q2 << res << endl; return res; } void CalcAB(const LINE& l, double& a, double& b) { double x1 = l.a.real(); double x2 = l.b.real(); double y1 = l.a.imag(); double y2 = l.b.imag(); a = (y2 - y1) / (x2 - x1); b = y1 - a * x1; } VEC Calc(const LINE& l1, const LINE& l2) { double a1, b1, a2, b2; CalcAB(l1, a1, b1); CalcAB(l2, a2, b2); double x = (b2 - b1) / (a1 - a2); double y = a1 * x + b1; return VEC(x, y); } const VEC ROT(polar(1.0, 1.0)); int main() { int loop; cin >> loop; double xa, ya, xb, yb; while (cin >> xa >> ya >> xb >> yb){ const VEC ca(VEC(xa, ya) * ROT); const VEC cb(VEC(xb, yb) * ROT); const LINE main_line = {ca, cb}; int n; cin >> n; vector > v; for (int i = 0; i != n; ++i){ double xs, ys, xt, yt; int o, l; cin >> xs >> ys >> xt >> yt >> o >> l; LINE line; line.a = VEC(xs, ys) * ROT; line.b = VEC(xt, yt) * ROT; v.push_back(make_pair(line, o ^ l)); } vector > vv; for (vector >::iterator it = v.begin(); it != v.end(); ++it){ LINE& l = it->first; int ol = it->second; if (IsCross(l, main_line)){ const VEC pos = Calc(l, main_line); vv.push_back(make_pair(abs(pos - ca), ol)); } } sort(vv.begin(), vv.end()); double last = -1.0; vector ans; for (vector >::iterator it = vv.begin(); it != vv.end(); ++it){ //cerr << it->first << " "; if (abs(it->first - last) < EPS){ continue; } ans.push_back(it->second); } ans.erase(unique(ans.begin(), ans.end()), ans.end()); cout << max(0, ((int)ans.size() - 1)) << endl; } }