CCOQR '16 - Stupendous Bowties

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 3.0s
Memory limit: 64M

Author:
Problem types

There are N (1 \le N \le 100\,000) distinct points on a 2D plane, with the i^{th} one at integral coordinates (x_i, y_i) (-10^9 \le x_i \le 10^9, -10^9 \le y_i \le 10^9).

A Fantastic Right Triangle is one which uses 3 of the given points as its vertices, is non-degenerate (i.e., has positive area), and which has both of its legs axis-aligned (one of its two shortest sides is horizontal and the other shortest side is vertical). The vertex at which the two legs of such a triangle meet (which has an interior right angle) is known as the triangle's Spectacular Vertex.

A Stupendous Bowtie consists of an unordered pair of Fantastic Right Triangles which share the same point as their Spectacular Vertex, and which touch only at that point. Count the number of Stupendous Bowties which exist amongst the given points.

Subtasks

  • For 2 of the 15 marks available, N \le 20.
  • For another 3 of the 15 marks available, N \le 200.
  • For another 2 of the 15 marks available, N \le 2\,000.
  • For another 3 of the 15 marks available, N \le 100\,000, -10^5 \le x_i \le 10^5, and -10^5 \le y_i \le 10^5.

Input Specification

The first line of the input contains one integer, N. The remaining N lines each contain two integers, x_i and y_i for i = 1 \dots N.

Output Specification

Output a single integer, the number of Stupendous Bowties which exist amongst the given points. Note that this value may not fit in a 32-bit signed integer, and may need to be stored in a long long / long / int64 variable in C++ / Java / Pascal, respectively.

Sample Input

12
-3 6
1 6
-1 4
-3 2
1 2
4 2
-4 -3
-3 -3
1 -3
-1 -4
1 -4
-3 -6

Output for Sample Input

8

Explanation for Output for Sample Input

The diagram below illustrates the set of given points (represented by the 12 blue dots), with one of the eight possible Stupendous Bowties shown in red.


Comments


  • 1
    println_hi_  commented on Nov. 24, 2016, 7:28 a.m. edit 5
    python issue?

    I have two versions of a algorithm for this which takes O(N log N) time and O(N) space. However neither version can pass test case 21 without a MLE or a TLE. I feel as this algorithm would pass if written in a faster language such as C++ and I am sure I have the lowest possible space and time complexity.

    I know there are some solutions which AC are written in python, presumably because they are faster by a constant factor. Could someone please give me some advice on how to speed up my code?(I have already implemented FastIO and PyPy3 causes such a significant memory issue that I fail the first test case)


    • 3
      Xyene  commented on Nov. 24, 2016, 11:18 a.m.

      Your submission passes as-is in Python 2. Python 3 is typically slower than Python 2 for contest scenarios.


      • 2
        println_hi_  commented on Nov. 24, 2016, 7:50 p.m.

        Ah thank you very much, that's very interesting. I'll have to research why.