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-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

Thread Cutting

Back to problem

Editorial

For distinct x,a,bx,a,bx,a,b, define

mid⁡(x;a,b)=[min⁡(a,b)<x<max⁡(a,b)].\operatorname{mid}(x;a,b)=[\min(a,b)<x<\max(a,b)].mid(x;a,b)=[min(a,b)<x<max(a,b)].

For four distinct nails a,b,c,da,b,c,da,b,c,d, chords ababab and cdcdcd cross exactly when

mid⁡(c;a,b)XORmid⁡(d;a,b)=1.\operatorname{mid}(c;a,b)\mathbin{\mathrm{XOR}}\operatorname{mid}(d;a,b)=1.mid(c;a,b)XORmid(d;a,b)=1.

Fix Ei=PiPi+1E_i=P_iP_{i+1}Ei​=Pi​Pi+1​. XORing its crossing indicators with cancels every intermediate endpoint and leaves

mid⁡(Pi+2;Pi,Pi+1)XORmid⁡(PR;Pi,Pi+1).\operatorname{mid}(P_{i+2};P_i,P_{i+1})\mathbin{\mathrm{XOR}}\operatorname{mid}(P_R;P_i,P_{i+1}).mid(Pi+2​;P

Let Bi=mid⁡(Pi+2;Pi,Pi+1)B_i=\operatorname{mid}(P_{i+2};P_i,P_{i+1})Bi​=mid(Pi+2​;P and build its prefix XOR array . Since

mid⁡(x;a,b)=[a<x]XOR[b<x],\operatorname{mid}(x;a,b)=[a<x]\mathbin{\mathrm{XOR}}[b<x],mid(x;a,b)=[a<x]XOR[b<x],

XORing the terms involving PRP_RPR​ for i=L,⋯ ,R−3i=L,\cdots,R-3i=L,⋯,R−3 cancels all internal comparisons and leaves

mid⁡(PR;PL,PR−2).\operatorname{mid}(P_R;P_L,P_{R-2}).mid(PR​;PL​,PR

The answer is 000 when R−L+1≤3R-L+1\leq3R−L+1≤3. Otherwise it is

SR−3XORSL−1XORmid⁡(PR;PL,PR−2).S_{R-3}\mathbin{\mathrm{XOR}}S_{L-1}\mathbin{\mathrm{XOR}}\operatorname{mid}(P_R;P_L,P_{R-2}).SR−3​XORS

Preprocessing takes O(N)O(N)O(N) time, and each query takes O(1)O(1)O(1) time. The total complexity is O(N+Q)O(N+Q)O(N+Q).

Solution written by GPT5

Ei+2,Ei+3,⋯ ,ER−1E_{i+2},E_{i+3},\cdots,E_{R-1}
Ei+2​,Ei+3​,⋯,ER−1​
i
​
,
Pi+1​
)
XOR
mid(PR​;Pi​,Pi+1​).
i
​
,
Pi+1​
)
SSS
−
2
​
)
.
L−1
​
XOR
mid(PR​;PL​,PR−2​).