0
$\begingroup$

N red and M blue points on the plane in such a way that no three points lie on the same line.what is the number of distinct triangles with vertices in red points which do not contain any blue point inside.

1 Answers 1

0

We will solve the problem using the following algorithm: Fix some red point. Find the number of triangles with vertices in the red points which don't contain any blue points inside and which have the fixed red point as one of the vertices. Remove the fixed red point and go back to statement 1, if there remain any red point. The first and third statements are obvious, so the main part of the solution is statement 2.

Suppose the red point A is fixed (in the first statement). Also suppose we have all other points which are still not removed (blue and red together) sorted by angle around the point A. We will iterate over all red points B, which will be the second vertice of triangle. Now, we need to find the number of triangles with vertices in red points which have points A and B as two vertices and which don't contain any blue point inside.

To solve this problem we will iterate over all unremoved points C in the increasing order of angle ABC starting from the point after the point B (in the same order). To avoid double counting we will stop when the angle between vectors AB and AC become greater than 180 degrees or when we reach the point which was already considered. Then we will perform such actions: If C is red then we will check whether there are blue points inside triangle ABC and if not - we will increase the answer by 1. Note, that to perform this check we don't need to iterate over all blue points. It is sufficient to maintain such point D from the ones which we have already seen for which the angle ABD is the smallest possible. If D doesn't lies inside the triangle ABC, then there is no blue point which lies inside it. If C is blue, then we will compare the angle ABC with ABD and if ABC is smaller, we will replace old D with C. Note, that after choosing new B we consider that there is no point D and there will be no blue points inside triangles ABC until we reach the first blue point.

The complexity is O(N2(N+M)).