 # TRICHEF - Editorial

Author: Utkarsh Saxena
Testers: Misha Chorniy
Editorialist: Praveen Dhinwa

medium

### PREREQUISITES:

prefix sums, data structures

### PROBLEM:

You are given n distinct points in 2-D plane. Their x coordinates can be either 0, 1 or 2.
Find the sum of areas of the triangles made by all the \binom{n}{3} triplets of points. The value of n is around 10^3.

### SOLUTION

Note that the x coordinates are only 0, 1 or 2. Suppose the three points have same x, then the area will be zero. So, the triangles with non-zero areas should be made of points which have at least two points with different x coordinates.

The formula for finding area of triangle with coordinates of points (x1, y1), (x2, y2), (x3, y3) will be

| \, \frac{1}{2} *\, ((x_1 y_2 - x_2 y_1) + (x_2 y_3 - x_3 y_2) + (x_3 y_1 - x_1 y_3)) \, |
= | \, \frac{1}{2} *\, ((x_1 y_2 + x_2 y_3 + x_3 y_1 ) - (y_1 x_2 + y_2 x_3 + y_3 x_1 )) \, |

Now there can be two cases.

Case 1: Two vertices of the triangle have the same x-coordinate.
Wlog assume the vertices 1 and 2 have same x coordinates, i.e. x_1 = x_2.

Area reduces to

| \, \frac{1}{2} *\, ((x_1 y_2 - x_1 y_1) + (x_1 y_3 - x_3 y_2) + (x_3 y_1 - x_1 y_3)) \, |
= | \, \frac{1}{2} *\, (x_1 (y_2 - y_1) - x_3 (y_2 - y_1)) \, |
= |\, \frac{1}{2} (x_1 - x_3) (y_2 - y_1) \, |

Note that the coordinate y_3 doesn’t matter for the area.

We can pick all possible pairs of (x_1, y_1) and (x_1, y_2). Note that value of y_2 - y_1 will be fixed after this.

Area formula becomes

= \frac{1}{2} \, |y_2 - y_1| * |\, (x_1 - x_3) \, |

The term \frac{1}{2} \, |y_2 - y_1| can be taken common out of this formula. Remaining term just becomes sum of values of |x_1 - x_3. We can find this by finding sum of x coordinates of points with their x coordinates < x_1 and sum of x coordinates of points with their x coordinates > x. Both of these can be computed by maintaining using prefix sums over x coordinates of the points.

This takes \mathcal{O}(N^2) time.

Case 2: All the three vertices of the triangle have different x-coordinate.

Wlog assume x_1 = 0, x_2 = 1 and x_3 = 3.

The formula for the area reduces to

|\, \frac{1}{2} (y_1 + y_3 - 2 y_2) \, |

We can the absolute sign by considering the two cases.

• y_1 + y_3 \geq 2 y_2:
Fix y_1 and y_3 and find the sum of all valid y_2 satisfying this inequality. This can be done efficiently by pre-computing partial sums.
• y_1 + y_3 < 2 y_2: This case can also be dealt similar to the case above.

The overall complexity of this program will be \mathcal{O}(n^2).

Reference slides describing this approach be found here.

### Setters and Tester Solution Codes

1 Like

Thanks for this @admin Great Slides and the tutorials are also quite good Can anyone help facing problem to convert the above approach into code specially the case 2. Thanks in advance @vijju123

X can be only 1,2,3 not 0,1,2.

@admin Isn’t the complexity of setter’s solution is O(n^3) because innermost while loop can have O(n) complexity in the worst case(Though I understand the input is small but just making sure for the general case).

	for(int i = 0; i < a.size(); i++) {
int start = 0;
for(int j = 0; j < a.size(); j++) {
ll tot = a[i] + a[j];
while(start < cnt && 2*a[start] < tot) start++;
ll contri = ((tot * start) - (2LL * pre[start])) + ((2LL * (pre[cnt] - pre[start])) - (tot * (cnt - start)));
ans += contri;
}
}


It can be improved to O(n^2(log n)) by using simple binary search.

man these editorials are really hard to comprehend… Case 2 went straight over my head