| Codeforces Round 1098 (Div. 2) |
|---|
| Finished |
This is the hard version of the problem. The difference between the versions is that in this version, $$$1\le q \le 3\cdot 10^5$$$ and $$$1\le\mathrm{op}\le2$$$. 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 $$$b_i\ge -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:
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 $$$a_i\ge -1$$$, and $$$q$$$ queries of the following two types:
$$$^{\text{∗}}$$$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.
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.
For each test case, the first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\leq n\leq 3\cdot 10^5$$$, $$$1\leq q\leq 3\cdot 10^5$$$).
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$-1\leq a_i\leq 10^6$$$).
Then $$$q$$$ lines follow. Each line describes a query in one of the following format. The first integer $$$\textrm{op}$$$ is $$$1$$$ or $$$2$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\cdot 10^5$$$.
It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$3\cdot 10^5$$$.
For each test case, for every query of the second type, output the value of $$$g([a_l,a_{l+1},\ldots,a_r],m)$$$ modulo $$$998\,244\,353$$$ on a line.
25 74 -1 7 6 -12 2 2 21 3 82 3 3 02 4 5 81 5 72 3 5 212 3 5 224 6-1 -1 -1 -12 1 1 42 1 2 52 1 3 62 1 4 71 3 32 1 4 5
40100701016205173612180286
In the first test case:
For the first query, $$$l=r=2$$$ and $$$m=2$$$. The subarray is $$$[a_2]=[-1]$$$, so the only valid sequence is $$$c=[2]$$$. Thus, the answer is $$$2^2=4$$$.
For the fourth query, $$$l=4$$$, $$$r=5$$$, and $$$m=8$$$. The subarray is $$$[a_4,a_5]=[6,-1]$$$. Since $$$c_1$$$ is fixed at $$$6$$$ and $$$c_1+c_2=m$$$, we obtain $$$c=[6,2]$$$. The prefix sums are $$$6$$$ and $$$8$$$, giving $$$$$$ f(c)=6^2+(6+2)^2=36+64=100. $$$$$$
In the second test case:
For the second query, the subarray is $$$[a_1,a_2]=[-1,-1]$$$. All valid sequences satisfy $$$c_1+c_2=5$$$ with $$$c_1,c_2\ge 0$$$, namely $$$[0,5],[1,4],\ldots,[5,0]$$$. Summing $$$f(c)$$$ over all such sequences yields $$$205$$$.
| Name |
|---|


