1389. Maximum Genetic Difference Query

HardBit ManipulationXOR TrieTreeOffline Queries

A rooted tree has n nodes numbered 0 to n-1, given by a parents array where parents[i] is the parent of node i and the root has parent -1. Each node's gene value is its own number. For each query [node, val], return the maximum value of val XOR x where x ranges over the gene values on the path from the root to node (inclusive).

Input: A JSON object {"parents": [<parent of each node, -1 for root>], "queries": [[node, val], ...]}.

Output: Return an array of answers, one per query, in the original order.

Examples

Example 1
Input: {"parents":[-1,0,1,1],"queries":[[0,2],[3,2],[2,5]]}
Output: [2,3,7]
Explanation: The path maxima give answers 2, 3 and 7.
Example 2
Input: {"parents":[-1,0,1,2,3],"queries":[[4,3],[2,6]]}
Output: [7,7]
Explanation: Each query maximises val XOR over its root path.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →