7
firstchangeNode = null,9
for (let i = 17; i >= 0; i--) {10
// a node can be up to (1e5), whose log2 is 17, so at most we need 17 bits11
let next = Number(Boolean(val & (1 << i)));12
if (cur[next] === -1) {13
if (firstchangeNode === null) (firstchangeNode = cur), (fcIndex = next);18
// memorize the first change (if any) that was made during the insertion of val19
return [firstchangeNode, fcIndex];24
for (let i = 17; i >= 0; i--) {25
let xor = 1 ^ Number(Boolean(val & (1 << i)));26
if (cur[xor] !== -1) (cur = cur[xor]), (res |= 1 << i);27
else cur = cur[xor ^ 1];32
var maxGeneticDifference = function (parents, queries) {35
res = [...Array(n)].map((d) => {}),36
Q = [...Array(n)].map((d) => []),37
adj = [...Array(n)].map((d) => []);38
// create some objects for fast access to the results39
for (let [node, val] of queries)40
Q[node].push(val), (res[node] = {}), (res[node][val] = undefined);41
// create an adjacency list that points to the next node42
for (let i = 0; i < parents.length; i++)43
if (parents[i] !== -1) adj[parents[i]].push(i);48
let [fnode, index] = T.insert(node); //insert the value of a node to our trie49
for (let q of Q[node]) //answer all the queries for our current node50
res[node][q] = T.search(q);51
for (let nei of adj[node]) // dfs to the children nodes54
// if my trie was altered during this turn, revert the change58
//return the results, which have already been calculated59
return queries.map(([node, val]) => res[node][val]);