Counting Rectangles
InterviewTime limit1sMemory limit128 MB
Count rectangles whose four corners are all intersection points of horizontal and vertical segments in a figure.
- Level
Medium6 of 10
- Topics
- Geometry, Hash map, Brute force, Implementation
- Solved
- No attempts yet
Problem
You are given a figure made up of only horizontal and vertical line segments. Your goal is to count how many distinct rectangles are formed by these segments. For example, the two figures below contain 5 and 0 rectangles respectively.

The figure contains many intersection points. An intersection point is a point shared by two or more segments. The input segments are guaranteed to be arranged so that every intersection point is formed by exactly one horizontal segment meeting exactly one vertical segment.
A rectangle is bounded by two distinct horizontal segments and two distinct vertical segments, and all four of its corners must be intersection points.
Input
The first line contains a single integer , the number of test cases (). The data for the test cases follows. Each test case begins with a line containing , the number of segments in the figure (). The next lines each contain four integers , the and coordinates of the two endpoints of one segment. All coordinates are integers between and , and every segment is either horizontal or vertical.
Output
For each test case, print the number of distinct rectangles in its figure. Print the answer for each test case on its own line.