Official

D - Insert XOR Editorial by evima


A sequence \(C\) of length at least \(2\) can be obtained by performing operations several times on the sequence \((0,0)\) if and only if:

  • Does not contain \(1\)

A sequence \(C\) of length at least \(2\) can be obtained by performing operations several times on the sequence \((0,1)\) if and only if:

  • \(C_1=0, \; C_{|C|}=1\)
  • \(0\)s do not appear consecutively

A sequence \(C\) of length at least \(2\) can be obtained by performing operations several times on the sequence \((1,1)\) if and only if:

  • \(C_1=1, \; C_{|C|}=1\)
  • If \(2 < |C|\), then it contains \(0\)
  • \(0\)s do not appear consecutively

Consider the elements of sequence \(A\) that are included in \(B\) where \(|B|\) is minimized.

\(A_1\) and \(A_N\) are included in \(B\).

For \(l,r\) satisfying:

  • \(l<r\)
  • \(A_l=A_{l+1}=\dots=A_{r}=0\)
  • \(l=1\) or \(A_{l-1}=1\)
  • \(r=N\) or \(A_{r+1}=1\)

we can see that \(A_l\) and \(A_r\) are included in \(B\), while \(A_{l+1},A_{l+2},\dots,A_{r-1}\) need not be included in \(B\). Also, for such \(l,r\), when we remove \(A_l,A_{l+1},\dots,A_r\) and partition at that point, each partitioned consecutive subsequence contains at least one \(1\), one of which needs to be included in \(B\), and this is also sufficient. Therefore, we can find the minimum value of \(|B|\) by the number of such partitions.
Note that when \((A_1,A_2)=(0,1)\) or \((A_N,A_{N-1})=(0,1)\), we need to include two elements from the same consecutive subsequence in \(B\).

To find this value for each update query, we need to manage how many consecutive subsequences to remove (where two or more \(0\)s appear consecutively), and the overall time complexity is \(O(N+Q)\).

posted:
last update: