#P7445. 「EZEC-7」线段树
「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 of length , initially all .
Then Bob performs range-add operations. Each operation randomly selects one sub-interval uniformly from all sub-intervals of to apply the operation. The added value each time is a uniformly random integer in .
After all operations, you need to obtain the value at every position.
Since Bob likes segment trees, he quickly wrote a segment tree over to solve this problem, using lazy tags to handle range addition.
While coding, Bob suddenly thought of two ways to reduce the number of operations (pushing down lazy tags):
-
Do not during updates, and do all operations at the end at once (i.e., the function).
-
When reaches a node, if the node's lazy tag is , then do not .
Now Bob wants to know the expected number of 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 .
Input Format
One line with three integers .
Output Format
One integer: the expected number of operations modulo .
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 nodes: .
Only the node may perform .
When the operation is , the root node's lazy tag is , so during it will at the root; while after , since the root node's lazy tag is , it will not .
The other 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 .
Therefore, among total cases, only case can , so the expected number is .
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 , then the root node's lazy tag is , so during it still will not .
Constraints.
This problem uses bundled testdata.
| Subtask ID | Score | Time Limit | |||
|---|---|---|---|---|---|
For of the data, , , .
Definition of a Segment Tree.
A segment tree is a binary tree where each node stores a segment. The root node stores . For each node, if the segment it stores is and , let , then its left and right child nodes store and , respectively. If , then it is a leaf node.
Translated by ChatGPT 5