#P7712. Critical Segments

    ID: 20899 Type: Default 1000ms 256MiB

Critical Segments

Critical Segments

On the plane, you are given \( n \) horizontal line segments and \( n \) vertical line segments. It is guaranteed that no two horizontal segments lie on the same horizontal line and no two vertical segments lie on the same vertical line.

Two segments are considered connected if they have an intersection point. Furthermore, connectivity is transitive: if segment \( a \) is connected to \( b \) and \( b \) is connected to \( c \), then \( a \) is connected to \( c \). A segment \( a \) is defined as critical if and only if there exist two other segments \( b \) and \( c \) such that \( b \) and \( c \) are connected in the original configuration, but after removing segment \( a \), \( b \) and \( c \) become disconnected.

Your task is to determine the number of critical segments among all the given segments.

inputFormat

The input begins with an integer \( n \) (the number of horizontal segments and also the number of vertical segments).

This is followed by \( n \) lines each containing three integers \( y\, x_1\, x_2 \), representing a horizontal segment at \( y \) spanning from \( x_1 \) to \( x_2 \) (assume \( x_1 \le x_2 \); if not, you may swap them).

After that, there are \( n \) lines each containing three integers \( x\, y_1\, y_2 \), representing a vertical segment at \( x \) spanning from \( y_1 \) to \( y_2 \) (assume \( y_1 \le y_2 \); if not, swap them).

A horizontal segment \( (y, x_1, x_2) \) and a vertical segment \( (x, y_1, y_2) \) intersect if and only if \[ x_1 \le x \le x_2 \quad\text{and}\quad y_1 \le y \le y_2. \]

outputFormat

Output a single integer representing the total number of critical segments. A segment is critical if there exist two other segments which are connected originally, but become disconnected after its removal.

sample

1
0 0 2
1 -1 1
0

</p>