0% found this document useful (0 votes)
7 views2 pages

Prob Colliding Balls

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Prob Colliding Balls

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Colliding Balls Problem

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

ˆ For all 1 ≤ i < j ≤ n, xi ̸= xj

ˆ For all 1 ≤ i < j ≤ m, yi ̸= yj

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.

You might also like