LCA Problem Revisited Input file: stdin Output file: stdout Time limit: 7 sec Memory…
- User
LCA Problem Revisited Input file: stdin
Output file: stdout
Time limit: 7 sec
Memory limit: 256 mb
A hanging tree is given, containing n (1 \le n \le 10^5) vertices, numbered from 0 to n - 1. It is required to answer m (1 \le m \le 10^7) queries about the lowest common ancestor for a pair of vertices.
The queries are generated as follows. Numbers a_1, a_2 are given and numbers x, y, z. Numbers a_3, \ldots, a_{2m} are generated as follows: a_i = (x \cdot a_{i - 2} + y \cdot a_{i - 1} + z) \mod n. The first query is of the form (a_1, a_2). If the answer to the i - 1-th query is v, then the i-th query is of the form ((a_{2i - 1} + v) \mod n, a_{2i}).
Input The first line contains two numbers: n and m. The root of the tree has number 0.
The second line contains n - 1 integers, the i-th of these numbers is equal to the number of the parent of vertex i.
The third line contains two integers in the range from 0 to n - 1: a_1 and a_2.
The fourth line contains three integers: x, y, z, these numbers are non-negative and do not exceed 10^9.
Output Output to the output file the sum of the vertex numbers - answers to all queries.
Examples Input 1:
copy
3 2 0 1 2 1 1 1 0
Output 1:
copy
2
Input 2:
copy
1 2
0 0 1 1 1
Output 2:
copy
0 solve this in c++ using competitive programming style
- Assistant