#C3713. Non-Colliding Robot Paths

    ID: 47171 Type: Default 1000ms 256MiB

Non-Colliding Robot Paths

Non-Colliding Robot Paths

You are given a set of robots, each following a path on a 2D grid. Each path is a straight line either in the horizontal or vertical direction. A path is represented by its start and end coordinates: (x_start, y_start, x_end, y_end). Two robots' paths are said to collide if they share at least one grid point.

The collision condition can be formally defined as follows: For two paths, let their sets of grid points be \(S_1\) and \(S_2\). The paths collide if \(S_1 \cap S_2 \neq \emptyset\). Otherwise, they do not collide.

Your task is to count, for each test case, the number of pairs of robots whose paths do not collide.

inputFormat

The input is read from stdin and consists of multiple test cases. The first line contains an integer T representing the number of test cases. For each test case:

  • The first line contains an integer R indicating the number of robots.
  • The next R lines each contain 4 integers: x_start y_start x_end y_end, representing the coordinates of the start and end points of a robot's path (which is guaranteed to be horizontal or vertical).

outputFormat

For each test case, output a single line to stdout with one integer: the number of pairs of robots that do not collide.

## sample
3
3
1 2 1 5
2 3 5 3
4 4 4 1
2
0 0 3 0
2 0 2 3
2
0 0 1 0
1 1 2 1
2

0 1

</p>