#P1366. 有序表的合并

有序表的合并

Description

Given two sequences a,ba, b, both sorted in non-decreasing order. It is guaranteed that there are no duplicate numbers in aa.

Now, for each number in aa, determine how many times it appears in bb.

Input Format

There are multiple test cases in a single test file.

The first line contains an integer TT, the number of test cases. Then, for each test case, the input is given as follows:

  • The first line contains two integers, the lengths of sequences aa and bb, denoted by nn and mm.
  • The second line contains nn integers representing sequence aa, where the ii-th integer is aia_i.
  • The third line contains mm integers representing sequence bb, where the ii-th integer is bib_i.

Output Format

To avoid excessive output, for each test case, output a single integer on one line: the bitwise XOR sum of the occurrence counts of every number of aa in bb.

Formally, let aia_i appear cic_i times in bb. You need to output c1c2cnc_1 \bigoplus c_2 \bigoplus \dots \bigoplus c_n, where \bigoplus denotes the bitwise XOR operation. You may refer to the Hint to complete the computation.

1
3 5
1 3 6
1 3 3 5 5
3
1
9 4
1 2 3 4 5 6 7 8 9
1 1 4 5
2
2
3 5
1 3 6
1 3 3 5 5
9 4
1 2 3 4 5 6 7 8 9
1 1 4 5
3
2

Hint

Explanation of Sample 1

  • a1=1a_1 = 1 appears 11 time in bb.
  • a2=3a_2 = 3 appears 22 times in bb.
  • a3=6a_3 = 6 appears 00 times in bb.

Therefore, the output is 12=31 \bigoplus 2 = 3.

Explanation of Sample 2

1,4,51, 4, 5 appear 2,1,12, 1, 1 times in bb, respectively, so the output is 211=22 \bigoplus 1 \bigoplus 1 = 2.

Constraints

For all test files, it is guaranteed that:

  • 1T101 \leq T \leq 10;
  • 1n,m1071 \leq n, m \leq 10^7, and (n+m)107\sum (n + m) \leq 10^7;
  • 1ai,bi<2641 \leq a_i, b_i < 2^{64}, and ai<ai+1a_i < a_{i + 1}, bibi+1b_i \leq b_{i + 1}.

Here, (n+m)\sum (n + m) denotes the sum of all nn and mm within a single test file, i.e., the total length of the input sequences does not exceed 10710^7.

Notes

  • Pay attention to the impact of large input on program efficiency. Choose an appropriate input method to avoid timeouts.
  • Use suitable data types to store variables to avoid overflow.
  • If you do not know what a bitwise XOR sum is, you can add the following function to your code:
template <class T>
T getXorSum(T *begin, T *end) {
  T ret = 0;
  for (T *it = begin; it != end; ++it) ret ^= *it;
  return ret;
}

This function computes the bitwise XOR sum over a half-open interval of the given array (including std::vector). The return type matches the type of the input array. The usage is similar to std::sort. For example, to compute the bitwise XOR sum of a1ana_1 \sim a_n, call getXorSum(a + 1, a + 1 + n). To compute the bitwise XOR sum of a0an1a_0 \sim a_{n - 1}, call getXorSum(a, a + n). If aa is a std::vector, replace a in the calls above with a.begin() accordingly.

Translated by ChatGPT 5