DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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 account linking.

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

Yet Another Angular Sort Problem

Back to problem

Editorial

The angular order of AiA_iAi​ and AjA_jAj​ swaps when the line through them intersects ℓ\ellℓ. If the two lines are not parallel, this happens exactly once and swaps the two indices in the permutation, contributing 2∣i−j∣2|i-j|2∣i−j∣. If their line is parallel to ℓ\ellℓ, no such change occurs.

The sum of 2∣i−j∣2|i-j|2∣i−j∣ over all pairs is

N(N−1)(N+1)3.\frac{N(N-1)(N+1)}{3}.3N(N−1)(N+1)​.

For Ai=(xi,yi)A_i=(x_i,y_i)Ai​=(xi​,yi​), set . The line is parallel to exactly when . Sort points by , compute the sum of inside every equal-value group, and subtract it from the total. The time complexity is .

vi=axi+byiv_i=ax_i+by_i
vi​=axi​+byi​
AiAjA_iA_jAi​Aj​
ℓ\ellℓ
vi=vjv_i=v_jvi​=vj​
viv_ivi​
2∣i−j∣2|i-j|2∣i−j∣
O(Nlog⁡N)O(N\log N)O(NlogN)