#P3384. 【模板】重链剖分 / 树链剖分
【模板】重链剖分 / 树链剖分
Description
As stated, given a tree with nodes (connected and acyclic), each node stores a value. Support the following operations:
1 x y z: add to the value of every node on the shortest path from to in the tree.2 x y: query the sum of values of all nodes on the shortest path from to in the tree.3 x z: add to the value of every node in the subtree rooted at .4 x: query the sum of values of all nodes in the subtree rooted at .
Input Format
The first line contains positive integers , representing the number of nodes in the tree, the number of operations, the index of the root, and the modulus, respectively (i.e., all outputs are taken modulo this).
The next line contains non-negative integers, representing the initial value at each node in order.
The next lines each contain two integers , indicating an edge between nodes and (guaranteed acyclic and connected).
The next lines each contain several positive integers; each line represents one operation.
Output Format
Output several lines, each being the result of operation type 2 or 4 (taken modulo ), in order.
5 5 2 24
7 3 7 8 0
1 2
1 5
3 1
4 1
3 4 2
3 2 2
4 5
1 5 1 3
2 1 3
2
21
Hint
[Constraints]
For of the testdata: , .
For of the testdata: , .
For of the testdata: , , , . All input numbers are within the int range.
[Sample Explanation]
The structure of the tree is as follows:

The operations are as follows:

Therefore, the outputs should be and in order.
Translated by ChatGPT 5
京公网安备 11011102002149号