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⋅1051\le q \le 3\cdot 10^5 and 1≤op≤21\le\mathrm{op}\le2. You can hack only if you solved all versions of this problem.

For a sequence ss, let ∣s∣|s| denote its length.

For a non-negative integer sequence cc, define f(c)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)g(b,m) as follows.

Let bb be an integer sequence such that bi≥−1b_i\ge -1 for every ii, and let mm be a non-negative integer. A non-negative integer sequence cc is called valid for (b,m)(b,m) if all of the following conditions hold:

  • ∣c∣=∣b∣|c|=|b|;
  • ∑i=1∣c∣ci=m\sum_{i=1}^{|c|} c_i=m;
  • for every 1≤i≤∣b∣1\le i\le |b|, if bi≥0b_i\ge 0, then ci=bic_i=b_i; otherwise, if bi=−1b_i=-1, then cic_i may be any non-negative integer.

The value g(b,m)g(b,m) is defined as the sum of f(c)f(c) over all valid sequences cc for (b,m)(b,m). If there is no such sequence, then g(b,m)=0g(b,m)=0.

You are given an array aa of length nn, where ai≥−1a_i\ge -1, and qq queries of the following two types:

  • Given two integers pp and vv, set apa_p to vv.
  • Given three integers ll, rr, and mm, compute g([al,al+1,…,ar],m)g([a_l,a_{l+1},\ldots,a_r],m) modulo 998 244 353998\,244\,353, where [al,al+1,…,ar][a_l,a_{l+1},\ldots,a_r] denotes the subarray∗^{\text{∗}} of aa from position ll to position rr.

∗^{\text{∗}}An array aa is a subarray of an array bb if aa can be obtained from bb 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⋅1051\le q \le 3\cdot 10^5 且 1≤op≤21\le\mathrm{op}\le2。仅当您已解决该问题的所有版本时,才允许进行 hack。

对于一个序列 ss,记 ∣s∣|s| 表示其长度。

对于一个非负整数序列 cc,定义 f(c)f(c) 为其前缀和的平方之和:

f(c)=∑i=1∣c∣(∑j=1icj)2.f(c)=\sum_{i=1}^{|c|}\left(\sum_{j=1}^{i} c_j\right)^2.

现在定义 g(b,m)g(b,m) 如下。

设 bb 是一个整数序列,满足对每个 ii 都有 bi≥−1b_i\ge -1;设 mm 是一个非负整数。一个非负整数序列 cc 称为关于 (b,m)(b,m) 的合法序列,当且仅当满足以下全部条件:

  • ∣c∣=∣b∣|c|=|b|;
  • ∑i=1∣c∣ci=m\sum_{i=1}^{|c|} c_i=m;
  • 对每个 1≤i≤∣b∣1\le i\le |b|,若 bi≥0b_i\ge 0,则 ci=bic_i=b_i;否则(即 bi=−1b_i=-1),cic_i 可取任意非负整数。

函数 g(b,m)g(b,m) 定义为所有关于 (b,m)(b,m) 的合法序列 cc 对应的 f(c)f(c) 之和。若不存在这样的序列,则 g(b,m)=0g(b,m)=0。

给定一个长度为 nn 的数组 aa,其中每个 ai≥−1a_i\ge -1,以及 qq 个如下两种类型的查询:

  • 给定两个整数 pp 和 vv,将 apa_p 赋值为 vv;
  • 给定三个整数 ll、rr 和 mm,计算 g([al,al+1,…,ar],m)g([a_l,a_{l+1},\ldots,a_r],m) 对 998 244 353998\,244\,353 取模的结果,其中 [al,al+1,…,ar][a_l,a_{l+1},\ldots,a_r] 表示数组 aa 从位置 ll 到位置 rr 的子数组∗^{\text{∗}}。

∗^{\text{∗}} 若数组 aa 可通过从数组 bb 的开头删除若干(可能为零或全部)元素、并从结尾删除若干(可能为零或全部)元素而得到,则称 aa 是 bb 的一个子数组。

输入格式

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

For each test case, the first line contains two integers nn and qq (1≤n≤3⋅1051\leq n\leq 3\cdot 10^5, 1≤q≤3⋅1051\leq q\leq 3\cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (−1≤ai≤106-1\leq a_i\leq 10^6).

Then qq lines follow. Each line describes a query in one of the following format. The first integer op\textrm{op} is 11 or 22.

  • 1 p v1\,p\,v: set apa_p to vv (1≤p≤n1\leq p\leq n, −1≤v≤106-1\leq v\leq 10^6);
  • 2 l r m2\,l\,r\,m: compute g([al,al+1,…,ar],m)g([a_l,a_{l+1},\ldots,a_r],m) modulo 998 244 353998\,244\,353 (1≤l≤r≤n1\leq l\leq r\leq n, 0≤m≤1060\leq m\leq 10^6).

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053\cdot 10^5.

It is guaranteed that the sum of qq over all test cases does not exceed 3⋅1053\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

对于每个测试用例,第一行包含两个整数 nn 和 qq(1≤n≤3⋅1051\leq n\leq 3\cdot 10^5,1≤q≤3⋅1051\leq q\leq 3\cdot 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(−1≤ai≤106-1\leq a_i\leq 10^6)。

接下来是 qq 行,每行描述一个查询,查询格式为以下两种之一。第一个整数 op\textrm{op} 为 11 或 22:

  • 1 p v1\,p\,v:将 apa_p 赋值为 vv(1≤p≤n1\leq p\leq n,−1≤v≤106-1\leq v\leq 10^6);
  • 2 l r m2\,l\,r\,m:计算 g([al,al+1,…,ar],m)g([a_l,a_{l+1},\ldots,a_r],m) 对 998 244 353998\,244\,353 取模的结果(1≤l≤r≤n1\leq l\leq r\leq n,0≤m≤1060\leq m\leq 10^6)。

保证所有测试用例的 nn 之和不超过 3⋅1053\cdot 10^5。

保证所有测试用例的 qq 之和不超过 3⋅1053\cdot 10^5。

输出格式

For each test case, for every query of the second type, output the value of g([al,al+1,…,ar],m)g([a_l,a_{l+1},\ldots,a_r],m) modulo 998 244 353998\,244\,353 on a line.

对于每个测试用例,对每一个第二类查询,输出 g([al,al+1,…,ar],m)g([a_l,a_{l+1},\ldots,a_r],m) 对 998 244 353998\,244\,353 取模的结果,每行一个。

输入输出样例

  • 输入#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=2l=r=2 and m=2m=2. The subarray is [a2]=[−1][a_2]=[-1], so the only valid sequence is c=[2]c=[2]. Thus, the answer is 22=42^2=4.

For the fourth query, l=4l=4, r=5r=5, and m=8m=8. The subarray is [a4,a5]=[6,−1][a_4,a_5]=[6,-1]. Since c1c_1 is fixed at 66 and c1+c2=mc_1+c_2=m, we obtain c=[6,2]c=[6,2]. The prefix sums are 66 and 88, 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][a_1,a_2]=[-1,-1]. All valid sequences satisfy c1+c2=5c_1+c_2=5 with c1,c2≥0c_1,c_2\ge 0, namely [0,5],[1,4],…,[5,0][0,5],[1,4],\ldots,[5,0]. Summing f(c)f(c) over all such sequences yields 205205.

在第一个测试用例中:

对于第一个查询,l=r=2l=r=2 且 m=2m=2。子数组为 [a2]=[−1][a_2]=[-1],因此唯一有效的序列是 c=[2]c=[2]。故答案为 22=42^2=4。

对于第四个查询,l=4l=4,r=5r=5,且 m=8m=8。子数组为 [a4,a5]=[6,−1][a_4,a_5]=[6,-1]。由于 c1c_1 固定为 66,且 c1+c2=mc_1+c_2=m,可得 c=[6,2]c=[6,2]。其前缀和分别为 66 和 88,因此

f(c)=62+(6+2)2=36+64=100.f(c)=6^2+(6+2)^2=36+64=100.

在第二个测试用例中:

对于第二个查询,子数组为 [a1,a2]=[−1,−1][a_1,a_2]=[-1,-1]。所有有效的序列均满足 c1+c2=5c_1+c_2=5 且 c1,c2≥0c_1,c_2\ge 0,即 [0,5],[1,4],…,[5,0][0,5],[1,4],\ldots,[5,0]。对所有此类序列求 f(c)f(c) 的和,结果为 205205。

输入解题思路,AI测评打分。不知道怎么写?

首页