给定一个由N个节点组成的理想二叉树,节点的值从1到N(如下图所示)和Q查询,其中每个查询由两个整数L和X组成。任务是找到X XOR Y的最大可能值,其中Y可以是级别L的任何节点。
例子:
Input: Q[] = {{2, 5}, {3, 15}}
Output:
7
11
1st Query: Level 2 has numbers 2 and 3.
Therefore, 2^5 = 7 and 3^5 = 6.
Hence, the answer is 7.
2nd Query: Level 3 has numbers 4, 5, 6 and 7
and 4^15 = 11 is the maximum possible.
Input: Q[] = {{ 1, 15 }, { 5, 23 }}
Output:
14
15
方法:级别L中的数字包含L位,例如,级别2中的数字为2和3,可以使用2位二进制表示。类似地,在级别3中,数字4、5、6和7可以用3位表示。
因此,我们必须找到一个L位数,使X与xor最大。将X的位存储在数组a []中。现在,填充数组B []尺寸L与元件相反的[],也就是说,如果一个[i]等于0,那么把B [I]等于1且反之亦然。
请注意,B []的L-1索引必须始终是1。如果的[]是小于b大小[]然后填写B []的剩余的索引与1数组b []填充相反数组a []的值,以便由数组b []的位数得出的最大值为xor值。最后,计算由b []得出的数字,并以X作为答案将其xor返回给查询。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
#define MAXN 60
// Function to solve queries of the maximum xor value
// between the nodes in a given level L of a
// perfect binary tree and a given value X
int solveQuery(int L, int X)
{
// Initialize result
int res;
// Initialize array to store bits
int a[MAXN], b[L];
// Initialize a copy of X
// and size of array
int ref = X, size_a = 0;
// Storing the bits of X
// in the array a[]
while (ref > 0) {
a[size_a] = ref % 2;
ref /= 2;
size_a++;
}
// Filling the array b[]
for (int i = 0; i < min(size_a, L); i++) {
if (a[i] == 1)
b[i] = 0;
else
b[i] = 1;
}
for (int i = min(size_a, L); i < L; i++)
b[i] = 1;
b[L - 1] = 1;
// Initializing variable which gives
// maximum xor
int temp = 0, p = 1;
for (int i = 0; i < L; i++) {
temp += b[i] * p;
p *= 2;
}
// Getting the maximum xor value
res = temp ^ X;
// Return the result
return res;
}
// Driver code
int main()
{
int queries[][2] = { { 2, 5 }, { 3, 15 } };
int q = sizeof(queries) / sizeof(queries[0]);
// Perform queries
for (int i = 0; i < q; i++)
cout << solveQuery(queries[i][0],
queries[i][1])
<< endl;
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MAXN = 60;
// Function to solve queries of the maximum xor value
// between the nodes in a given level L of a
// perfect binary tree and a given value X
static int solveQuery(int L, int X)
{
// Initialize result
int res;
// Initialize array to store bits
int []a = new int [MAXN];
int []b = new int[L];
// Initialize a copy of X
// and size of array
int refer = X, size_a = 0;
// Storing the bits of X
// in the array a[]
while (refer > 0)
{
a[size_a] = refer % 2;
refer /= 2;
size_a++;
}
// Filling the array b[]
for (int i = 0; i < Math.min(size_a, L); i++)
{
if (a[i] == 1)
b[i] = 0;
else
b[i] = 1;
}
for (int i = Math.min(size_a, L); i < L; i++)
b[i] = 1;
b[L - 1] = 1;
// Initializing variable which gives
// maximum xor
int temp = 0, p = 1;
for (int i = 0; i < L; i++)
{
temp += b[i] * p;
p *= 2;
}
// Getting the maximum xor value
res = temp ^ X;
// Return the result
return res;
}
// Driver code
static public void main (String args[])
{
int [][]queries= { { 2, 5 }, { 3, 15 } };
int q = queries.length;
// Perform queries
for (int i = 0; i < q; i++)
System.out.println( solveQuery(queries[i][0],
queries[i][1]) );
}
}
// This code is contributed by AnkitRai01
Python
# Python3 implementation of the approach
MAXN = 60
# Function to solve queries of the maximum xor value
# between the nodes in a given level L of a
# perfect binary tree and a given value X
def solveQuery(L, X):
# Initialize result
res = 0
# Initialize array to store bits
a = [0 for i in range(MAXN)]
b = [0 for i in range(MAXN)]
# Initialize a copy of X
# and size of array
ref = X
size_a = 0
# Storing the bits of X
# in the array a[]
while (ref > 0):
a[size_a] = ref % 2
ref //= 2
size_a+=1
# Filling the array b[]
for i in range(min(size_a,L)):
if (a[i] == 1):
b[i] = 0
else:
b[i] = 1
for i in range(min(size_a, L),L):
b[i] = 1
b[L - 1] = 1
# Initializing variable which gives
# maximum xor
temp = 0
p = 1
for i in range(L):
temp += b[i] * p
p *= 2
# Getting the maximum xor value
res = temp ^ X
# Return the result
return res
# Driver code
queries= [ [ 2, 5 ], [ 3, 15 ] ]
q = len(queries)
# Perform queries
for i in range(q):
print(solveQuery(queries[i][0],queries[i][1]))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAXN = 60;
// Function to solve queries of the maximum xor value
// between the nodes in a given level L of a
// perfect binary tree and a given value X
static int solveQuery(int L, int X)
{
// Initialize result
int res;
// Initialize array to store bits
int []a = new int [MAXN];
int []b = new int[L];
// Initialize a copy of X
// and size of array
int refer = X, size_a = 0;
// Storing the bits of X
// in the array a[]
while (refer > 0)
{
a[size_a] = refer % 2;
refer /= 2;
size_a++;
}
// Filling the array b[]
for (int i = 0; i < Math.Min(size_a, L); i++)
{
if (a[i] == 1)
b[i] = 0;
else
b[i] = 1;
}
for (int i = Math.Min(size_a, L); i < L; i++)
b[i] = 1;
b[L - 1] = 1;
// Initializing variable which gives
// maximum xor
int temp = 0, p = 1;
for (int i = 0; i < L; i++)
{
temp += b[i] * p;
p *= 2;
}
// Getting the maximum xor value
res = temp ^ X;
// Return the result
return res;
}
// Driver code
static public void Main ()
{
int [,]queries= { { 2, 5 }, { 3, 15 } };
int q = queries.Length;
// Perform queries
for (int i = 0; i < q; i++)
Console.WriteLine( solveQuery(queries[i,0],
queries[i,1]) );
}
}
// This code is contributed by anuj_67..
7
11