CF2255E2.What Will Remain at the End? (Hard Version)
NOI/NOI+/CTSC
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 [−109,109]. You can make hacks only if both versions of the problem are solved.
Before her final sortie, Chtholly asks Willem three questions.
The second is this: what will remain if the sky truly reaches its end?
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.
The initial contents of the chronicle form an array a1,a2,…,an, called version 0. Chtholly then performs q operations. For each 1≤i≤q, the i-th operation creates a new version i from version i−1.
Each operation has one of the following four types:
- 1 l r x: set ak←x for every l≤k≤r.
- 2 l r: set ak←−ak for every l≤k≤r.
- 3 l r: set ak←max(ak,0) for every l≤k≤r.
- 4 p: consider the value at position p in every previous version 0,1,…,i−1. Let these values be b0,b1,…,bi−1. Find the maximum sum over all non-empty subarrays∗ of this sequence.
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.
The operations are encoded and must be processed in order. Their decoding depends on lastans, which is updated after every operation of type 4.
Help Willem answer every operation of type 4.
∗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.
这是该问题的困难版本。两个版本的唯一区别在于初始数组和类型 1 操作中 x 的允许取值范围。在本版本中,这些值可以是区间 [−109,109] 内的任意整数。仅当两个版本的问题均被解决时,才允许进行 Hack。
在她最后一次出击前,Chtholly 向 Willem 提出了三个问题。
第二个问题是:若天空真的抵达尽头,将剩下什么?
Willem 无法直接回答她。相反,他打开了一本记载着 n 条记录的编年史,记录编号从 1 到 n。每条记录保存一个整数:正值代表希望,负值代表绝望。
编年史的初始内容构成一个数组 a1,a2,…,an,称为第 0 版本。随后 Chtholly 执行 q 次操作。对每个 1≤i≤q,第 i 次操作基于第 i−1 版本生成一个新的第 i 版本。
每次操作属于以下四种类型之一:
- 1 l r x:对每个满足 l≤k≤r 的 k,令 ak←x。
- 2 l r:对每个满足 l≤k≤r 的 k,令 ak←−ak。
- 3 l r:对每个满足 l≤k≤r 的 k,令 ak←max(ak,0)。
- 4 p:考虑位置 p 在所有先前版本 0,1,…,i−1 中的取值。设这些值为 b0,b1,…,bi−1。求该序列的所有非空子数组∗ 的最大和。
若操作类型为 1、2 或 3,则对第 i−1 版本应用指定修改以得到第 i 版本。类型 4 的操作不修改数组,因此第 i 版本与第 i−1 版本完全相同。
这些操作经过编码,必须按顺序处理。其解码依赖于变量 lastans,该变量在每次类型 4 的操作后更新。
请帮助 Willem 回答每一次类型 4 的操作。
∗ 若数组 c 可通过对数组 b 删除开头若干(可能为零或全部)元素及结尾若干(可能为零或全部)元素而得到,则称 c 是 b 的一个子数组。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and q (1≤n,q≤5⋅105) — the length of the array and the number of operations.
The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109) — the array in version 0.
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.
- 1 u v x (0≤u,v<264, −109≤x≤109);
- 2 u v (0≤u,v<264);
- 3 u v (0≤u,v<264);
- 4 u (0≤u<264).
The operations are encoded and must be processed in order. Their decoding depends on a value lastans.
Initially, lastans=0. After answering an operation of type 4, set lastans to the least non-negative residue of its answer modulo 264. Operations of all other types leave lastans unchanged.
For every encoded coordinate y, define d(y)=((y⊕lastans)modn)+1. Here, ⊕ denotes the bitwise XOR operation.
- For an operation 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 2 u v or 3 u v, let l=min(d(u),d(v)) and r=max(d(u),d(v)).
- For an operation 4 u, let p=d(u).
The operation type is not encoded. Do not forget to update lastans after answering each operation of type 4.
It is guaranteed that the sum of n over all test cases does not exceed 5⋅105.
It is guaranteed that the sum of q over all test cases does not exceed 5⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 q(1≤n,q≤5⋅105)——分别表示数组长度和操作次数。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(−109≤ai≤109)——表示版本 0 的初始数组。
接下来的 q 行,每行描述一个操作,格式为以下编码形式之一:每行第一个整数为操作类型。
- 1 u v x(其中 0≤u,v<264,−109≤x≤109);
- 2 u v(其中 0≤u,v<264);
- 3 u v(其中 0≤u,v<264);
- 4 u(其中 0≤u<264)。
这些操作是经过编码的,必须按顺序处理。其解码依赖于一个值 lastans。
初始时,lastans=0。在回答完类型为 4 的操作后,将 lastans 更新为该答案对 264 取模所得的最小非负剩余。其余所有类型的操作均不改变 lastans 的值。
对于任意编码坐标 y,定义 d(y)=((y⊕lastans)modn)+1。其中,⊕ 表示按位异或运算。
- 对于操作 1 u v x,令 l=min(d(u),d(v)),r=max(d(u),d(v));值 x 未被编码。
- 对于操作 2 u v 或 3 u v,令 l=min(d(u),d(v)),r=max(d(u),d(v))。
- 对于操作 4 u,令 p=d(u)。
操作类型本身未被编码。请勿忘记在每次回答类型为 4 的操作后更新 lastans。
保证所有测试用例的 n 之和不超过 5⋅105。
保证所有测试用例的 q 之和不超过 5⋅105。
输出格式
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 处的值所构成序列的非空子数组的最大和。
输入输出样例
输入#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
输出#1
-3 3 9 4 1 6 2
说明/提示
In the first test case, for the first operation, 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.
Now lastans=264−3=18446744073709551613. Therefore the encoded interval [18446744073709551613,18446744073709551615] is decoded into [1,3], and the encoded coordinate 18446744073709551612 is decoded into position 2.
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.
Before the third query, the values at position 2 in versions 0,1,…,5 form sequence [−3,−3,3,3,3,−4]. The three consecutive values equal to 3 form a subarray with sum 9.
Notice that the first, second, and third queries create versions 1, 3, and 6, respectively, even though they do not change the array.
In the second test case, the first query asks about position 1, so its answer is 1. Then lastans=1, and the encoded operation 1 0 3 -1 is decoded into 1 2 3 -1.
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.
Afterwards, lastans=6. The encoded operations 2 6 7 and 3 7 4 are decoded into 2 1 2 and 3 2 3, respectively. Before the last query, the values at position 2 in versions 0,1,…,5 form sequence [−2,−2,−1,−1,1,1], whose maximum non-empty subarray sum is 2.
在第一个测试用例中,对于第一次操作,lastans=0,因此编码坐标 1 被解码为位置 2。此时仅考虑版本 0。位置 2 处的值为 −3,故答案为 −3。
此时 lastans=264−3=18446744073709551613。因此,编码区间 [18446744073709551613,18446744073709551615] 被解码为 [1,3],而编码坐标 18446744073709551612 被解码为位置 2。
在第二次查询之前,版本 0、1 和 2 中位置 2 处的值分别为 −3、−3 和 3,它们的最大非空子数组和为 3。
在第三次查询之前,版本 0,1,…,5 中位置 2 处的值构成序列 [−3,−3,3,3,3,−4]。其中三个连续相等的值 3 构成一个子数组,其和为 9。
注意:第一次、第二次和第三次查询分别创建了版本 1、3 和 6,尽管它们并未改变数组。
在第二个测试用例中,第一次查询询问位置 1,因此其答案为 1。随后 lastans=1,编码操作 1 0 3 -1 被解码为 1 2 3 -1。
在第二次查询之前,版本 0、1 和 2 中位置 3 处的值分别为 3、3 和 −1,因此答案为 6。
此后,lastans=6。编码操作 2 6 7 和 3 7 4 分别被解码为 2 1 2 和 3 2 3。在最后一次查询之前,版本 0,1,…,5 中位置 2 处的值构成序列 [−2,−2,−1,−1,1,1],其最大非空子数组和为 2。
输入解题思路,AI测评打分。不知道怎么写?