#P7445. 「EZEC-7」线段树

    ID: 8269 远端评测题 500~3000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>O2优化生成函数期望快速傅里叶变换 FFT快速数论变换 NTT洛谷月赛

「EZEC-7」线段树

Background

Bob likes segment trees.

Problem Description

If you do not know what a segment tree is, you can read the definition in the Hint section.

Bob is given a sequence a1,2,,na_{1,2,\cdots,n} of length nn, initially all 00.

Then Bob performs mm range-add operations. Each operation randomly selects one sub-interval uniformly from all n(n+1)2\dfrac{n(n+1)}{2} sub-intervals of [1,n][1,n] to apply the operation. The added value each time is a uniformly random integer in [1,V][-1,V].

After all mm operations, you need to obtain the value at every position.

Since Bob likes segment trees, he quickly wrote a segment tree over [1,n][1,n] to solve this problem, using lazy tags to handle range addition.

While coding, Bob suddenly thought of two ways to reduce the number of Pushdown\mathrm{Pushdown} operations (pushing down lazy tags):

  • Do not Pushdown\mathrm{Pushdown} during updates, and do all Pushdown\mathrm{Pushdown} operations at the end at once (i.e., the Pushall\mathrm{Pushall} function).

  • When Pushall\mathrm{Pushall} reaches a node, if the node's lazy tag is 00, then do not Pushdown\mathrm{Pushdown}.

Now Bob wants to know the expected number of Pushdown\mathrm{Pushdown} operations, but he cannot compute it, so he asks you.

Below is the segment tree pseudocode written by Bob (the array tag stores lazy tags):

$\displaystyle \begin{array}{l} \mathrm{pushdown\_counter}\leftarrow 0\\ \\ \textbf{function }\mathrm{Update(Node},l,r,ql,qr,Delta)\\ \qquad \textbf{if } [l,r]\cap [ql,qr] = \varnothing \textbf{ then}\\ \qquad \qquad \textbf{return}\\ \qquad \textbf{end if}\\ \qquad \textbf{if }[l,r] \subseteq [ql,qr] \textbf{ then}\\ \qquad \qquad \mathrm{tag[Node]\leftarrow tag[Node]}+Delta\\ \qquad \qquad \textbf{return}\\ \qquad \textbf{end if}\\ \qquad mid\leftarrow \lfloor\dfrac{l+r}{2}\rfloor\\ \qquad \mathrm{Update(LeftChild},l,mid,ql,qr,Delta)\\ \qquad \mathrm{Update(RightChild},mid+1,r,ql,qr,Delta)\\ \textbf{end function}\\ \\ \textbf{function }\mathrm{Pushdown(Node)} \\ \qquad \mathrm{tag[LeftChild]\leftarrow tag[LeftChild]+tag[Node]}\\ \qquad \mathrm{tag[RightChild]\leftarrow tag[RightChild]+tag[Node]}\\ \qquad \mathrm{pushdown\_counter}\leftarrow \mathrm{pushdown\_counter}+1\\ \textbf{end function}\\ \\ \textbf{function }\mathrm{Pushall(Node},l,r)\\ \qquad \textbf{if } \mathrm{Node\ is\ Leaf} \textbf{ then}\\ \qquad \qquad \textbf{return}\\ \qquad \textbf{end if}\\ \qquad \textbf{if } \mathrm{tag[Node] \not= 0} \textbf{ then}\\ \qquad \qquad \mathrm{Pushdown(Node)}\\ \qquad \textbf{end if}\\ \qquad mid\leftarrow \lfloor\dfrac{l+r}{2}\rfloor\\ \qquad \mathrm{Pushall(LeftChild},l,mid)\\ \qquad \mathrm{Pushall(RightChild},mid+1,r)\\ \textbf{end function} \end{array}$

In other words, you need to help Bob compute the expected value of pushdown_counter.

Output the answer modulo 998244353998244353.

Input Format

One line with three integers n,m,Vn,m,V.

Output Format

One integer: the expected number of Pushdown\mathrm{Pushdown} operations modulo 998244353998244353.

2 1 0

166374059

2 2 1

813384288

3 2 1

462150164

100 114 514

718571152

Hint

Sample Explanation #1.

The whole segment tree has only 33 nodes: [1,2],[1,1],[2,2][1,2],[1,1],[2,2].

Only the node [1,2][1,2] may perform Pushdown\mathrm{Pushdown}.

When the operation is Update(1,2,1)\mathrm{Update(1,2,-1)}, the root node's lazy tag is 1-1, so during Pushall\mathrm{Pushall} it will Pushdown\mathrm{Pushdown} at the root; while after Update(1,2,0)\mathrm{Update(1,2,0)}, since the root node's lazy tag is 00, it will not Pushdown\mathrm{Pushdown}.

The other 44 operations all place the lazy tag on leaf nodes, i.e. $\mathrm{Update(1,1,-1)},\mathrm{Update(1,1,0)},\mathrm{Update(2,2,-1)},\mathrm{Update(2,2,0)}$, so they will not Pushdown\mathrm{Pushdown}.

Therefore, among 66 total cases, only 11 case can Pushdown\mathrm{Pushdown}, so the expected number is 16\dfrac{1}{6}.

Sample Explanation #2.

Since there are too many cases, they are not explained one by one; only one case is explained.

If the two executed operations are Update(1,2,1),Update(1,2,1)\mathrm{Update(1,2,-1)},\mathrm{Update(1,2,1)}, then the root node's lazy tag is 00, so during Pushall\mathrm{Pushall} it still will not Pushdown\mathrm{Pushdown}.


Constraints.

This problem uses bundled testdata.

Subtask ID nn\le mm\le VV Score Time Limit / ms\text{ / ms}
11 44 2\le 2 33 500500
22 300300 300\le 300 77
33 10001000 1000\le 1000 1010
44 300300 10510^5 =1000=1000 1515 20002000
55 10510^5 0\le 0 1010 30003000
66 =1000=1000 1515
77 998244350\le 998244350 4040

For 100%100\% of the data, 1n1051 \le n \le 10^5, 1m1051 \le m \le 10^5, 1V998244350-1 \le V \le 998244350.


Definition of a Segment Tree.

A segment tree is a binary tree where each node stores a segment. The root node stores [1,n][1,n]. For each node, if the segment it stores is [l,r][l,r] and lrl\not= r, let m=l+r2m = \lfloor \dfrac{l+r}{2} \rfloor, then its left and right child nodes store [l,m][l,m] and [m+1,r][m+1,r], respectively. If l=rl=r, then it is a leaf node.

Translated by ChatGPT 5