Counting Rectangles Drawn with Segments
Time limit1sMemory limit16 MB
Count axis-aligned rectangles whose four sides are fully covered by the union of the given collinear segments.
- Level
Medium7 of 10
- Topics
- Geometry, Hash map, Sorting, Brute force
- Solved
- No attempts yet
Problem
Marek loves mathematics, especially geometry. One idle afternoon he grabbed his favourite ruler and drew a handful of horizontal and vertical line segments. When he looked at the paper afterwards he noticed rectangles outlined by the segments, but every time he recounted them he got a different total, so he needs your help.
You are given axis-aligned segments; each one is either horizontal (parallel to the x-axis) or vertical (parallel to the y-axis). Your task is to count how many axis-aligned rectangles are outlined by the drawn segments.
A rectangle has its left and right sides on the vertical lines and its bottom and top sides on the horizontal lines . It is counted only when all four sides are completely drawn:
- the vertical segments at jointly cover the whole side from to with no gap;
- the vertical segments at jointly cover the side from to ;
- the horizontal segments at jointly cover the side from to ;
- the horizontal segments at jointly cover the side from to .
A single side may be covered by the union of several collinear segments that overlap or merely touch, and segments are allowed to overlap or coincide.
Input
The first line contains the number of segments ().
Each of the next lines contains four integers , , , (), where is one endpoint of a segment and is the other. Every segment is parallel to the x-axis or to the y-axis.
Output
Print a single line containing the number of rectangles.
Note
In the first test case the rectangles split by size into four , three , two , and one , giving in total. Note that the bottom side of the widest rectangle is covered by two horizontal segments that touch, which still counts as fully drawn.