Language: C++

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
#include <list>
#include <tuple>
#include <bitset>
#include <ciso646>
#include <complex>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; }
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
template<class T> inline T sqr(T x) { return x*x; }

#define For(i,a,b)	for(int (i) = (a);i < (b);(i)++)
#define rep(i,n)	For(i,0,n)
#define clr(a)		memset((a), 0 ,sizeof(a))
#define mclr(a)		memset((a), -1 ,sizeof(a))
#define all(a)		(a).begin(),(a).end()
#define sz(a)		(sizeof(a))
#define Fill(a,v)	fill((int*)a,(int*)(a+(sz(a)/sz(*(a)))),v)

const int mod = 1000000007;
const int INF = 1e9;


typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> VP;
const D EPS = 1e-9;
#define EQ(n,m) (abs((n)-(m)) < EPS)

D dot(P a, P b){
	return (conj(a)* b).real();
}

D cross(P a, P b){
	return (conj(a)* b).imag();
}

int ccw(P a, P b, P c){
	b -= a, c -= a;
	if (cross(b, c) > EPS) return 1;
	if (cross(b, c) > -EPS) return -1;
	if (dot(b, c) > -EPS) return 2;
	if (norm(b) < norm(c)) return -2;
	return 0;
}

bool isecLP(P a1, P a2, P b){
	return abs(ccw(a1, a2, b)) != 1;
}
bool isecLL(P a1, P a2, P b1, P b2) {
	return !isecLP(a2 - a1, b2 - b1, 0) || isecLP(a1, b1, b2);
}

P crosspointLL(P a1, P a2, P b1, P b2) {
	D d1 = cross(b2 - b1, b1 - a1);
	D d2 = cross(b2 - b1, a2 - a1);
	if (EQ(d1, 0) && EQ(d2, 0)) return a1;  // same line
	if (EQ(d2, 0)) throw "kouten ga nai";   // ???????????????
	return a1 + d1 / d2 * (a2 - a1);
}


int main()
{
	int N;
	cin >> N;
	rep(_, N){

		double sy, sx, gy, gx;
		cin >> sx >> sy >> gx >> gy;

		L sl(pair<P,P>(P(sx,sy),P(gx,gy)));

		int n;
		cin >> n;

		typedef pair<L, pair<int, int>> LL;
		vector<LL> l;

		rep(i, n){
			cin >> sx >> sy >> gx >> gy;
			int o, tl;
			cin >> o >> tl;

			LL t;
			t.first.first._Val[0] = sx;
			t.first.first._Val[1] = sy;
			t.first.second._Val[0] = gx;
			t.first.second._Val[1] = gy;

			t.second.first = o;
			t.second.second = tl;

			l.push_back(t);
		}
		vector<int> vo, vl;
		vector<P> point;

		rep(i, l.size()){
			auto lp1 = l[i].first.first;
			auto lp2 = l[i].first.first;
			auto data = l[i].second;

			if (isecLL(sl.first, sl.second, lp1, lp2)){
				auto p = crosspointLL(sl.first, sl.second, lp1, lp2);
				point.push_back(p);
				vo.push_back(data.first);
				vl.push_back(data.second);
			}
		}

		bool f = true;
		while (f){
			f = false;
			rep(i, point.size() - 1){
				double x, y, tx, ty;
				x = point[i].imag(); y = point[i].real();
				tx = point[i+1].imag(); ty = point[i+1].real();
				double l1 = (x - sx) * (x - sx) + (y - sy) * (y - sy), l2 = (tx - sx) * (tx - sx) + (ty - sy) * (ty - sy);

				if (l1 > l2){
					swap(point[i], point[i + 1]);
					swap(vo[i], vo[i + 1]);
					swap(vl[i], vl[i + 1]);
					f = true;
				}

			}
		}

		int ans = INF;
		rep(j, 2){
			int val = 0;
			int sj = j;
			rep(i, point.size()){

				if (vo[i] == 1){
					if (vl[i] != sj){
						val++;
						sj = vl[i];
					}
				}
				if (vo[i] == 0){
					if (vl[i] == sj){
						val++;
						sj = (1-sj);
					}
				}

			}
			ans = min(ans, val);
		}
		cout << ans << endl;

	}



	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:103:39: error: 'class std::complex<double>' has no member named '_Val'
  103 |                         t.first.first._Val[0] = sx;
      |                                       ^~~~
a.cc:104:39: error: 'class std::complex<double>' has no member named '_Val'
  104 |                         t.first.first._Val[1] = sy;
      |                                       ^~~~
a.cc:105:40: error: 'class std::complex<double>' has no member named '_Val'
  105 |                         t.first.second._Val[0] = gx;
      |                                        ^~~~
a.cc:106:40: error: 'class std::complex<double>' has no member named '_Val'
  106 |                         t.first.second._Val[1] = gy;
      |                                        ^~~~