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

MEXOR

Back to problem

Editorial

Let P(l,r)P(l,r)P(l,r) denote the parity of the number of zeros in Al,…,ArA_l,\ldots,A_rAl​,…,Ar​. Splitting every element into a singleton yields the XOR value P(1,N)P(1,N)P(1,N).

If we replace the singleton segments inside an interval [l,r][l,r][l,r] with one segment, the XOR changes by MEX(l,r)⊕P(l,r)\mathrm{MEX}(l,r)\oplus P(l,r)MEX(l,r)⊕P(l,r). Thus, we may select disjoint intervals and XOR their changes into the baseline.

An interval with MEX 000 has zero change. If the MEX is 111, the interval contains no 111. An odd number of zeros also yields zero change. If it contains an even number of zeros, we may replace it with the interval between a pair of consecutive zero occurrences having no 111 between them. This interval has change 111.

Now consider an interval whose MEX is m≥2m\ge2m≥2. Inside it, select a shortest subinterval [u,v][u,v][u,v] containing all values 0,1,…,m−10,1,\ldots,m-10,1,…,m−1. Each endpoint must occur exactly once inside this subinterval and must have value less than . Call such intervals . Enumerate them by maintaining frequencies and MEX for every left endpoint.

If the removed part contains an even number of zeros, the essential interval produces the same change. Otherwise, extend it to the closest zero immediately to its left or right, choosing a side contained in the original interval. This adds exactly one zero. The MEX remains mmm because the original interval contains no mmm. Hence, it suffices to consider every essential interval and its one-sided extensions to the nearest zero, provided the MEX does not change. The number of essential intervals, and therefore the number of candidates, is O(N)O(N)O(N).

Let dp[i]dp[i]dp[i] be the set of XOR changes obtainable from disjoint candidate intervals contained in the prefix [1,i][1,i][1,i]. We may skip position iii, or select any candidate [l,i][l,i][l,i] and XOR its change with every state of .

Let GGG be the MEX of the entire array. Every segment MEX is at most GGG, so all possible XOR values are below 2⌈log⁡2(G+1)⌉2^{\lceil\log_2(G+1)\rceil}2⌈log2​(G+. Representing each DP row as a bitset yields time and space. The answer is the maximum of over all .

mmm
essential intervals
dp[l−1]dp[l-1]
dp[l−1]
1
)⌉
O(N2)O(N^2)O(N2)
O(N2)O(N^2)O(N2)
x⊕P(1,N)x\oplus P(1,N)x⊕P(1,N)
x∈dp[N]x\in dp[N]x∈dp[N]
Solution written by GPT6