#P2441. Finding the Nearest Similar Boss

    ID: 15712 Type: Default 1000ms 256MiB

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 = 22, weak spirit = 33, blonde hair = 55, yandere = 77, twin tails = 1111, etc.). For instance, a beauty might have the attribute 2×2×3=122\times2\times3 = 12.

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 11). 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 1-1.

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:

  1. Query (format: 1 u): For a given member uu, find the closest boss (ancestor) having an attribute with at least one common prime factor with member uu.

  2. Update (format: 2 u val): Update the attribute of member uu to the new value valval.

inputFormat

The first line contains two integers nn and qq, representing the number of members and the number of queries, respectively.
The second line contains nn integers, the initial attribute values of the members from 11 to nn.
The next n1n-1 lines each contain one integer: for i=2i = 2 to nn, the ii-th line gives the immediate boss of member ii.
The following qq lines each contain a query in one of the following formats:\ • 1 u (Query: find the nearest boss for member uu with a similar attribute)\ • 2 u val (Update: change the attribute value of member uu to valval)

outputFormat

For each query of type 1, output a single line containing the id of the closest boss with a similar attribute. Output 1-1 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>