Disjoint Triangles
Contest: Codeforces Round 505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
Difficulty rating: 2700
Tags: geometry

A point belongs to a triangle if it lies inside the triangle or on one of its sides. Two triangles are disjoint if there is no point on the plane that belongs to both triangles.

You are given $$$n$$$ points on the plane. No two points coincide and no three points are collinear.

Find the number of different ways to choose two disjoint triangles with vertices in the given points. Two ways which differ only in order of triangles or in order of vertices inside triangles are considered equal.

Input format:
The first line of the input contains an integer $$$n$$$ ($$$6 \le n \le 2000$$$) – the number of points.

Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$|x_i|, |y_i| \le 10^9$$$) – the coordinates of a point.

No two points coincide and no three points are collinear.

Output format:
Print one integer – the number of ways to choose two disjoint triangles.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example there are six pairs of disjoint triangles, they are shown on the picture below.

All other pairs of triangles are not disjoint, for example the following pair:

Example input:
6
1 1
2 2
4 6
4 5
7 2
5 3

Example output:
6

Example input:
7
0 -1000000000
-5 -5
5 -5
-5 0
5 0
-2 2
2 2

Example output:
21

Editorial:
The most challenging part of the problem is to think of the way how to count each pair of triangles exactly once.
It turns out, that this can be done in a nice and geometrical way.
Each pair of triangles has exactly two inner tangents between them. Moreover, exactly one of them (if we direct tangent from point of the first polygon to the point of the second polygon) leaves the first rectangle on the right side and the other tangent leaves it on the left side.
So let's brute-force the inner tangent. If we continue the tangent to draw the line and count the number of points on the left and on the right of it, say $$$k_1$$$ and $$$k_2$$$ respectively, then we simply need to add $$$\frac{k_1 (k_1 - 1)}{2} \frac{k_2 (k_2 - 1)}{2}$$$ to our answer, since if we select arbitrary pair of vertices on each halfplanes, together with tangent points we will form a pair of triangles with such a tangent.
The question is how to count $$$k_1$$$ and $$$k_2$$$ for each tangent efficiently. If the points would be sorted by direction, perpendicular to the tangent, we could have answered this query with binary search (since points of one halfplane form a prefix of the sorted array and the other — suffix).
However, if we wrote down all the interesting directions, sort them by angle, and make a scanline on it, we could maintain the points in sorted order.
Basically, points $$$a_i$$$ and $$$a_j$$$ change their order in the sorted array at the direction $$$rotate_{90}(a_i - a_j)$$$, and, since there are no three points on one line, these points are neighbours in the sorted array at the moment of the swap.
The complexity is $$$\mathcal{O}(n^2 \log(n))$$$ since we need to sort all $$$n^2$$$ directions and for each direction make a binary search on the sorted array.