#SGM0000081. Gán hàm tại đỉnh, hợp hàm trên đường đi (Vertex Set Path Composite)
Gán hàm tại đỉnh, hợp hàm trên đường đi (Vertex Set Path Composite)
Vertex Set Path Composite
Source: Library Checker
Version: Phuoc Hung OJ Extended
Problem
Each vertex of a tree stores an affine function modulo . Support replacing the function at one vertex and evaluating the composition of functions along the directed path from to , in path order.
Input
The first line contains and . The next lines contain pairs describing . The next lines contain 0-based edges. Each query is one of:
0 p c d: replace the function at vertex with .1 u v x: starting with , apply the functions of the vertices on the path from to in that exact order.
Output
For each query of type 1, print the resulting value modulo .
Subtasks
Subtask 1 (20%)
- The tree size and number of operations are at most 20.
- All other conditions are the same as Subtask 3.
Subtask 2 (30%)
- The tree size and number of operations are at most 2000.
- All other conditions are the same as Subtask 3.
Subtask 3 (50%)
- .
- Vertex indices are 0-based.
- Every coefficient and query value belongs to .
Example
Input
1 5
934920615 566121894
1 0 0 809158230
0 0 299738720 566523253
1 0 0 364937308
0 0 693007693 193053012
0 0 704915724 242410315
Output
135724921
823408647
Explanation
With one vertex, a type-1 query simply applies the current affine function stored at vertex 0 to the supplied value.