eolymp
bolt
Try our new interface for solving problems
Problems

Collisions

Collisions

Identical small balls are located on a straight line and can move along this line only. Each ball moves with a constant velocity, but velocities of different balls may be different. When two balls meet, a perfectly elastic collision occurs. It’s a common-known physical fact, that when two equal-mass physical bodies \textbf{A} and \textbf{B} collide perfectly elastically, they swap their velocities, i. e. new \textbf{A}’s velocity is old \textbf{B}’s one, and new \textbf{B}’s is old \textbf{A}’s. Your task is to write a program to find the total number of collisions. \InputFile The first line at input contains the number of balls \textbf{N} (\textbf{3} ≤ \textbf{N} ≤ \textbf{200000}). Each of the following \textbf{N} lines contains \textbf{2} space-separated integers --- the starting coordinate and the velocity of corresponding ball. All start coordinates are in range --10\textbf{^11} < \textbf{x} < \textbf{10^11}, all velocities are in range \textbf{--10^8} < \textbf{v} < \textbf{10^8}. All start coordinates are different. It’s guaranteed that each collision involves exactly two balls (none involves three or more balls together). \OutputFile Your program should output exactly one integer number in a single line -- the total number of collisions (or \textbf{987654321987654321} if the number is infinite).
Time limit 1 second
Memory limit 64 MiB
Input example #1
3
-5 3
0 -1
7 -2
Output example #1
3