#P97. [NOI 2025 Day 2] 三目运算符

[NOI 2025 Day 2] 三目运算符

Person in Charge

Problem Description

For a binary string S=s1snS = s_1 \ldots s_n of length n (n3)n\ (n \ge 3), define the transformation T=f(S)=t1tnT = f(S) = t_1 \ldots t_n as follows:

$$t_i = \begin{cases} s_i, & i \le 2, \\ s_i, & i \ge 3 \text{ and } s_{i-2} = 0, \\ s_{i-1}, & i \ge 3 \text{ and } s_{i-2} = 1. \end{cases}$$

A fixed point of the transformation ff is defined as follows: if a binary string TT satisfies f(T)=Tf(T) = T, then TT is called a fixed point of ff.

Let fk(S)f^k(S) denote the string obtained by applying the transformation ff kk times to SS. Specifically, let f0(S)=Sf^0(S) = S. Find the smallest natural number kk such that fk(S)f^k(S) is a fixed point of ff, i.e., the smallest natural number kk satisfying fk+1(S)=fk(S)f^{k+1}(S) = f^k(S). It can be proven that such a kk always exists.

Little Z found this problem too simple, so he added qq modification operations. The ii-th (1iq1 \le i \le q) modification is given by two positive integers li,ril_i, r_i (1lirin1 \le l_i \le r_i \le n), which flips all bits in the interval [li,ri][l_i, r_i] (i.e., 0 becomes 1 and 1 becomes 0). For the initial string and after each modification, you need to find the smallest natural number kk such that fk(S)f^k(S) is a fixed point of ff.

Input Format

This problem contains multiple test cases.

The first line of input contains two non-negative integers c,tc, t, representing the test case number and the number of test cases, respectively. c=0c = 0 indicates that this test case is a sample.

For each test case:

The first line contains two positive integers n,qn, q, representing the length of SS and the number of modifications, respectively.

The second line contains a binary string S=s1snS = s_1 \ldots s_n of length nn, representing the initial string.

The (i+2)(i + 2)-th line (1iq1 \le i \le q) contains two positive integers li,ril_i, r_i, representing a modification operation.

Output Format

For each test case, let k0k_0 be the answer for the initial string and kik_i (1iq1 \le i \le q) be the answer after the ii-th modification. Output a single positive integer, which is i=0q((i+1)×ki)\oplus_{i=0}^{q} ((i + 1) \times k_i), where \oplus denotes the bitwise XOR operation.

Samples

0 2
5 2
11010
3 3
2 2
7 3
1010100
7 7
2 4
1 2
2
4

Sample 1 Explanation

This sample contains two test cases.

For the first test case:

  • Initially, S=11010S = 11010, f(S)=11100f(S) = 11100, f2(S)=11110f^2(S) = 11110, f3(S)=f4(S)=11111f^3(S) = f^4(S) = 11111, so k0=3k_0 = 3;
  • After the first modification, S=11110S = 11110, f(S)=f2(S)=11111f(S) = f^2(S) = 11111, so k1=1k_1 = 1;
  • After the second modification, S=10110S = 10110, f(S)=f2(S)=10011f(S) = f^2(S) = 10011, so k2=1k_2 = 1.

Thus, the answer is $\bigoplus_{i=0}^{q} ((i+1) \times k_i) = (1 \times 3) \oplus (2 \times 1) \oplus (3 \times 1) = 3 \oplus 2 \oplus 3 = 2$.

For the second test case:

  • Initially, S=1010100S = 1010100, k0=1k_0 = 1;
  • After the first modification, S=1010101S = 1010101, k1=1k_1 = 1;
  • After the second modification, S=1101101S = 1101101, k2=5k_2 = 5;
  • After the third modification, S=0001101S = 0001101, k3=2k_3 = 2.

Thus, the answer is $\bigoplus_{i=0}^{q} ((i+1) \times k_i) = (1 \times 1) \oplus (2 \times 1) \oplus (3 \times 5) \oplus (4 \times 2) = 4$.

Sample 2

See the files ternary/ternary2.in and ternary/ternary2.ans in the contestant directory.

This sample satisfies the constraints of test cases 131 \sim 3.

Sample 3

See the files ternary/ternary3.in and ternary/ternary3.ans in the contestant directory.

This sample satisfies the constraints of test cases 464 \sim 6.

Sample 4

See the files ternary/ternary4.in and ternary/ternary4.ans in the contestant directory.

This sample satisfies the constraints of test cases 13,1413,14.

Sample 5

See the files ternary/ternary5.in and ternary/ternary5.ans in the contestant directory.

This sample satisfies the constraints of test cases 171917 \sim 19.

Data Range

Let N,QN, Q be the sums of n,qn, q across all test cases in a single test point. For all test cases, it is guaranteed:

  • 1t51 \le t \le 5;
  • 3n4×1053 \le n \le 4 \times 10^5, N8×105N \le 8 \times 10^5;
  • 1q4×1051 \le q \le 4 \times 10^5, Q8×105Q \le 8 \times 10^5;
  • For all 1in1 \le i \le n, si{0,1}s_i \in \{0, 1\};
  • For all 1iq1 \le i \le q, 1lirin1 \le l_i \le r_i \le n.
Test Case n,qn, q \le N,QN, Q \le Special Properties
131 \sim 3 200200 10310^3 A
464 \sim 6 None
7,87, 8 5,0005,000 10410^4 A
9119 \sim 11 None
1212 10510^5 2×1052 \times 10^5 A
13,1413, 14 B
15,1615, 16 None
171917 \sim 19 4×1054 \times 10^5 8×1058 \times 10^5 C
2020 None
  • Special Property A: The initial string and each modified string have a position p[2,n]p \in [2, n] such that s1=s2==sp=1s_1 = s_2 = \cdots = s_p = 1 and sp+1==sn=0s_{p+1} = \cdots = s_n = 0.
  • Special Property B: For all 1iq1 \le i \le q, li=1l_i = 1 and ri=nr_i = n.
  • Special Property C: For all 1iq1 \le i \le q, li=1l_i = 1 and r1r2rqr_1 \le r_2 \le \cdots \le r_q.