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

+⊕ and query

Back to problem

Editorial

Let K=31K=31K=31. Every input value is smaller than 2302^{30}230, so considering bits 000 through 303030 is sufficient.

For 0≤j<K0 \leq j < K0≤j<K and 0≤r<2j0 \leq r < 2^j0≤r<2j, put every index satisfying Ai  into a node . Bits are read from least significant to most significant. Thus, the two children of are and .

Index iii gives the following demand to node (j,Ai mod 2j)(j,A_i \bmod 2^j)(j,Ai​mod2j):

di,j=bit⁡j(AixorBi).d_{i,j}=\operatorname{bit}_j(A_i\mathbin{\mathrm{xor}}B_i).di,j​=bitj​(A

Suppose a composition of additions and XORs maps an input value zzz to f(z)f(z)f(z). The lower jjj bits of f(z)f(z)f(z) depend only on the lower jjj bits of . Therefore, all demands assigned to one node must be equal.

The following lemma is the key observation.

Lemma. Write ej,re_{j,r}ej,r​ for the change of bit jjj at node (j,r)(j,r)(j,r). These changes can be produced by a composition of additions and XORs if and only if, for every 1≤, the following value is the same for every :

ej,rxorej+1,rxorej+1,r+2j.e_{j,r}\mathbin{\mathrm{xor}}e_{j+1,r}\mathbin{\mathrm{xor}}e_{j+1,r+2^j}.ej,r​xorej

There is only one node at j=0j=0j=0, so no separate condition is needed there. The lemma can be proved by checking the identity for an addition and an XOR, then constructing a composition bit by bit from lower bits to higher bits. The additional bit 303030 prevents a wraparound that would only be valid modulo a power of two. The nonnegativity restriction can be met by using a sufficiently large common offset during the construction.

Call a node containing at least one index an active node. If one active node receives different demands, the transformation is impossible. Otherwise, denote its common demand by d(j,r)d(j,r)d(j,r).

If both children of an active node (j,r)(j,r)(j,r) are active, the value in the lemma is forced to be

c(j,r)=d(j,r)xord(j+1,r)xord(j+1,r+2j).c(j,r)=d(j,r)\mathbin{\mathrm{xor}}d(j+1,r)\mathbin{\mathrm{xor}}d(j+1,r+2^j).c(j,r)=d(j,r)xord(j

If one child is empty, its value is free and creates no restriction. Consequently, a transformation exists if and only if both conditions below hold.

  • All demands agree inside every active node.
  • At each depth jjj, all nodes with two active children have the same value of c(j,r)c(j,r)c(j,r).

Maintain a binary trie of the values in AAA, inserting bits from least significant to most significant. Each node stores the number of indices demanding 000 and the number demanding 111. We also maintain the total number of nodes with conflicting demands and, at every depth, the numbers of branching nodes forcing each of the two possible values of ccc.

When one element changes, remove the old pair (Ai,Bi)(A_i,B_i)(Ai​,Bi​) from the trie and insert the new pair. Only one root-to-leaf path is affected. The status of a node can be updated using that node and its two children.

The time complexity of one query is O(K)=O(log⁡109)O(K)=O(\log 10^9)O(K)=O(log109), and the total space complexity is O((N+Q)K)O((N+Q)K)O((N+Q.

Solution written by GPT5

mod 2j=rA_i \bmod 2^j=r
Ai​mod2j=r
(j,r)(j,r)(j,r)
(j,r)(j,r)(j,r)
(j+1,r)(j+1,r)(j+1,r)
(j+1,r+2j)(j+1,r+2^j)(j+1,r+2j)
i
​
xor
Bi​).
zz
z
j≤K−21 \leq j \leq K-2
1≤j≤K−2
rrr
+
1
,
r
​
xor
ej+1,r+2j​.
+
1,r)xor
d(j+
1,r+
2j).
)
K
)