← 返回 waymo 的题目列表Count Axis-Aligned Squares Formed by Intersecting Vertical and Horizontal Segments
类型:online_judge
Problem
You are given a set of vertical line segments and a set of horizontal line segments. Each segment is represented by two endpoints, and each point is represented as (x, y).
Return the number of axis-aligned squares that can be formed by these segments.
A valid square is formed by two distinct vertical segments and two distinct horizontal segments, similar to one cell in a # shape. More formally, there must exist two distinct x-coordinates x1 < x2 and two distinct y-coordinates y1 < y2 such that:
x2 - x1 == y2 - y1;
On x = x1 and x = x2, the given vertical segments cover the interval from y1 to y2;
On y = y1 and y = y2, the given horizontal segments cover the interval from x1 to x2.
Then these four sides form one valid square.
If duplicate or overlapping segments exist, count each unique geometric square only once, not by the number of segment combinations.
Input Format
n m
x1 y1 x2 y2 # next n lines are vertical segments, guaranteed x1 == x2
...
x1 y1 x2 y2 # next m lines are horizontal segments, guaranteed y1 == y2
...
Output Format
count
Print one integer, the number of axis-aligned squares.
Constraints
1 <= n, m <= 500
Coordinates are integers in [-10^9, 10^9]
Endpoint order is arbitrary
Example
Input:
2 2
0 0 0 2
2 0 2 2
0 0 2 0
0 2 2 2
Output:
1
Explanation: The four segments form one square with side length 2.
Example
Input
2 2
0 0 0 2
2 0 2 2
0 0 2 0
0 2 2 2
Output
1