DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

A Segment and Triangles

Back to problem

Editorial

Let n+n_+n+​ and n−n_-n−​ be the numbers of points on the two sides of the line PQPQPQ. If all three vertices of a triangle lie on the same side, the triangle is disjoint from PQPQPQ and has Type 4. There are (n+3)+(n−3)\binom{n_+}{3}+\binom{n_-}{3}(3n+​​)+( such triangles.

Now consider a triangle with vertices on both sides. Let UUU be its only vertex on one side, and let V,WV,WV,W be the two vertices on the other side. Classify the position where the line UVUVUV intersects the line PQPQPQ as follows.

  • LLL: the intersection is before PPP.
  • MMM: the intersection is in the interior of the segment PQPQPQ.
  • RRR: the intersection is after QQQ.

Classify UWUWUW in the same way. These two intersections are the endpoints of the interval where the triangle meets the line PQPQPQ. Consequently, the pair of labels determines the type of the triangle.

  • L,RL,RL,R: Type 1.
  • L,ML,ML,M or M,RM,RM,R: Type 2.
  • M,MM,MM,M: Type 3.
  • or : Type 4.

For a fixed UUU, let lU,mU,rUl_U,m_U,r_UlU​,mU​,r be the numbers of points on the opposite side with labels , respectively. The triangles for which is the unique vertex on its side contribute the following amounts to the four types:

lUrU,mU(lU+rU),(mU2),(lU2)+(rU2).l_Ur_U,\qquad m_U(l_U+r_U),\qquad \binom{m_U}{2},\qquad \binom{l_U}{2}+\binom{r_U}{2}.lU​rU​

Every triangle having vertices on both sides has a unique vertex on the minority side. Therefore, summing these values over all UUU counts every such triangle exactly once.

It remains to compute lU,mU,rUl_U,m_U,r_UlU​,mU​,rU​ efficiently. Apply an affine change of coordinates and regard and . For a point , define

aX=xXyX,bX=xX−1yX.a_X=\frac{x_X}{y_X},\qquad b_X=\frac{x_X-1}{y_X}.aX​=yX​

The general-position condition guarantees that every yXy_XyX​ is nonzero and that no ties occur in either ordering.

If UUU is above the line PQPQPQ, then

lU=#{V:aV>aU},rU=#{V:bV<bU},l_U=\#\{V:a_V>a_U\},\qquad r_U=\#\{V:b_V<b_U\},lU​=#{V:aV

where VVV ranges only over points on the side opposite to UUU. If UUU is below the line, both inequalities are reversed. Thus, sorting all points by aXa_XaX​ and scanning prefix counts for the two sides gives every lUl_U. A second sort by gives every . Finally,

mU=(number of points opposite to U)−lU−rU.m_U=(\text{number of points opposite to }U)-l_U-r_U.mU​=(number of points opposite to U)−lU​−

No division or floating-point arithmetic is needed. Let d⃗=Q−P\vec d=Q-Pd=Q−P. For a base point BBB equal to PP or , sorting the following fraction gives the order of or , respectively:

d⃗⋅(X−B)d⃗×(X−P).\frac{\vec d\cdot(X-B)}{\vec d\times(X-P)}.d×(X−P)

If the denominator is negative, negate both the numerator and denominator. Compare two fractions by cross multiplication. Signed 128-bit integers are required for these products under the coordinate bounds.

The two sorts take O(Nlog⁡N)O(N\log N)O(NlogN) time, and the algorithm uses O(N)O(N)O(N) memory.

Solution written by GPT5

3
n−​
​
)
L,LL,L
L,L
R,RR,RR,R
U
​
L,M,RL,M,RL,M,R
UUU
,
mU​
(
lU​
+
rU​),(2mU​​),(2lU​​)+
(2rU​​).
P=(0,0)P=(0,0)
P=(0,0)
Q=(1,0)Q=(1,0)Q=(1,0)
X=(xX,yX)X=(x_X,y_X)X=(xX​,yX​)
xX​
​
,
bX​
=
yX​xX​−1​.
​
>
aU​},rU​=
#{V:
bV​<
bU​},
lU​
bXb_XbX​
rUr_UrU​
rU​.
P
QQQ
aXa_XaX​
bXb_XbX​
d⋅(X−B)
​
.