Codeforces Round 1111 (Div. 2)
D2
XOR 排序(困难版)
XOR Sorting (Hard Version)
Codeforces
2000
原题
← D1E →

XOR 排序(困难版)AI (CI翻译)

This is the hard version of the problem. The only difference between the versions is that in this version, $0 \le q \le 10^6$.

这是本题的困难版本。两个版本之间的唯一差异在于,本版本中 $0 \le q \le 10^6$。AI (CI翻译)

Note that zero-based indexing is used in this problem.

请注意,本题使用从零开始的下标。AI (CI翻译)

For an array $b$ consisting of $m$ positive integers, define $f(b)$ as follows.

对于一个由 $m$ 个正整数组成的数组 $b$,定义 $f(b)$ 如下。AI (CI翻译)

For a non-negative integer $k$, we say that $b$ can be $k$-sorted if it can be sorted in non-decreasing order by performing the following operation any number of times:

对于非负整数 $k$,如果可以通过任意次执行以下操作,将 $b$ 排序为非递减顺序,则称 $b$ 可以被 $k$-排序:AI (CI翻译)

  1. Choose two indices $i$ and $j$ ($0 \le i \lt j \le m - 1$) such that $i \oplus j \le k$$^{\text{∗}}$. Note that the condition applies to the indices $i$ and $j$, not to the elements $b_i$ and $b_j$.
  2. Swap the elements $b_i$ and $b_j$.
  1. 选择两个下标 $i$ 和 $j$($0 \le i \lt j \le m - 1$),满足 $i \oplus j \le k$$^{\text{∗}}$。注意,该条件适用于下标 $i$ 和 $j$,而不是元素 $b_i$ 和 $b_j$。
  2. 交换元素 $b_i$ 和 $b_j$。
AI (CI翻译)

The value $f(b)$ is defined as the smallest non-negative integer $k$ such that the array $b$ can be $k$-sorted.

将使数组 $b$ 可以被 $k$-排序的最小非负整数 $k$ 定义为 $f(b)$。AI (CI翻译)

You are given an array $a$ of length $n$, consisting of positive integers. You will perform $q$ updates on $a$. Each update has the following form:

给定一个长度为 $n$、由正整数组成的数组 $a$。你需要对 $a$ 执行 $q$ 次更新。每次更新的形式如下:AI (CI翻译)

  • $i \; x$: assign $a_i = x$.
  • $i \; x$:令 $a_i = x$。
AI (CI翻译)

Note that the updates are persistent. In other words, each update affects all subsequent states of the array.

注意,更新是持久的。换句话说,每次更新都会影响数组之后的所有状态。AI (CI翻译)

For each of the $q + 1$ states of $a$ — the initial state and the state after each of the $q$ updates — find the value of $f(a)$.

对于 $a$ 的 $q + 1$ 个状态——初始状态以及每次更新后的状态——求 $f(a)$ 的值。AI (CI翻译)

$^{\text{∗}}$$\oplus$ denotes the bitwise XOR operation

$^{\text{∗}}$$\oplus$ 表示按位异或运算。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 10^6$, $0 \le q \le 10^6$) — the length of the array $a$ and the number of updates.

每个测试用例的第一行包含两个整数 $n$ 和 $q$($1 \le n \le 10^6$,$0 \le q \le 10^6$)——数组 $a$ 的长度以及更新次数。AI (CI翻译)

The second line of each test case contains $n$ integers $a_0, a_1, \ldots, a_{n - 1}$ ($1 \le a_i \le 10^9$) — the array $a$.

每个测试用例的第二行包含 $n$ 个整数 $a_0, a_1, \ldots, a_{n - 1}$($1 \le a_i \le 10^9$)——数组 $a$。AI (CI翻译)

The $j$-th of the following $q$ lines contains two integers $i_j$ and $x_j$ ($0 \le i_j \lt n$, $1 \le x_j \le 10^9$) — the description of the $j$-th update. This update means that the assignment $a_{i_j} = x_j$ is performed.

接下来的 $q$ 行中的第 $j$ 行包含两个整数 $i_j$ 和 $x_j$($0 \le i_j \lt n$,$1 \le x_j \le 10^9$)——第 $j$ 次更新的描述。该更新表示执行赋值 $a_{i_j} = x_j$。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

保证所有测试用例的 $n$ 之和不超过 $10^6$。AI (CI翻译)

It is guaranteed that the sum of $q$ over all test cases does not exceed $10^6$.

保证所有测试用例的 $q$ 之和不超过 $10^6$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output $q + 1$ integers — the values of $f(a)$ for the initial state of the array and after each of the $q$ updates, in order.

对于每个测试用例,输出 $q + 1$ 个整数——数组初始状态以及按顺序执行每次更新后数组状态对应的 $f(a)$ 值。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first example, the initial array is $a = [1, 2]$. It is already sorted, so $f(a) = 0$.

在第一个样例中,初始数组为 $a = [1, 2]$。它已经有序,因此 $f(a) = 0$。AI (CI翻译)

In the second example, the initial array is $a = [10^9, 10^9 - 1]$. We can swap $a_0$ and $a_1$, transforming $a$ as follows: $[\color{red}{10^9}, \color{red}{10^9 - 1}] \rightarrow [\color{red}{10^9 - 1}, \color{red}{10^9}]$. Therefore, $f(a) = 0 \oplus 1 = 1$.

在第二个样例中,初始数组为 $a = [10^9, 10^9 - 1]$。我们可以交换 $a_0$ 和 $a_1$,将 $a$ 变为:$[\color{red}{10^9}, \color{red}{10^9 - 1}] \rightarrow [\color{red}{10^9 - 1}, \color{red}{10^9}]$。因此,$f(a) = 0 \oplus 1 = 1$。AI (CI翻译)

After the only update, the array becomes $a = [10^9, 10^9]$. It is already sorted, so $f(a) = 0$.

执行唯一一次更新后,数组变为 $a = [10^9, 10^9]$。它已经有序,因此 $f(a) = 0$。AI (CI翻译)

In the third example, the initial array is $a = [2, 5, 3, 4, 1, 6]$. We can perform swaps using the index pairs $(0, 1)$ and $(0, 4)$, transforming $a$ as follows: $[\color{red}{2}, \color{red}{5}, 3, 4, 1, 6] \rightarrow [\color{red}{5}, \color{red}{2}, 3, 4, 1, 6]$, $[\color{red}{5}, 2, 3, 4, \color{red}{1}, 6] \rightarrow [\color{red}{1}, 2, 3, 4, \color{red}{5}, 6]$. It can be shown that no smaller value of $k$ is sufficient, so $f(a) = \max(0 \oplus 1, 0 \oplus 4) = 4$.

在第三个样例中,初始数组为 $a = [2, 5, 3, 4, 1, 6]$。我们可以使用下标对 $(0, 1)$ 和 $(0, 4)$ 执行交换,将 $a$ 依次变为:$[\color{red}{2}, \color{red}{5}, 3, 4, 1, 6] \rightarrow [\color{red}{5}, \color{red}{2}, 3, 4, 1, 6]$,$[\color{red}{5}, 2, 3, 4, \color{red}{1}, 6] \rightarrow [\color{red}{1}, 2, 3, 4, \color{red}{5}, 6]$。可以证明,没有更小的 $k$ 能够满足要求,因此 $f(a) = \max(0 \oplus 1, 0 \oplus 4) = 4$。AI (CI翻译)

样例

Input #1
3
2 0
1 2
2 1
1000000000 999999999
1 1000000000
6 2
2 5 3 4 1 6
1 2
4 5
Output #1
0
1
0
4
4
0
InputCopy OutputCopy
题目信息
题号
2247D2
时间限制
2 s
内存限制
256 MB
标签
位运算
数据结构
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。