CF2228E2.Amanojaku and Sequence (Hard Version)
省选/NOI-
通过率:0%
时间限制:4.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Tinkerbell of Inequality
— Taboo Japan Disentanglement
This is the hard version of the problem. The difference between the versions is that in this version, 1≤q≤3⋅105 and 1≤op≤2. You can hack only if you solved all versions of this problem.
For a sequence s, let ∣s∣ denote its length.
For a non-negative integer sequence c, define f(c) as the sum of the squares of its prefix sums: $$ f(c)=\sum_{i=1}{|c|}\left(\sum_{j=1}{i} c_j\right)^2. $$
Now define g(b,m) as follows.
Let b be an integer sequence such that bi≥−1 for every i, and let m be a non-negative integer. A non-negative integer sequence c is called valid for (b,m) if all of the following conditions hold:
- ∣c∣=∣b∣;
- ∑i=1∣c∣ci=m;
- for every 1≤i≤∣b∣, if bi≥0, then ci=bi; otherwise, if bi=−1, then ci may be any non-negative integer.
The value g(b,m) is defined as the sum of f(c) over all valid sequences c for (b,m). If there is no such sequence, then g(b,m)=0.
You are given an array a of length n, where ai≥−1, and q queries of the following two types:
- Given two integers p and v, set ap to v.
- Given three integers l, r, and m, compute g([al,al+1,…,ar],m) modulo 998244353, where [al,al+1,…,ar] denotes the subarray∗ of a from position l to position r.
∗An array a is a subarray of an array b if a 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.
不等式的 tinkerbelle
——禁忌日本解耦问题
这是该问题的困难版本。两个版本的区别在于:在此版本中,1≤q≤3⋅105 且 1≤op≤2。仅当您已解决该问题的所有版本时,才允许进行 hack。
对于一个序列 s,记 ∣s∣ 表示其长度。
对于一个非负整数序列 c,定义 f(c) 为其前缀和的平方之和:
f(c)=i=1∑∣c∣(j=1∑icj)2.
现在定义 g(b,m) 如下。
设 b 是一个整数序列,满足对每个 i 都有 bi≥−1;设 m 是一个非负整数。一个非负整数序列 c 称为关于 (b,m) 的合法序列,当且仅当满足以下全部条件:
- ∣c∣=∣b∣;
- ∑i=1∣c∣ci=m;
- 对每个 1≤i≤∣b∣,若 bi≥0,则 ci=bi;否则(即 bi=−1),ci 可取任意非负整数。
函数 g(b,m) 定义为所有关于 (b,m) 的合法序列 c 对应的 f(c) 之和。若不存在这样的序列,则 g(b,m)=0。
给定一个长度为 n 的数组 a,其中每个 ai≥−1,以及 q 个如下两种类型的查询:
- 给定两个整数 p 和 v,将 ap 赋值为 v;
- 给定三个整数 l、r 和 m,计算 g([al,al+1,…,ar],m) 对 998244353 取模的结果,其中 [al,al+1,…,ar] 表示数组 a 从位置 l 到位置 r 的子数组∗。
∗ 若数组 a 可通过从数组 b 的开头删除若干(可能为零或全部)元素、并从结尾删除若干(可能为零或全部)元素而得到,则称 a 是 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.
For each test case, the first line contains two integers n and q (1≤n≤3⋅105, 1≤q≤3⋅105).
The second line contains n integers a1,a2,…,an (−1≤ai≤106).
Then q lines follow. Each line describes a query in one of the following format. The first integer op is 1 or 2.
- 1pv: set ap to v (1≤p≤n, −1≤v≤106);
- 2lrm: compute g([al,al+1,…,ar],m) modulo 998244353 (1≤l≤r≤n, 0≤m≤106).
It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.
It is guaranteed that the sum of q over all test cases does not exceed 3⋅105.
每个测试包含多个测试用例。第一行包含测试用例数量 t(1≤t≤104)。随后是各测试用例的描述。
对于每个测试用例,第一行包含两个整数 n 和 q(1≤n≤3⋅105,1≤q≤3⋅105)。
第二行包含 n 个整数 a1,a2,…,an(−1≤ai≤106)。
接下来是 q 行,每行描述一个查询,查询格式为以下两种之一。第一个整数 op 为 1 或 2:
- 1pv:将 ap 赋值为 v(1≤p≤n,−1≤v≤106);
- 2lrm:计算 g([al,al+1,…,ar],m) 对 998244353 取模的结果(1≤l≤r≤n,0≤m≤106)。
保证所有测试用例的 n 之和不超过 3⋅105。
保证所有测试用例的 q 之和不超过 3⋅105。
输出格式
For each test case, for every query of the second type, output the value of g([al,al+1,…,ar],m) modulo 998244353 on a line.
对于每个测试用例,对每一个第二类查询,输出 g([al,al+1,…,ar],m) 对 998244353 取模的结果,每行一个。
输入输出样例
输入#1
2 5 7 4 -1 7 6 -1 2 2 2 2 1 3 8 2 3 3 0 2 4 5 8 1 5 7 2 3 5 21 2 3 5 22 4 6 -1 -1 -1 -1 2 1 1 4 2 1 2 5 2 1 3 6 2 1 4 7 1 3 3 2 1 4 5
输出#1
4 0 100 701 0 16 205 1736 12180 286
说明/提示
In the first test case:
For the first query, l=r=2 and m=2. The subarray is [a2]=[−1], so the only valid sequence is c=[2]. Thus, the answer is 22=4.
For the fourth query, l=4, r=5, and m=8. The subarray is [a4,a5]=[6,−1]. Since c1 is fixed at 6 and c1+c2=m, we obtain c=[6,2]. The prefix sums are 6 and 8, giving $$ f(c)=62+(6+2)2=36+64=100. $$
In the second test case:
For the second query, the subarray is [a1,a2]=[−1,−1]. All valid sequences satisfy c1+c2=5 with c1,c2≥0, namely [0,5],[1,4],…,[5,0]. Summing f(c) over all such sequences yields 205.
在第一个测试用例中:
对于第一个查询,l=r=2 且 m=2。子数组为 [a2]=[−1],因此唯一有效的序列是 c=[2]。故答案为 22=4。
对于第四个查询,l=4,r=5,且 m=8。子数组为 [a4,a5]=[6,−1]。由于 c1 固定为 6,且 c1+c2=m,可得 c=[6,2]。其前缀和分别为 6 和 8,因此
f(c)=62+(6+2)2=36+64=100.
在第二个测试用例中:
对于第二个查询,子数组为 [a1,a2]=[−1,−1]。所有有效的序列均满足 c1+c2=5 且 c1,c2≥0,即 [0,5],[1,4],…,[5,0]。对所有此类序列求 f(c) 的和,结果为 205。
输入解题思路,AI测评打分。不知道怎么写?