Each operation adds generated values to up to 1000 vertices and asks for the sum on the path between two vertices.
Medium7TreeSegment treeNo attempts yetTime limit4sMemory limit256 MBMoney grows on a tree with N vertices. The vertices are numbered 0 to N−1 and vertex 0 is the root. Every vertex i other than vertex 0 has a parent p(i) with p(i)<i. At the start, vertex i holds v(i) monetary units.
An organization runs Q operations on the tree. Each operation has two steps.
Compute the answer to step 2 of every operation.
The limits are as follows.
The first line contains the number of vertices N. Each of the next N−1 lines contains two space separated integers p(i) and i describing one edge of the tree. The next line contains the starting amounts v(0),…,v(N−1), separated by spaces.
The next line contains the number of operations Q. Each of the next Q lines describes one operation with nine space separated integers in this order: K, x(1), y(1), A, B, C, D, u, v (0≤A,B,C,D<1000000007). Only x(1) and y(1) are given directly. The remaining values for 2≤i≤K come from
x(i)=(A⋅x(i−1)+B)modN
y(i)=(C⋅y(i−1)+D)mod1000000007
Print Q lines. Line j contains the answer to step 2 of operation j. Money added in step 1 stays in the vertex, so every operation computes its answer with the step 1 additions of all earlier operations and of its own step 1 already applied.
An answer can exceed the range of a 32-bit integer.
In the first example, operation 1 has A=C=1 and B=D=0, so the value 1 is added to vertex 1 one thousand times. The path between vertex 0 and vertex 2 passes through vertices 0, 1, 2, and the total amount of money in them is 1006.
In operation 2 the generated values are x(1)=0, y(1)=5, x(2)=1, y(2)=12, and the path between vertex 2 and vertex 3 passes through every vertex of the tree.
In operation 3, K=1, so A, B, C, D are not used.