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

XOR Merge

Back to problem

Editorial

Let P0=0P_0=0P0​=0 and Pi=A1⊕A2⊕⋯⊕AiP_i=A_1\oplus A_2\oplus\cdots\oplus A_iPi​=A1​⊕A2​⊕⋯⊕Ai​. Let S=PNS=P_NS=PN​ be the XOR of the whole sequence.

A split operation is exactly the insertion of one arbitrary internal value into PPP while keeping both endpoints fixed. If AiA_iAi​ is split into x,yx,yx,y, the newly inserted prefix value is Pi−1⊕xP_{i-1}\oplus xP. Conversely, a merge operation deletes exactly one internal prefix value .

If the final sequence has length mmm and every element is kkk, its prefix XOR sequence is

0,k,0,k,⋯ .0,k,0,k,\cdots.0,k,0,k,⋯.

The total XOR SSS is invariant under both operations.

Case S≠0S\ne 0S=0

Then mmm must be odd and k=Sk=Sk=S. The target prefix sequence is 0,S,0,S,⋯ ,S0,S,0,S,\cdots,S0,S,0,S,⋯,S.

Filter the original PPP so that only values equal to 000 or SSS remain. Let CCC be the number of maximal runs of equal values in this filtered sequence. Keeping one value from every run gives exactly an alternating sequence 0,S,0,S,⋯ ,S0,S,0,S,\cdots,S0,S,0. Every other prefix value must be deleted, so the answer is

N+1−C.N+1-C.N+1−C.

Case S=0S=0S=0

Let MMM be the number of nonzero values among the internal prefixes P1,⋯ ,PN−1P_1,\cdots,P_{N-1}P1​,⋯,PN−1​. Let be the number of gaps between consecutive zero values in the full prefix sequence.

For k=0k=0k=0, deleting every nonzero internal prefix is sufficient, so the cost is MMM.

Now fix k≠0k\ne 0k=0. Keeping every zero prefix never makes an optimum worse. Each gap between two consecutive zeros needs exactly one kkk in the target. Let HkH_kHk​ be the number of gaps that already contain at least one prefix equal to . In such a gap we can keep one occurrence of and save one deletion. In every other gap we must insert one . Therefore the cost is

M−Hk+(G−Hk)=M+G−2Hk.M-H_k+(G-H_k)=M+G-2H_k.M−Hk​+(G−Hk​

If H=max⁡HkH=\max H_kH=maxHk​, the answer is

min⁡(M, M+G−2H).\min(M,\ M+G-2H).min(M, M+G−2H).

We can compute HHH in one scan. Increase the gap index whenever the current prefix is zero. For every nonzero value vvv, store in a hash table the last gap in which vvv was counted and the number of distinct gaps containing vvv. This makes repeated occurrences of vvv inside the same gap count only once.

The expected time complexity is O(N)O(N)O(N) and the memory complexity is O(N)O(N)O(N).

Solution written by GPT5.6

i−1​
⊕
x
PiP_iPi​
,
S
,
⋯
,
S
GG
G
kk
k
kkk
kkk
)
=
M+
G−
2Hk​.