Power Transmission (Hard Edition)
Contest: Codeforces Round 558 (Div. 2)
Difficulty rating: 1900
Tags: data structures, geometry, implementation, math

This problem is same as the previous one, but has larger constraints.

It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.

At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $$$(x_i, y_i)$$$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.

Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?

Input format:
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — the number of electric poles.

Each of the following $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^4 \le x_i, y_i \le 10^4$$$) — the coordinates of the poles.

It is guaranteed that all of these $$$n$$$ points are distinct.

Output format:
Print a single integer — the number of pairs of wires that are intersecting.

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

Note:
In the first example:

In the second example:

Note that the three poles $$$(0, 0)$$$, $$$(0, 2)$$$ and $$$(0, 4)$$$ are connected by a single wire.

In the third example:

Example input:
4
0 0
1 1
0 3
1 2

Example output:
14

Example input:
4
0 0
0 2
0 4
2 0

Example output:
6

Editorial:
First, we will divide the problem into several parts: 1) construct the wires, 2) remove duplicates, and 3) count the number of pairs that intersect.
The first part is relatively simple: note that each wire is simply a line that goes through two distinct points (poles) on the $$$Oxy$$$ plane. Suppose this line goes through point $$$A(x_1,y_1)$$$ and point $$$B(x_2,y_2)$$$, then it can be described by the equation $$$ax - by = c$$$ where $$$a = y_1 - y_2$$$, $$$b = x_1 - x_2$$$, $$$c = y_1x_2 - y_2x_1$$$.
For step two, we will simplify the equation for each line by dividing each coefficient by their greatest common divisor. Now each equation uniquely identifies a line, and vice versa - so we can sort the wires by their values of a, b, and c; then remove adjacent duplicates.
The final part of the solution also makes use of this sorted list. As any pair of lines on the plane must intersect unless they are parallel, we only need to count the number of parallel pairs and subtract these from the total number of pairs. These are the pairs with the same slope (i.e. same value of a and b), and they already are next to each other in our list. We now iterate over these "blocks" of parallel lines and count the number of pairs each block contributes — a block of size $$$s$$$ gives $$$\frac{s(s-1)}{2}$$$ pairs.
Complexity: $$$O(n^2\log{n})$$$.