Language: C++

#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <iterator>
#include <limits>

using namespace std;

typedef pair<double, double> point;
typedef pair<double, double> line;

typedef struct {
    double x, y;
} pt;

typedef pair<pt, pt> seg;

seg mkseg(double ax, double ay, double bx, double by) {
    pt a = {ax, ay};
    pt b = {bx, by};
    return seg(a, b);
}

double inf = numeric_limits<double>::infinity();

pt seg_cross(seg a, seg b) {
    double A = (a.second.x - a.first.x) / (a.second.y - a.first.y);
    double B = (b.second.x - b.first.x) / (b.second.y - b.first.y);

    if (A == inf) {
        double y = a.second.y;
        double x = B * (y - b.first.y) + b.first.x;
        pt p = {x, y};
        return p;
    }

    if (B == inf) {
        double y = b.second.y;
        double x = A * (y - a.first.y) + a.first.x;
        pt p = {x, y};
        return p;
    }

    double y = (A * a.first.y - B * b.first.y + b.first.x - a.first.x) / (A - B);
    double x = A * (y - a.first.y) + a.first.x;
    pt p = {x, y};
    return p;
}

typedef struct {
    line line;
    int minx, maxx;
    bool flag;
} route;

point cross_point(line a, line b) {
    double x = - (b.second - a.second) / (b.first - a.first);
    double y = x * a.first + a.second;
    return point(x, y);
}

line line_from_point(int ax, int ay, int bx, int by) {
    double a = (ay - by) / (ax - bx);
    return line(a, ay - a * ax);
}

void solve() {
    int ax, ay, bx, by;
    cin >> ax >> ay >> bx >> by;

    seg new_seg = mkseg(ax, ay, bx, by);

    int n;
    cin >> n;

    vector< pair<pt, bool> > pts;

    while (n--) {
        int ax_, ay_, bx_, by_;
        int pos, owner;
        cin >> ax_ >> ay_ >> bx_ >> by_ >> pos >> owner;

        seg s = mkseg(ax_, ay_, bx_, by_);

        double minx = min(ax_, bx_);
        double maxx = max(ax_, bx_);

        pt c = seg_cross(new_seg, s);
        cerr << "CROSS AT " << c.x << "," << c.y << endl;

        bool flag = (pos == 1) ^ (owner == 1);
        if (minx <= c.x && c.x <= maxx && min(ax, bx) <= c.x && c.x <= max(ax, bx)) {
            cerr << "PUSH " << c.x << "," << c.y << endl;
            pts.push_back(make_pair(c, flag));
        }

        // route r;
        // line l = line_from_point(ax_, ay_, bx_, by_);
        // r.minx = min(ax_, bx_);
        // r.maxx = max(ax_, bx_);
        // r.flag = (pos == 1) ^ (owner == 1);

        // point cp = cross_point(new_line, l);
        // double cpx = cp.first;
        // if (r.minx <= cpx && cpx <= r.maxx && ax <= cpx && cpx <= bx) {
        //     cross_points.push_back(make_pair(cp, r.flag));
        // }
    }

    struct {
        bool operator()(pair<pt, bool> a, pair<pt, bool> b) {
            return a.first.x < b.first.x;
        }
    } by_x;

    sort(pts.begin(), pts.end(), by_x);

    int curr_pos = -1;
    int cnt = 0;
    for (vector< pair<pt, bool> >::iterator it = pts.begin(); it != pts.end(); it++) {
        pair<pt, bool> p = *it;
        cerr << "FOUND " << p.first.x << " " << p.first.y << " " << p.second << endl;
        if (curr_pos != -1) {
            if (curr_pos != p.second) {
                cnt++;
            }
        }
        curr_pos = p.second;
    }
    cout << cnt << endl;

    /*
    struct {
        bool operator()(pair<point, bool> a, pair<point, bool> b) {
            return a.first.first < b.first.first;
        }
    } by_x;

    sort(cross_points.begin(), cross_points.end(), by_x);

    int curr_pos = -1;
    int cnt = 0;
    for (vector< pair<point, bool> >::iterator it = cross_points.begin(); it != cross_points.end(); it++) {
        pair<point, bool> p = *it;
        // cout << p.first.first << " " << p.first.second << " " << p.second << endl;
        if (curr_pos != -1) {
            if (curr_pos != p.second) {
                cnt++;
            }
        }
        curr_pos = p.second;
    }
    cout << cnt << endl;
    */
}

int main() {
    int N;
    cin >> N;
    while (N--) {
        solve();
    }
}

Compiler output:
a.cc:52:10: error: declaration of 'line <unnamed struct>::line' changes meaning of 'line' [-Wchanges-meaning]
   52 |     line line;
      |          ^~~~
a.cc:52:5: note: used here to mean 'typedef struct std::pair<double, double> line'
   52 |     line line;
      |     ^~~~
a.cc:11:30: note: declared here
   11 | typedef pair<double, double> line;
      |                              ^~~~