Prob Colliding Balls
Prob Colliding Balls
Problem Statement
There are n red balls kept on the positive X axis, and m blue balls kept on the
positive Y axis. You are given the positions of the balls. For each i from 1 to n,
the i-th red ball has the coordinates (xi , 0), where xi is a positive integer. For
each i from 1 to m, the i-th blue ball has the coordinates (0, yi ), where yi is a
positive integer.
It is given that all xi are distinct. Also, all yi are distinct.
At time t = 0, for each i from 1 to n, the i-th red ball is thrown towards
the positive Y axis with a speed of ui (that is, with velocity vector (0, ui )).
Simultaneously (at time t = 0), for each i from 1 to m, the i-th blue ball is
thrown towards the positive X axis with a speed of vi (that is, with velocity
vector (vi , 0)).
Two balls are said to collide if they are at the same position at the same
time. When two balls collide, they disappear and no longer collide with other
balls.
Your task is to find the total number of collisions that occur between the
balls.
Input
The first line contains n and m, the number of red balls and the number
of blue balls, respectively.
The next n lines each contain two space-separated integers xi and ui ,
representing the position and speed of the i-th red ball, respectively.
The next m lines each contain two space-separated integers yi and vi ,
representing the position and speed of the i-th blue ball, respectively.
Output
Print the total number of collisions.
1
Constraints
1 ≤ n, m ≤ 105
1 ≤ xi , ui , yi , vi ≤ 109
Example
Input 1
1 1
1 2
2 1
Output 1
1
Input 2
1 2
1 2
2 1
1 2
Output 2
1
Explanation
Example Case 1: The balls collide at t = 1, at the coordinates (1, 2).
Example Case 2: The red ball and the second blue ball collide at time
t = 0.5 at coordinates (1, 1). Note that the first blue ball would have
collided with the red ball at t = 1 (like in sample input #1), if the second
blue ball wasn’t present. However, since the red ball disappears at t = 0.5,
its collision with the first blue ball does not happen. Thus, the total
number of collisions is 1.