Genetic Difference

Solve a hard-level problem of finding the genetic difference between individuals using tries.

Problem statement

In a family of NN people, individuals are numbered from 00 to N1N-1 and each individual has a unique binary representation. The genetic difference between two individuals is calculated as the XOR of their binary representations. For example, if two individuals in a family are denoted by 11 and 22, then their genetic difference is calculated as 1 XOR 2=31 \space XOR \space 2 = 3.

You're provided with an integer array parent, where parent[i]parent[i] is the parent of an individual ii. Also, the head (root) of the family does not have any parent, so the value of parent for root is 1-1. The scientists are now comparing the individuals in this family with a new external individual, and they wish to find the person in this family who is genetically most dissimilar to this new individual. But to limit the search space, they define the limit on the people to whom they'll be comparing the new individual.

You are given QQ queries in the format (nodei,valuei)(node_i, value_i). For every query, find the maximum genetic difference between valueivalue_i and pip_i, where pip_i is the genetic value of any node that's on the path between nodeinode_i and the root (including nodeinode_i and the root). In other words, maximize valueivalue_i XOR pip_i.

Example

Sample input

Get hands-on with 1400+ tech skills courses.