#P2441. Finding the Nearest Similar Boss
Finding the Nearest Similar Boss
Finding the Nearest Similar Boss
The organization "Xumeng Fan Club" is structured as a tree. The president is at the root and each member has an attribute which is the product of several prime numbers (for example, cat ears = , weak spirit = , blonde hair = , yandere = , twin tails = , etc.). For instance, a beauty might have the attribute .
Two members are considered to have similar attributes if they share at least one common prime factor (i.e. if their greatest common divisor is more than ). Now, each member wants to know the closest boss (i.e. ancestor in the tree) whose attribute is similar to theirs. If no such boss exists on the chain from the member up to the president, output .
In addition, members may update their attribute at any time.
You are given the structure of the organization with the initial attributes and a sequence of queries. There are two types of queries:
-
Query (format:
1 u
): For a given member , find the closest boss (ancestor) having an attribute with at least one common prime factor with member . -
Update (format:
2 u val
): Update the attribute of member to the new value .
inputFormat
The first line contains two integers and , representing the number of members and the number of queries, respectively.
The second line contains integers, the initial attribute values of the members from to .
The next lines each contain one integer: for to , the -th line gives the immediate boss of member .
The following lines each contain a query in one of the following formats:\
• 1 u
(Query: find the nearest boss for member with a similar attribute)\
• 2 u val
(Update: change the attribute value of member to )
outputFormat
For each query of type 1, output a single line containing the id of the closest boss with a similar attribute. Output if no such boss exists.
sample
5 7
6 2 15 10 7
1
1
2
2
1 4
1 3
1 5
2 5 35
1 5
2 2 14
1 5
2
1
-1
-1
2
</p>