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

Binary Sequence and Queries

Back to problem

Editorial

For convenience, use indices from 000 to N−1N-1N−1, with every index interpreted modulo NNN.

Applying the operation twice at the same starting position cancels out. Therefore it is enough to let xi∈{0,1}x_i\in\{0,1\}xi​∈{0,1} denote whether the operation starting at iii is used an odd number of times.

For position iii to become 0, we need

ai=⨁j=i−K+1ixj.a_i=\bigoplus_{j=i-K+1}^{i}x_j.ai​=j=i−K+1⨁i​

XORing the equations for two adjacent positions gives

xi⊕xi−K=Δi,Δi=ai⊕ai−1.x_i\oplus x_{i-K}=\Delta_i,\qquad \Delta_i=a_i\oplus a_{i-1}.xi​⊕xi−K​=

Let d=gcd⁡(N,K)d=\gcd(N,K)d=gcd(N,K), m=N/dm=N/dm=N/d, and h=K/dh=K/dh=K. The permutation splits the indices into exactly cycles, each of length .

For a residue class rrr, define

Pr=⨁i≡r(modd)ai.P_r=\bigoplus_{i\equiv r\pmod d}a_i.Pr​=i≡r(modd)⨁

The XOR of all difference equations in one cycle is Pr⊕Pr−1P_r\oplus P_{r-1}Pr​⊕Pr−1​. Thus the difference equations are consistent if and only if

P0=P1=⋯=Pd−1.P_0=P_1=\cdots=P_{d-1}.P0​=P1​=⋯=P

Write one cycle in the order

vt=r+tK(modN),t=0,…,m−1.v_t=r+tK\pmod N,\qquad t=0,\ldots,m-1.vt​=r+tK(modN),

Choose the canonical solution

xv0=0,xvt=xvt−1⊕Δvt(t≥1).x_{v_0}=0,\qquad x_{v_t}=x_{v_{t-1}}\oplus\Delta_{v_t}\quad(t\ge1).xv0​​=0,x

Let crc_rcr​ be the number of ones among the xxx values in cycle rrr.

Considering only the difference equations, every cycle has one free variable yry_ryr​ that flips the entire cycle. The cost is crc_rcr​ for yr=0y_r=0y and for . Hence the minimum cost for the difference equations alone is

B=∑rmin⁡(cr,m−cr).B=\sum_r\min(c_r,m-c_r).B=r∑​min(cr​,m−

The difference equations used only differences of the original equations, so one original equation must still be enforced. For position 000, define the residual

E=a0⊕⨁j=−K+10xj.E=a_0\oplus\bigoplus_{j=-K+1}^{0}x_j.E=a0​⊕j=−K+1⨁

We need E=0E=0E=0.

A length-KKK window contains exactly h=K/dh=K/dh=K/d indices from every residue class.

If hhh is even, flipping an entire cycle does not change EEE. Therefore E=1E=1E=1 is impossible, while for E=0E=0E=0 the answer is BBB.

If hhh is odd, each yry_ryr​ toggles EEE once, so

y0⊕y1⊕⋯⊕yd−1=E.y_0\oplus y_1\oplus\cdots\oplus y_{d-1}=E.y0​⊕y1​⊕⋯⊕y

Let the cheaper choice for each cycle be

zr=[cr>m/2],Z=⨁rzr.z_r=[c_r>m/2],\qquad Z=\bigoplus_r z_r.zr​=[cr​>m/2],

If Z=EZ=EZ=E, the answer is BBB. Otherwise one cycle must take the opposite choice, at an extra cost of

min⁡r∣m−2cr∣.\min_r|m-2c_r|.rmin​∣m−2cr​∣.

Now consider a query. Flipping apa_pap​ toggles only Δp\Delta_pΔp​ and Δp+1\Delta_{p+1}Δ, and it toggles only . Thus the condition that all are equal can be checked in by maintaining the number of classes with .

Inside a cycle, toggling Δvs\Delta_{v_s}Δvs​​ with s≥1s\ge1s≥1 flips the suffix

xvs,xvs+1,…,xvm−1x_{v_s},x_{v_{s+1}},\ldots,x_{v_{m-1}}xvs​​,xv

of the canonical solution. When s=0s=0s=0, Δv0\Delta_{v_0}Δv0​​ is used only by the cycle-closing consistency equation, so the canonical values themselves do not change.

Concatenate all cycles in cycle order into one array of length NNN. A lazy segment tree supports range flips and range-one-count queries. Every query needs at most two suffix flips and recomputation of crc_rcr​ for at most two affected cycles.

Whenever a crc_rcr​ changes, maintain the following values.

  • B=∑rmin⁡(cr,m−cr)B=\sum_r\min(c_r,m-c_r)B=∑r​min(cr​,m−c

The residual EEE can also be updated instead of recomputed. Precompute impact[u]impact[u]impact[u], the parity of the number of positions in the affected canonical suffix for Δu\Delta_uΔu​ that lie in the length-KK window of . For a cycle root, . A query at updates

E⊕=[p=0]⊕impact[p]⊕impact[p+1].E\mathrel{\oplus}=[p=0]\oplus impact[p]\oplus impact[p+1].E⊕=[p=0]⊕impact[p]⊕impac

Therefore the answer to each query is:

  • -1 if the PrP_rPr​ values are not all equal;
  • if hhh is even, -1 when E=1E=1E=1, otherwise BBB;
  • if is odd, .

Preprocessing each test case takes O(Nlog⁡N)O(N\log N)O(NlogN) time, and each query takes O(log⁡N)O(\log N)O(logN) time. The total complexity is O((N+Q)log⁡N)O((N+Q)\log N)O((N+ with memory.

Solution written by GPT5.6

xj​
.
Δi​
,
Δi​
=
ai​⊕
ai−1​.
/
d
i↦i+Ki\mapsto i+Ki↦i+K
ddd
mmm
​
ai​
.
d−1
​
.
t
=
0,…,m−
1.
vt​​
=
xvt−1​​⊕
Δvt​​(t≥
1).
r
​
=
0
m−crm-c_rm−cr​
yr=1y_r=1yr​=1
c
r​
)
.
0
​
xj​
.
d−1​
=
E.
Z
=
r⨁​zr​.
p+1
​
Pp mod dP_{p\bmod d}Ppmodd​
PrP_rPr​
O(1)O(1)O(1)
Pr=1P_r=1Pr​=1
s+1
​
​
,
…
,
xvm−1​​
r​
)
  • Z=⨁r[cr>m/2]Z=\bigoplus_r[c_r>m/2]Z=⨁r​[cr​>m/2]
  • a multiset of all ∣m−2cr∣|m-2c_r|∣m−2cr​∣
  • K
    EEE
    impact[u]=0impact[u]=0impact[u]=0
    ppp
    t
    [
    p
    +
    1].
    hh
    h
    B+[Z≠E]⋅min⁡r∣m−2cr∣B+[Z\ne E]\cdot\min_r|m-2c_r|B+[Z=E]⋅minr​∣m−2cr​∣
    Q
    )
    log
    N
    )
    O(N)O(N)O(N)