/** * @param {number[]} arr * @param {number[][]} queries * @return {number[]} */ var xorQueries = function(arr, queries) { let len = arr.length; let dp = new Array(len+1).fill(0); dp[0] = 0; for(let i = 1;i<=len;i++){ dp[i] = dp[i-1] ^ arr[i-1] } let n = queries.length; let res = new Array(n).fill(0); for(let i=0;i<n;i++){ let l = queries[i][0]; let r = queries[i][1]; res[i] = dp[l]^dp[r+1]; } return res; }; let arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] console.log(arr,queries, xorQueries(arr,queries)) arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]] console.log(arr,queries, xorQueries(arr,queries))
// 示例 1:
// 输入:arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] // 输出:[2,7,14,8] // 解释: // 数组中元素的二进制表示形式是: // 1 = 0001 // 3 = 0011 // 4 = 0100 // 8 = 1000 // 查询的 XOR 值为: // [0,1] = 1 xor 3 = 2 // [1,2] = 3 xor 4 = 7 // [0,3] = 1 xor 3 xor 4 xor 8 = 14 // [3,3] = 8 // 示例 2: // 输入:arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]] // 输出:[8,0,4,4] // 提示: // 1 <= arr.length <= 3 * 10^4 // 1 <= arr[i] <= 10^9 // 1 <= queries.length <= 3 * 10^4 // queries[i].length == 2 // 0 <= queries[i][0] <= queries[i][1] < arr.length