#P93. [CSP-S 2024] Arena championship

[CSP-S 2024] Arena championship

Person in Charge

Problem Description

Little S wants to organize a tournament game. If there are 2k2^k participants, the game will proceed in kk rounds:

  • In the first round, participants with IDs 11 and 22 compete, participants with IDs 33 and 44 compete, and so on, up to participants with IDs 2k12^k - 1 and 2k2^k.
  • In the second round, only the winners from the first round remain, and adjacent pairs compete sequentially.
  • This continues similarly until the (k1)(k - 1)-th round, where only the 44 winners from the (k2)(k - 2)-th round remain, and the first two and last two compete separately (i.e., the semifinals).
  • The kk-th round is the final between the two winners of the semifinals.

After determining the advancement rules, Little S sets the match rules as a "challenge" system. Specifically, each participant has an ability value a1,a2,,a2ka_1, a_2, \dots, a_{2^k}, where each aia_i is an integer in [0,2311][0, 2^{31}-1]. For each match, a number 00 or 11 is drawn randomly. Let dR,Gd_{R,G} denote the number drawn for the GG-th match in the RR-th round. If dR,G=0d_{R,G} = 0, the participant with the smaller ID is the "challenger"; if dR,G=1d_{R,G} = 1, the participant with the larger ID is the challenger. The challenger wins if and only if their ability value aRa \ge R. In other words, the outcome of the match depends solely on the challenger's ability value and the current round number, and is independent of the other participant's ability value.

Now, Little S receives registration information from nn participants one by one, each reporting their ability value. Little S assigns IDs 1,2,,n1, 2, \dots, n to the participants in the order they register. Little S is interested in determining the minimum number of additional participants needed to make the total number of participants a power of 22, and the sum of IDs of all possible champions after a complete tournament under all possible outcomes.

Formally, let kk be the smallest non-negative integer such that 2kn2^k \ge n. Then, (2kn)(2^k - n) additional participants must be added, and their ability values can be any integers in [0,2311][0, 2^{31}-1]. If an additional participant has a chance to win, their ID must also be included in the answer.

Little S finds this problem too simple, so he gives you mm queries c1,c2,,cmc_1, c_2, \dots, c_m. For each cic_i, Little S asks you to compute the answer when only the first cic_i participants have registered.

Input Format

This problem contains multiple test cases. However, different test cases are generated by modifying only a1,a2,,ana_1, a_2, \dots, a_n, while other content remains unchanged. Refer to the following format. Here, \oplus denotes the bitwise XOR operator, and amodba \bmod b denotes the remainder when aa is divided by bb.

The first line of input contains two positive integers n,mn, m, representing the number of registered participants and the number of queries.

The second line contains nn non-negative integers a1,a2,,ana'_1, a'_2, \dots, a'_n, which are used to compute the actual ability values.

The third line contains mm positive integers c1,c2,,cmc_1, c_2, \dots, c_m, representing the queries.

Let KK be the smallest non-negative integer such that 2Kn2^K \ge n. The next KK lines each contain 2KR2^{K-R} numbers (without spaces), where the GG-th number in the RR-th line represents the drawn value dR,G=0/1d_{R,G} = 0/1 for the GG-th match in the RR-th round.

Note that since queries only require rounding the number of participants up to 2kci2^k \ge c_i, where kKk \le K, you may not use all the input values.

The next line contains a positive integer TT, indicating the number of test cases.

The following TT lines each describe a test case with 44 non-negative integers X0,X1,X2,X3X_0, X_1, X_2, X_3. The actual ability values for the test case are computed as ai=aiXimod4a_i = a'_i \oplus X_{i \bmod 4}, where 1in1 \le i \le n.

Output Format

Output TT lines. For each test case, let AiA_i be the answer for the ii-th query (1im1 \le i \le m). You need to output a single integer per line, representing $(1 \times A_1) \oplus (2 \times A_2) \oplus \dots \oplus (m \times A_m)$.

Samples

5 5
0 0 0 0 0
5 4 1 2 3
1001
10
1
4
2 1 0 0
1 2 1 0
0 2 3 1
2 2 0 1
5
19
7
1

Sample 1 Explanation

There are T=4T = 4 test cases. Here, only the first test case is explained. The actual ability values for the 55 participants are [1,0,0,2,1][1, 0, 0, 2, 1]. The 55 queries are for prefixes of lengths 5,4,1,2,35, 4, 1, 2, 3.

  1. For the prefix of length 11, since there is only participant 11, the answer is 11.
  2. For the prefix of length 22, since 22 is already a power of 22, no additional participants are needed. The draw d1,1=1d_{1,1} = 1 means participant 22 is the challenger. Since a2<1a_2 < 1, participant 11 wins, and the answer is 11.
  3. For the prefix of length 33, participant 11 wins against 22 (since d1,1=1d_{1,1} = 1, 22 is the challenger, and a2<1a_2 < 1). Although the ability value of participant 44 is unknown, participant 44 will always win against 33 (since d1,2=0d_{1,2} = 0, 33 is the challenger, and a3<1a_3 < 1). In the final, either 11 or 44 can win (since d2,1=1d_{2,1} = 1, 44 is the challenger; if a4<2a_4 < 2, 11 wins; otherwise, 44 wins). Thus, the answer is 1+4=51 + 4 = 5.
  4. For the prefix of length 44, since a42a_4 \ge 2, participant 44 wins the final.
  5. For the prefix of length 55, it can be shown that possible winners include participants 44, 77, and 88, so the answer is 1919.

Thus, the output for this test case is $(1 \times 19) \oplus (2 \times 4) \oplus (3 \times 1) \oplus (4 \times 1) \oplus (5 \times 5) = 5$.

Sample 2

See the files arena/arena2.in and arena/arena2.ans in the contestant directory.

This sample satisfies Special Property A.

Sample 3

See the files arena/arena3.in and arena/arena3.ans in the contestant directory.

This sample satisfies Special Property B.

Sample 4

See the files arena/arena4.in and arena/arena4.ans in the contestant directory.

Sample 5

See the files arena/arena5.in and arena/arena5.ans in the contestant directory.

Data Range

For all test cases, it is guaranteed that: 2n,m1052 \le n, m \le 10^5, 0ai,Xj<2310 \le a_i, X_j < 2^{31}, 1cin1 \le c_i \le n, 1T2561 \le T \le 256.

Test Case T=T= n,mn, m \le Special Property A Special Property B
131\sim 3 11 88 No No
4,54,5 500500 Yes
686\sim 8 No Yes
9,109,10 50005000 No
11,1211,12 10510^5 Yes
131513\sim 15 No Yes
16,1716,17 44 No
18,1918,19 1616
20,2120,21 6464
22,2322,23 128128
24,2524,25 256256

Special Property A: All queries cic_i are powers of 22.

Special Property B: All dR,G=0d_{R,G} = 0.