Codeforces Round 1116 (Div. 1)
E2
最终会剩下什么?(困难版)
What Will Remain at the End? (Hard Version)
Codeforces
3400
原题
← E1F →

最终会剩下什么?(困难版)AI (CI翻译)

This is the hard version of the problem. The only difference between the two versions is the set of allowed values for the initial array and for $x$ in operations of type $1$. In this version, these values can be any integers in $[-10^9, 10^9]$. You can make hacks only if both versions of the problem are solved.

这是本题的困难版本。两个版本之间唯一的区别是初始数组以及类型 $1$ 操作中的 $x$ 所允许的取值范围。在本版本中,这些值可以是 $[-10^9, 10^9]$ 中的任意整数。只有在两个版本的问题都解决后才能进行 hack。AI (CI翻译)

Before her final sortie, Chtholly asks Willem three questions.

在最终出击前,Chtholly 向 Willem 提出了三个问题。AI (CI翻译)

The second is this: what will remain if the sky truly reaches its end?

第二个问题是:如果天空真的走到了尽头,最终会剩下什么?AI (CI翻译)

Willem cannot answer her directly. Instead, he opens a chronicle containing $n$ records, numbered from $1$ to $n$. Each record holds an integer: a positive value represents hope, while a negative value represents despair.

Willem 无法直接回答她。于是,他打开了一本包含 $n$ 条记录的编年史,记录编号从 $1$ 到 $n$。每条记录中存放一个整数:正数代表希望,负数代表绝望。AI (CI翻译)

The initial contents of the chronicle form an array $a_1,a_2,\ldots,a_n$, called version $0$. Chtholly then performs $q$ operations. For each $1\le i\le q$, the $i$-th operation creates a new version $i$ from version $i-1$.

编年史的初始内容构成数组 $a_1,a_2,\ldots,a_n$,称为版本 $0$。随后,Chtholly 执行 $q$ 次操作。对于每个 $1\le i\le q$,第 $i$ 次操作会根据版本 $i-1$ 创建一个新的版本 $i$。AI (CI翻译)

Each operation has one of the following four types:

每次操作属于以下四种类型之一:AI (CI翻译)

  • $\texttt{1 l r x}$: set $a_k\gets x$ for every $l\le k\le r$.
  • $\texttt{2 l r}$: set $a_k\gets -a_k$ for every $l\le k\le r$.
  • $\texttt{3 l r}$: set $a_k\gets\max(a_k,0)$ for every $l\le k\le r$.
  • $\texttt{4 p}$: consider the value at position $p$ in every previous version $0,1,\ldots, i-1$. Let these values be $b_0,b_1,\ldots,b_{i-1}$. Find the maximum sum over all non-empty subarrays$^{\text{∗}}$ of this sequence.
  • $\texttt{1 l r x}$:对于每个 $l\le k\le r$,令 $a_k\gets x$。
  • $\texttt{2 l r}$:对于每个 $l\le k\le r$,令 $a_k\gets -a_k$。
  • $\texttt{3 l r}$:对于每个 $l\le k\le r$,令 $a_k\gets\max(a_k,0)$。
  • $\texttt{4 p}$:考虑每个之前版本 $0,1,\ldots,i-1$ 中位置 $p$ 的值。设这些值为 $b_0,b_1,\ldots,b_{i-1}$。求该序列所有非空子数组$^{\text{∗}}$中的最大元素和。
AI (CI翻译)

If the operation is of type $1$, $2$, or $3$, the specified modification is applied to version $i-1$ to obtain version $i$. An operation of type $4$ does not modify the array, so version $i$ is identical to version $i-1$.

如果操作类型为 $1$、$2$ 或 $3$,则将指定修改应用于版本 $i-1$,得到版本 $i$。类型 $4$ 的操作不会修改数组,因此版本 $i$ 与版本 $i-1$ 相同。AI (CI翻译)

The operations are encoded and must be processed in order. Their decoding depends on $\mathrm{lastans}$, which is updated after every operation of type $4$.

**操作经过编码,必须按顺序处理。**它们的解码依赖于 $\mathrm{lastans}$,而 $\mathrm{lastans}$ 会在每次类型 $4$ 的操作后更新。AI (CI翻译)

Help Willem answer every operation of type $4$.

请帮助 Willem 回答每个类型 $4$ 的操作。AI (CI翻译)

$^{\text{∗}}$An array $c$ is a subarray of an array $b$ if $c$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

$^{\text{∗}}$如果数组 $c$ 可以通过从数组 $b$ 的开头删除若干个(可以为零个或全部)元素,并从末尾删除若干个(可以为零个或全部)元素得到,则称数组 $c$ 是数组 $b$ 的子数组。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,q\le5\cdot10^5$) — the length of the array and the number of operations.

每个测试用例的第一行包含两个整数 $n$ 和 $q$($1\le n,q\le5\cdot10^5$)——数组长度和操作次数。AI (CI翻译)

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9\le a_i\le 10^9$) — the array in version $0$.

每个测试用例的第二行包含 $n$ 个整数 $a_1,a_2,\ldots,a_n$($-10^9\le a_i\le 10^9$)——版本 $0$ 中的数组。AI (CI翻译)

Each of the next $q$ lines describes one operation in one of the following encoded formats: The first integer on the line is the operation type.

接下来的 $q$ 行分别描述一个操作,操作采用以下编码格式之一。每行的第一个整数表示操作类型。AI (CI翻译)

  • $\texttt{1 u v x}$ ($0\le u,v \lt 2^{64}$, $-10^9\le x\le 10^9$);
  • $\texttt{2 u v}$ ($0\le u,v \lt 2^{64}$);
  • $\texttt{3 u v}$ ($0\le u,v \lt 2^{64}$);
  • $\texttt{4 u}$ ($0\le u \lt 2^{64}$).
  • $\texttt{1 u v x}$($0\le u,v \lt 2^{64}$,$-10^9\le x\le 10^9$);
  • $\texttt{2 u v}$($0\le u,v \lt 2^{64}$);
  • $\texttt{3 u v}$($0\le u,v \lt 2^{64}$);
  • $\texttt{4 u}$($0\le u \lt 2^{64}$)。
AI (CI翻译)

The operations are encoded and must be processed in order. Their decoding depends on a value $\mathrm{lastans}$.

操作经过编码,必须按顺序处理。它们的解码依赖于一个值 $\mathrm{lastans}$。AI (CI翻译)

Initially, $\mathrm{lastans}=0$. After answering an operation of type $4$, set $\mathrm{lastans}$ to the least non-negative residue of its answer modulo $2^{64}$. Operations of all other types leave $\mathrm{lastans}$ unchanged.

初始时,$\mathrm{lastans}=0$。回答一个类型 $4$ 的操作后,将 $\mathrm{lastans}$ 设为其答案对 $2^{64}$ 取模所得的最小非负剩余。其他类型的操作不会改变 $\mathrm{lastans}$。AI (CI翻译)

For every encoded coordinate $y$, define $d(y)=\left(\left(y\oplus\mathrm{lastans}\right)\bmod n\right)+1$. Here, $\oplus$ denotes the bitwise XOR operation.

对于每个编码后的坐标 $y$,定义 $d(y)=\left(\left(y\oplus\mathrm{lastans}\right)\bmod n\right)+1$。其中,$\oplus$ 表示按位异或运算。AI (CI翻译)

  • For an operation $\texttt{1 u v x}$, let $l=\min(d(u),d(v))$ and $r=\max(d(u),d(v))$. The value $x$ is not encoded.
  • For an operation $\texttt{2 u v}$ or $\texttt{3 u v}$, let $l=\min(d(u),d(v))$ and $r=\max(d(u),d(v))$.
  • For an operation $\texttt{4 u}$, let $p=d(u)$.
  • 对于操作 $\texttt{1 u v x}$,令 $l=\min(d(u),d(v))$,$r=\max(d(u),d(v))$。数值 $x$ 未经过编码。
  • 对于操作 $\texttt{2 u v}$ 或 $\texttt{3 u v}$,令 $l=\min(d(u),d(v))$,$r=\max(d(u),d(v))$。
  • 对于操作 $\texttt{4 u}$,令 $p=d(u)$。
AI (CI翻译)

The operation type is not encoded. Do not forget to update $\mathrm{lastans}$ after answering each operation of type $4$.

操作类型本身未经过编码。回答每个类型 $4$ 的操作后,不要忘记更新 $\mathrm{lastans}$。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $5\cdot10^5$.

保证所有测试用例中 $n$ 的总和不超过 $5\cdot10^5$。AI (CI翻译)

It is guaranteed that the sum of $q$ over all test cases does not exceed $5\cdot10^5$.

保证所有测试用例中 $q$ 的总和不超过 $5\cdot10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For every operation of type $4$, output a single integer — the maximum sum of a non-empty subarray of the sequence of the values at position $p$ over all versions preceding this operation.

对于每个类型 $4$ 的操作,输出一个整数——在该操作之前的所有版本中,位置 $p$ 的值所组成的序列的非空子数组的最大元素和。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, for the first operation, $\mathrm{lastans}=0$, so the encoded coordinate $1$ is decoded into position $2$. Only version $0$ is considered. The value at position $2$ is $-3$, hence the answer is $-3$.

在第一个测试用例中,对于第一次操作,$\mathrm{lastans}=0$,因此编码后的坐标 $1$ 被解码为位置 $2$。此时只考虑版本 $0$。位置 $2$ 的值为 $-3$,因此答案为 $-3$。AI (CI翻译)

Now $\mathrm{lastans}=2^{64}-3=18\,446\,744\,073\,709\,551\,613$. Therefore the encoded interval $[18\,446\,744\,073\,709\,551\,613,18\,446\,744\,073\,709\,551\,615]$ is decoded into $[1,3]$, and the encoded coordinate $18\,446\,744\,073\,709\,551\,612$ is decoded into position $2$.

现在 $\mathrm{lastans}=2^{64}-3=18\,446\,744\,073\,709\,551\,613$。因此,编码后的区间 $[18\,446\,744\,073\,709\,551\,613,18\,446\,744\,073\,709\,551\,615]$ 被解码为 $[1,3]$,而编码后的坐标 $18\,446\,744\,073\,709\,551\,612$ 被解码为位置 $2$。AI (CI翻译)

Before the second query, the values at position $2$ in versions $0$, $1$, and $2$ are $-3$, $-3$, and $3$, respectively. Their maximum non-empty subarray sum is $3$.

在第二次查询之前,版本 $0$、$1$ 和 $2$ 中位置 $2$ 的值分别为 $-3$、$-3$ 和 $3$。它们组成的序列的最大非空子数组和为 $3$。AI (CI翻译)

Before the third query, the values at position $2$ in versions $0,1,\ldots,5$ form sequence $[-3,-3,3,3,3,-4]$. The three consecutive values equal to $3$ form a subarray with sum $9$.

在第三次查询之前,版本 $0,1,\ldots,5$ 中位置 $2$ 的值组成序列 $[-3,-3,3,3,3,-4]$。连续的三个值 $3$ 构成了一个和为 $9$ 的子数组。AI (CI翻译)

Notice that the first, second, and third queries create versions $1$, $3$, and $6$, respectively, even though they do not change the array.

注意,第一次、第二次和第三次查询分别创建了版本 $1$、$3$ 和 $6$,尽管它们并不会改变数组。AI (CI翻译)

In the second test case, the first query asks about position $1$, so its answer is $1$. Then $\mathrm{lastans}=1$, and the encoded operation $\texttt{1 0 3 -1}$ is decoded into $\texttt{1 2 3 -1}$.

在第二个测试用例中,第一次查询询问位置 $1$,因此答案为 $1$。此时 $\mathrm{lastans}=1$,编码后的操作 $\texttt{1 0 3 -1}$ 被解码为 $\texttt{1 2 3 -1}$。AI (CI翻译)

Before the second query, the values at position $3$ in versions $0$, $1$, and $2$ are $3$, $3$, and $-1$, respectively, so the answer is $6$.

在第二次查询之前,版本 $0$、$1$ 和 $2$ 中位置 $3$ 的值分别为 $3$、$3$ 和 $-1$,因此答案为 $6$。AI (CI翻译)

Afterwards, $\mathrm{lastans}=6$. The encoded operations $\texttt{2 6 7}$ and $\texttt{3 7 4}$ are decoded into $\texttt{2 1 2}$ and $\texttt{3 2 3}$, respectively. Before the last query, the values at position $2$ in versions $0,1,\ldots,5$ form sequence $[-2,-2,-1,-1,1,1]$, whose maximum non-empty subarray sum is $2$.

之后,$\mathrm{lastans}=6$。编码后的操作 $\texttt{2 6 7}$ 和 $\texttt{3 7 4}$ 分别被解码为 $\texttt{2 1 2}$ 和 $\texttt{3 2 3}$。在最后一次查询之前,版本 $0,1,\ldots,5$ 中位置 $2$ 的值组成序列 $[-2,-2,-1,-1,1,1]$,其最大非空子数组和为 $2$。AI (CI翻译)

样例

Input #1
2
4 8
2 -3 1 -2
4 1
2 18446744073709551613 18446744073709551615
4 18446744073709551612
3 2 1
1 2 0 -4
4 2
2 8 10
4 10
3 6
1 -2 3
4 0
1 0 3 -1
4 3
2 6 7
3 7 4
4 7
Output #1
-3
3
9
4
1
6
2
InputCopy OutputCopy
题目信息
题号
2255E2
时间限制
3 s
内存限制
256 MB
标签
数据结构

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