Official

Ex - 01? Queries Editorial by en_translator


We refer “a possible substring of a string obtained by replacing each occurrence of ? in SS by 0 or 1 independently” to simply “a string obtained from SS.” For simplicity, here we assume that an empty string can also be obtained from SS too.

For the string S=s1s2sNS = s_1s_2\ldots s_N, for each i=0,1,,Ni = 0, 1, \ldots, N, let

  • dp[i][0]:=\mathrm{dp}[i][0] := (the number of strings obtained from the first ii character of SS, s1s2sis_1s_2\ldots s_i, ending with 0);
  • dp[i][1]:=\mathrm{dp}[i][1] := (the number of strings obtained from the first ii character of SS, s1s2sis_1s_2\ldots s_i, ending with 1).

Then, the value that should be printed for each query is dp[N][0]+dp[N][1]\mathrm{dp}[N][0] + \mathrm{dp}[N][1] for SS at each moment. We will consider how to compute this.

First, obviously, dp[0][0]=dp[0][1]=0\mathrm{dp}[0][0] = \mathrm{dp}[0][1] = 0. We will now consider, for i=1,2,,Ni = 1, 2, \ldots, N, “how to find dp[i][0]\mathrm{dp}[i][0] and dp[i][1]\mathrm{dp}[i][1] when we know dp[i1][0]\mathrm{dp}[i-1][0] and dp[i1][1]\mathrm{dp}[i-1][1].”

We first consider si=s_i = 0. Any string obtained from s1s2si1s_1s_2\ldots s_{i-1} can be obviously obtained from s1s2sis_1s_2\ldots s_i too. Any string that cannot be obtained from s1s2si1s_1s_2\ldots s_{i-1} that can be obtained from s1s2si1sis_1s_2\ldots s_{i-1} s_i by appending si=s_i = 0 is

a concatenation of a string obtained from s1s2si1s_1s_2\ldots s_{i-1} and 0, that cannot be obtained from s1s2si1s_1s_2\ldots s_{i-1}.

The number of “strings that are concatenations of a string obtained from s1s2si1s_1s_2\ldots s_{i-1} and 0” is equal to the number of strings that can be obtained from s1s2si1s_1s_2\ldots s_{i-1}, which is expressed as dp[i1][0]+dp[i1][1]+1\mathrm{dp}[i-1][0] + \mathrm{dp}[i-1][1] + 1 (where the last term 11 correspond to an empty string) The number of strings that “can be obtained from s1s2si1s_1s_2\ldots s_{i-1}” is dp[i1][0]\mathrm{dp}[i-1][0] (any string t1t2tlt_1 t_2 \ldots t_l ending with 0 that can be obtained from s1s2si1s_1s_2\ldots s_{i-1} can be constructed as a concatenation of t1t2tl1t_1 t_2 \ldots t_{l-1}, which can be obtained from s1s2si1s_1s_2\ldots s_{i-1}, and 0).

Therefore, by appending si=s_i = 0 to the tail of s1s2si1s_1s_2\ldots s_{i-1}, there are dp[i1][1]+1\mathrm{dp}[i-1][1] + 1 new strings ending with 0 obtained not from s1s2si1s_1s_2\ldots s_{i-1} but from s1s2si1sis_1s_2\ldots s_{i-1} s_i.

Therefore, when si=s_i = 0,

  • dp[i][0]=dp[i1][0]+(dp[i1][1]+1)\mathrm{dp}[i][0] = \mathrm{dp}[i-1][0] + (\mathrm{dp}[i-1][1] + 1),
  • dp[i][1]=dp[i1][1]\mathrm{dp}[i][1] = \mathrm{dp}[i-1][1].

Similarly, when si=s_i = 1,

  • dp[i][0]=dp[i1][0]\mathrm{dp}[i][0] = \mathrm{dp}[i-1][0],
  • dp[i][1]=dp[i1][1]+(dp[i1][0]+1)\mathrm{dp}[i][1] = \mathrm{dp}[i-1][1] + (\mathrm{dp}[i-1][0] + 1),

and when si=s_i = ?,

  • dp[i][0]=dp[i1][0]+(dp[i1][1]+1)\mathrm{dp}[i][0] = \mathrm{dp}[i-1][0] + (\mathrm{dp}[i-1][1] + 1)
  • dp[i][1]=dp[i1][1]+(dp[i1][0]+1)\mathrm{dp}[i][1] = \mathrm{dp}[i-1][1] + (\mathrm{dp}[i-1][0] + 1)

Therefore, let

A0:=[111010001],A1:=[100111001],A?:=[111111001], \mathbf{A}_0 := \left[\begin{matrix} 1 & 1 & 1 \\ 0 & 1 & 0\\ 0 & 0 & 1 \\ \end{matrix}\right], \mathbf{A}_1 := \left[\begin{matrix} 1 & 0 & 0 \\ 1 & 1 & 1\\ 0 & 0 & 1 \\ \end{matrix}\right], \mathbf{A}_? := \left[\begin{matrix} 1 & 1 & 1 \\ 1 & 1 & 1\\ 0 & 0 & 1 \\ \end{matrix}\right],

then for each i=1,2,,Ni = 1, 2, \ldots, N we have

[dp[i][0]dp[i][1]1]=Asi[dp[i1][0]dp[i1][1]1]. \left[\begin{matrix} \mathrm{dp}[i][0]\\ \mathrm{dp}[i][1]\\ 1 \\ \end{matrix}\right] = \mathbf{A}_{s_i} \left[\begin{matrix} \mathrm{dp}[i-1][0]\\ \mathrm{dp}[i-1][1]\\ 1 \\ \end{matrix}\right].

Especially,

[dp[N][0]dp[N][1]1]=AsNAsN1As1[dp[0][0]dp[0][1]1]=AsNAsN1As1[001]. \left[\begin{matrix} \mathrm{dp}[N][0]\\ \mathrm{dp}[N][1]\\ 1 \\ \end{matrix}\right] = \mathbf{A}_{s_N}\mathbf{A}_{s_{N-1}}\cdots \mathbf{A}_{s_1}\left[\begin{matrix} \mathrm{dp}[0][0]\\ \mathrm{dp}[0][1]\\ 1 \\ \end{matrix}\right] = \mathbf{A}_{s_N}\mathbf{A}_{s_{N-1}}\cdots \mathbf{A}_{s_1}\left[\begin{matrix} 0\\ 0\\ 1 \\ \end{matrix}\right].

Therefore, each query can be processed by computing the product of NN matrices AsNAsN1As1\mathbf{A}_{s_N}\mathbf{A}_{s_{N-1}}\cdots \mathbf{A}_{s_1}, and updating one of AsN,AsN1,,As1\mathbf{A}_{s_N}, \mathbf{A}_{s_{N-1}}, \ldots, \mathbf{A}_{s_1} to another matrix. This can be achieved with a segment tree in an O(logN)\mathrm{O}(\log N) time for each query.

Therefore, the problem has been solved in a total of O(N+QlogN)\mathrm{O}(N + Q\log N) time.

posted:
last update:



2025-04-15 (Tue)
04:18:07 +00:00