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

mex slop

Back to problem

Editorial

Write F(X)F(X)F(X) for the result of applying one operation to a sequence XXX, and let B=F(A)B=F(A)B=F(A) and C=F(B)C=F(B)C=F(B).

As we move right, the set of preceding elements only grows, so BBB is nondecreasing. Applying the operation to a nondecreasing sequence gives C1=0C_1=0C1​=0 and makes every difference between adjacent elements either 000 or 111.

A prefix ending at Ci−1C_{i-1}Ci−1​ therefore contains every integer from 000 through Ci−1C_{i-1}Ci−1​. Consequently,

F(C)1=0,F(C)i=Ci−1+1(2≤i≤N).F(C)_1=0,\qquad F(C)_i=C_{i-1}+1\quad(2\leq i\leq N).F(C)1​=0,F(C)i

The result has the same property, so every subsequent operation follows the same rule. Equivalently, prepend 000, increase each retained old element by 111, and discard the last old element.

We only need to precompute BBB and CCC to answer a query.

  • If K=1K=1K=1, the answer is BPB_PBP​.
  • If K≥2K\geq2K≥2, let d=K−2d=K-2. If , the answer is . Otherwise, it is .

To compute BBB, scan the original sequence from left to right, recording which values have appeared and maintaining the current mex⁡\operatorname{mex}mex. Store that value in BiB_iBi​ before inserting AiA_iA. Values at least do not need to be recorded.

Compute CCC with another left-to-right scan. If the current mex⁡\operatorname{mex}mex is xxx, first set Ci=xC_i=xCi​=x, then increase by only when .

Preprocessing takes O(N)O(N)O(N) time, and each query takes O(1)O(1)O(1) time. The total time complexity is O(N+Q)O(N+Q)O(N+Q). Processing queries as they are read gives an O(N)O(N) space complexity.

Proof

Adding an element to a set cannot remove any value already present, so its mex⁡\operatorname{mex}mex cannot decrease. Thus, B=F(A)B=F(A)B=F(A) is nondecreasing for every original sequence AAA.

Let xxx be the mex of the first elements of a nondecreasing sequence . If , adding leaves absent, so the does not change. If , all preceding elements are at most , so has not appeared. After adding , the new is exactly . Therefore, and every difference is either or .

Solution written by GPT6

​
=
Ci−1​+
1(2≤
i≤
N).
d=K−2
P≤d+1P\leq d+1P≤d+1
P−1P-1P−1
CP−d+dC_{P-d}+dCP−d​+d
i
​
NNN
xx
x
111
Bi=xB_i=xBi​=x
O
(
N
)
⁡\operatorname{mex}
mex
i−1i-1i−1
BBB
Bi≠xB_i\neq xBi​=x
BiB_iBi​
xxx
mex⁡\operatorname{mex}mex
Bi=xB_i=xBi​=x
BiB_iBi​
x+1x+1x+1
xxx
mex⁡\operatorname{mex}mex
x+1x+1x+1
C1=0C_1=0C1​=0
Ci+1−CiC_{i+1}-C_iCi+1​−Ci​
000
111

Any prefix of a nondecreasing sequence that starts at 000 and increases by at most 111 contains all integers from 000 through its last value, and no larger values. Its mex⁡\operatorname{mex}mex is therefore exactly one greater than its last value. This proves F(C)1=0F(C)_1=0F(C)1​=0 and F(C)i=Ci−1+1F(C)_i=C_{i-1}+1F(C)i​=Ci−1​+1.

The first two values of the transformed sequence, when both exist, are 0,10,10,1, and its remaining adjacent differences equal the corresponding differences in the original sequence. The same property is therefore preserved. After applying this rule ddd times, the first min⁡(d,N)\min(d,N)min(d,N) positions have values 0,1,…,min⁡(d,N)−10,1,\ldots,\min(d,N)-10,1,…,min(d,N)−1, while a later position PPP contains CP−d+dC_{P-d}+dCP−d​+d. At P=d+1P=d+1P=d+1, this is also P−1P-1P−1 because C1=0C_1=0C1​=0. If more operations are applied than there are positions, the already established value P−1P-1P−1 at each position remains unchanged. Hence, the query formula holds for every valid K,PK,PK,P.

While computing BBB with a presence array, the mex⁡\operatorname{mex}mex never decreases and never exceeds NNN. The total number of increments is therefore O(N)O(N)O(N). Computing CCC also requires only one scan, so preprocessing takes O(N)O(N)O(N) time.