Off by One
Contest: Educational Codeforces Round 108 (Rated for Div. 2)
Difficulty rating: 2700
Tags: constructive algorithms, dfs and similar, geometry, graphs, sortings, trees

There are $$$n$$$ points on an infinite plane. The $$$i$$$-th point has coordinates $$$(x_i, y_i)$$$ such that $$$x_i > 0$$$ and $$$y_i > 0$$$. The coordinates are not necessarily integer.

In one move you perform the following operations:

- choose two points $$$a$$$ and $$$b$$$ ($$$a \neq b$$$);
- move point $$$a$$$ from $$$(x_a, y_a)$$$ to either $$$(x_a + 1, y_a)$$$ or $$$(x_a, y_a + 1)$$$;
- move point $$$b$$$ from $$$(x_b, y_b)$$$ to either $$$(x_b + 1, y_b)$$$ or $$$(x_b, y_b + 1)$$$;
- remove points $$$a$$$ and $$$b$$$.

However, the move can only be performed if there exists a line that passes through the new coordinates of $$$a$$$, new coordinates of $$$b$$$ and $$$(0, 0)$$$.

Otherwise, the move can't be performed and the points stay at their original coordinates $$$(x_a, y_a)$$$ and $$$(x_b, y_b)$$$, respectively.

The numeration of points does not change after some points are removed. Once the points are removed, they can't be chosen in any later moves. Note that you have to move both points during the move, you can't leave them at their original coordinates.

What is the maximum number of moves you can perform? What are these moves?

If there are multiple answers, you can print any of them.

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of points.

The $$$i$$$-th of the next $$$n$$$ lines contains four integers $$$a_i, b_i, c_i, d_i$$$ ($$$1 \le a_i, b_i, c_i, d_i \le 10^9$$$). The coordinates of the $$$i$$$-th point are $$$x_i = \frac{a_i}{b_i}$$$ and $$$y_i = \frac{c_i}{d_i}$$$.

Output format:
In the first line print a single integer $$$c$$$ — the maximum number of moves you can perform.

Each of the next $$$c$$$ lines should contain a description of a move: two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le n$$$, $$$a \neq b$$$) — the points that are removed during the current move. There should be a way to move points $$$a$$$ and $$$b$$$ according to the statement so that there's a line that passes through the new coordinates of $$$a$$$, the new coordinates of $$$b$$$ and $$$(0, 0)$$$. No removed point can be chosen in a later move.

If there are multiple answers, you can print any of them. You can print the moves and the points in the move in the arbitrary order.

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

Note:
Here are the points and the moves for the ones that get chosen for the moves from the first example:

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

Example output:
3
1 6
2 4
5 7

Example input:
4
2 1 1 1
1 1 2 1
2 1 1 2
1 2 1 2

Example output:
1
1 2

Editorial:
At first the problem sounds like some sort of matching. However, it seems like you first want to match each point with either of its moves and then some pairs of points to each other. That doesn't sound viable but since the matchings are often connected with graphs, the graph idea might come handy.
Let's first consider a pair of matched points. What does it actually mean that there exists a line through their new coordinates and $$$(0, 0)$$$? It's the same as: the angles of a line through the new coordinates of $$$a$$$ and $$$(0, 0)$$$ and a line through the new coordinates of $$$b$$$ and $$$(0, 0)$$$ are the same. Angles are the same means that their tangents are the same (and vice versa since we only consider the first quadrant of the plane). So we can conclude that $$$\frac{y}{x+1}$$$ or $$$\frac{y+1}{x}$$$ of the first point should be equal to any of these of the second point.
Now consider the following graph. Various values of tangents of the lines are the nodes. Each point produces an edge between their $$$\frac{y}{x+1}$$$ and $$$\frac{y+1}{x}$$$. What are the matched pairs of points in this graph? It's such a pair of edges that they share at least one endpoint.
Building a graph is the slowest part of the solution since you have to use some data structure (or at least a sort and a binary search). $$$O(n)$$$ is possible with some sort of hashmap but $$$O(n \log n)$$$ should be perfectly fine as well.
So we reduced the problem to a more well-known one: given an arbitrary undirected graph, find the maximum number of pairs of edges such that each pair shares at least one endpoint and each edge is included in no more than one pair.
The upper bound on the answer is the following. Let $$$m_i$$$ be the number of edges in the $$$i$$$-th connected component. Best case we can make $$$\lfloor \frac{m_i}{2} \rfloor$$$ pairs from it. Let's come up with an algorithm to achieve this bound.
Consider a dfs tree of a component. It's known that a dfs tree of an undirected graph contains no cross edges. So if we direct all the edges of a dfs tree downwards (convert all back edges to forward edges), each edge will connect some vertex to its descendant.
Imagine we came up with a dfs such that $$$dfs(v)$$$ matches all the edges that have their upper node in the subtree of $$$v$$$ to each other (except one edge in case there is an odd number of them). $$$dfs(root)$$$ will solve the task exactly then.
How should that dfs work exactly?
What if there were no forward edges at all? That case is easy since all edges are tree edges. We'll try to maintain an invariant that the only unmatched edge is an edge that has $$$v$$$ as one of its endpoints. If $$$v$$$ is a leaf, then there's nothing to match. Otherwise, we go into some child $$$u$$$. If it can't match all its edges, then match its remaining edge to an edge $$$(v, u)$$$. If it can then remember that we have an edge $$$(v, u)$$$ unmatched so far. Go into another child $$$w$$$. Same, match our edge with its edge if it has one unmatched. However, if $$$(v, w)$$$ turned out to get unmatched and $$$(v, u)$$$ turned out to be unmatched, then you can match them to each other. This way you will be left with at most one unmatched edge after you process all the children, and that edge has its endpoint at $$$v$$$.
Add the forward edges back. Did anything change? Look at the forward edge that has its upper vertex the lowest. We can see that it points to a vertex $$$u$$$ that has its subtree fully matches. So why don't we treat this forward edge the same as an edge to a leaf? Forget that $$$u$$$ has some subtree of its own and just believe that you can't match the edge $$$(v, u)$$$ so far. Proceed the same as the easy case.
Since we mark exactly which edges you pair up with which, it's trivial to retrieve the answer.
Overall complexity: $$$O(n)/O(n \log n)$$$.