Railroad Conflict
Time limit8sMemory limit512 MB
Given segment AB and up to 100 existing segments, each with owner and elevation, find the minimum number of elevation changes along AB needed to satisfy crossing constraints.
- Level
Medium7 of 10
- Topics
- Geometry, Sorting, Greedy, Implementation
- Solved
- No attempts yet
Problem
Nate U. Smith runs a railroad company in a large metropolitan area. Besides his company, there is a rival railroad company in this metropolitan area. The two companies compete with each other.
In this metropolitan area, to make train operation easy, every line follows the segment connecting its two endpoint stations. Also, to avoid level crossings, every line is built either elevated or underground. An existing line runs either elevated along its entire length or underground along its entire length.
Recently, two districts A and B in this metropolitan area have been heavily developed, so Nate's company has decided to build a new line between these districts. Like the existing lines, this new line should follow the segment whose endpoints are A and B, but because other lines lie along its path, it cannot be built entirely elevated or entirely underground. Therefore, part of the line is built elevated and the rest underground. Where the new line crosses one of his own company's existing lines, to make transfers between the new line and the existing line convenient, if the existing line is elevated then the new line also runs elevated, and if the existing line is underground then the new line also runs underground. Where the new line crosses another company's existing line, to avoid interference from the other company, if the existing line is elevated then the new line runs underground, and if the existing line is underground then the new line runs elevated. It does not matter whether station A and station B are each built elevated or underground.
Naturally, entrances must be placed where the new line changes from elevated to underground or from underground to elevated. Placing an entrance costs money, so the company wants to minimize the number of entrances on the new line. Nate thought he would need the help of a programmer for this, and summoned you to his company.
Your job is to write a program that, given the positions of station A and station B and information about the existing lines, finds the minimum number of entrances that must be placed on the new line.
The following figures show the contents of the sample input and output.


Input
The first line of the input contains a single positive integer, which is the number of datasets. Each dataset is given in the following format.
xa ya xb yb
n
xs1 ys1 xt1 yt1 o1 l1
xs2 ys2 xt2 yt2 o2 l2
...
xsn ysn xtn ytn on ln
Here, (xa,ya) and (xb,yb) are the coordinates of station A and station B, respectively. N is a positive integer at most 100, giving the number of existing lines. (xs**i,ys**i) and (xt**i,yt**i) are the coordinates of the start point and the end point of the i-th existing line. o**i is an integer giving the owner of the i-th existing line. It is either 1 or 0, where 1 means the line is owned by his company and 0 means it is owned by the other company. l**i is an integer giving whether the i-th existing line is elevated or underground. It is also either 1 or 0, where 1 means the line is elevated and 0 means it is underground.
The x and y coordinate values appearing in the input are in the range -10000 to 10000, inclusive. Also, in each dataset, for all lines including the new line, you may assume the following conditions hold. Here, two points being very close means the distance between them is at most 10-9.
- No other intersection point is very close to an intersection point.
- No other line passes very close to an endpoint of a line.
- Parts or all of two lines do not overlap.
Output
For each dataset, print the minimum number of entrances that must be placed on one line.