#K40657. Counting Right-Angled Triangles with Axes-Aligned Legs

    ID: 26691 Type: Default 1000ms 256MiB

Counting Right-Angled Triangles with Axes-Aligned Legs

Counting Right-Angled Triangles with Axes-Aligned Legs

You are given N distinct points on a 2-dimensional plane. The task is to count the number of right-angled triangles that can be formed using these points such that the two legs of the triangle are parallel to the X and Y axes respectively. Formally, for a right-angled triangle with the right angle at point (x, y), there must exist another point with the same x-coordinate and a third point with the same y-coordinate. The number of triangles with right angle at (x, y) is ((\text{count}_x(x)-1) \times (\text{count}_y(y)-1)), where (\text{count}_x(x)) is the frequency of points that have the x-coordinate x, and similarly for y. Sum over all points gives the final answer.

Input/Output method: Read input from standard input (stdin) and print the result to standard output (stdout). This problem evaluates your understanding of coordinate geometry and combinatorial counting, and it is rated as a hard problem.

inputFormat

The first line of input contains a single integer N (0 (\le) N (\le) 105), representing the number of points. Each of the following N lines contains two integers x and y representing the coordinates of a point.

outputFormat

Output a single integer --- the number of right-angled triangles that can be formed with the given points such that the legs of the triangle are parallel to the axes.## sample

4
1 2
2 2
1 3
2 3
4