#P4458. [BJOI2018] 链上二次求和
[BJOI2018] 链上二次求和
Description
There is a chain of length (an undirected graph where , there is an edge between node and node ). Each node has an integer weight; the weight of the -th node is . There are operations, each as follows:
Operation 1 (modify): Given two nodes on the chain and an integer , add to the weight of every node on the unique simple path from to on the chain.
Operation 2 (query): Given two positive integers , compute the sum of node-weight sums over all simple paths whose number of nodes is at least and at most . Since the answer can be very large, output the result modulo the prime .
The node-weight sum of a simple path with nodes is the sum of the weights of all nodes (including endpoints) on that path. In this problem, you need to compute the sum of this value over all simple paths that meet the requirement.
Since the graph is undirected, paths are undirected as well; that is, the path from node to node is the same as the path from node to node , so do not double count.
Input Format
The first line contains two positive integers , denoting the number of nodes and the number of operations.
The second line contains integers; the -th number is the initial weight of the -th node.
Each of the next lines is in the form 1 u v d or 2 l r, indicating an Operation 1 (modify) or Operation 2 (query), respectively.
Output Format
For each query, output one line with one integer, which is the answer modulo .
5 5
1 1 1 1 1
2 5 5
2 1 2
1 1 2 2
2 1 1
1 1 5 3
5
13
9
Hint
Sample Explanation:
There is exactly simple path with nodes, and its node-weight sum is , so the first query outputs .
There are simple paths with node, each with node-weight sum ; there are simple paths with nodes, each with node-weight sum , so the second query outputs .
After adding to the weights of nodes and , the simple paths with node have node-weight sums , , , , , so the third query outputs .
Constraints:
Let the number of Operation 1 (modify) be .
For all testdata, it is guaranteed that , , , .
, , , .
For the detailed scale and conventions of each test point, see the table below.

Translated by ChatGPT 5
京公网安备 11011102002149号