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

MEX Interval Score

Back to problem

Editorial

Call an interval I=[l,r]I=[l,r]I=[l,r] a minimal MEX interval if no smaller nonempty subinterval of III has the same MEX. Every interval of length 111 is minimal.

All CiC_iCi​ are nonnegative. If an endpoint can be removed without changing the MEX, removing it does not increase the cost sum. Repeating this operation produces a minimal MEX interval with the same MEX and a score no smaller than the original score. Therefore, only minimal MEX intervals can be optimal.

For a minimal interval [l,r][l,r][l,r] of length at least 222, we have Al≠ArA_l\ne A_rAl​=.

First suppose Al<ArA_l<A_rAl​<Ar​. Removing the right endpoint must change the MEX, so

mex⁡(Al,⋯ ,Ar)>Ar.\operatorname{mex}(A_l,\cdots,A_r)>A_r.mex(Al​,⋯,Ar​)>A

For a fixed right endpoint rrr, there is a unique largest lll satisfying this condition. Let last⁡r(x)\operatorname{last}_r(x)lastr​(x) be the last occurrence of xxx up to position . Then

l=min⁡0≤x≤Arlast⁡r(x).l=\min_{0\le x\le A_r}\operatorname{last}_r(x).l=0≤x≤Ar​min​last

All required values must exist, and the previous occurrence of ArA_rAr​ must be before lll so that removing position rrr changes the MEX.

The case Al>ArA_l>A_rAl​>Ar​ is symmetric. Let next⁡l(x)\operatorname{next}_l(x)next be the first occurrence of at or after . Then the candidate endpoint is

r=max⁡0≤x≤Alnext⁡l(x),r=\max_{0\le x\le A_l}\operatorname{next}_l(x),r=0≤x≤Al​max​next

and the next occurrence of AlA_lAl​ must be after rrr.

The first case contributes at most one candidate for each rrr, and the second case contributes at most one candidate for each lll. Together with all singleton intervals, there are only O(N)O(N)O(N) candidates.

During a left-to-right scan, maintain the last occurrence of every value 0,1,⋯ ,N0,1,\cdots,N0,1,⋯,N in a segment tree. We need the minimum over a value prefix and the smallest value whose stored position is less than a given lll; the latter is the interval MEX. A symmetric right-to-left scan maintains next occurrences. All minimal intervals are found in O(Nlog⁡N)O(N\log N)O(NlogN) time.

For every candidate [s,e][s,e][s,e], compute

w(s,e)=Bmex⁡(As,⋯ ,Ae)−(Pe−Ps−1),w(s,e)=B_{\operatorname{mex}(A_s,\cdots,A_e)}-(P_e-P_{s-1}),w(s,e)=Bmex(As

where PPP is the prefix sum of CCC.

Sort queries by their right endpoint. Sort candidates by eee and add every candidate with eee not exceeding the current query endpoint. A segment tree indexed by sss stores the maximum weight added at each start position. The answer to query [l,r][l,r][l,r] is the maximum over start positions [l,N][l,N][l. Every added candidate already satisfies , so this is exactly the set of candidates contained in the query interval.

The total time complexity is O((N+Q)log⁡N)O((N+Q)\log N)O((N+Q)logN) and the memory complexity is O(N+Q)O(N+Q)O(N+Q).

Ar​
r​
.
rr
r
r
​
(
x
)
.
l
​
(
x
)
xxx
lll
l
​
(
x
)
,
​
,
⋯
,
Ae​
)
​
−
(Pe​−
Ps−1​),
,
N
]
e≤re\le re≤r