📌  相关文章
📜  对于给定的一组查询,找到给定范围[L,R]中值为K的元素的XOR

📅  最后修改于: 2021-04-29 05:22:59             🧑  作者: Mango

给定一个数组arr []Q个查询,任务是在Q个查询之后查找结果更新后的数组。有两种类型的查询,它们执行以下操作:

  1. Update(L,R,K):K到L到R范围内的每个元素执行XOR。
  2. Display():显示给定数组的当前状态。

例子:

朴素的方法:针对每个更新查询的朴素方法是,从L到R循环运行,并使用给定的K从arr [L]到arr [R]的所有元素执行XOR操作。为了执行显示查询,只需打印数组arr []。此方法的时间复杂度为O(NQ),其中N是数组的长度,Q是查询数。

高效方法:想法是使用kadane算法解决此问题。

  • 空数组res []的定义长度与原始数组相同。
  • 对于每个更新查询{L,R,K},将res []数组修改为:
    1. XOR K到res [L]
    2. XOR K到res [R + 1]
  • 每当用户发出显示查询时:
    1. 初始化计数器变量“ i”到索引1。
    2. 执行操作:
      res[i] = res[i] ^ res[i - 1]
      
      
          
    3. 将计数器变量“ i”初始化为索引0。
    4. 对于数组中的每个索引,所需的答案是:
      arr[i] ^ res[i]
      

下面是上述方法的实现:

C++
// C++ implementation to perform the
// XOR range updates on an array
#include 
using namespace std;
  
// Function to perform the update operation
// on the given array
void update(int res[], int L, int R, int K)
{
  
    // Converting the indices to
    // 0 indexing.
    L -= 1;
    R -= 1;
  
    // Saving the XOR of K from the starting
    // index in the range [L, R].
    res[L] ^= K;
  
    // Saving the XOR of K at the ending index
    // in the given [L, R].
    res[R + 1] ^= K;
}
  
// Function to display the resulting array
void display(int arr[], int res[], int n)
{
    for (int i = 1; i < n; i++) {
  
        // Finding the resultant value in the
        // result array
        res[i] = res[i] ^ res[i - 1];
    }
  
    for (int i = 0; i < n; i++) {
  
        // Combining the effects of the updates
        // with the original array without
        // changing the initial array.
        cout << (arr[i] ^ res[i]) << " ";
    }
    cout << endl;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 10 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int res[N];
    memset(res, 0, sizeof(res));
  
    // Query 1
    int L = 1, R = 3, K = 2;
    update(res, L, R, K);
  
    // Query 2
    L = 2;
    R = 4;
    K = 3;
    update(res, L, R, K);
  
    // Query 3
    display(arr, res, N);
  
    return 0;
}


Java
// Java implementation to perform the
// XOR range updates on an array
class GFG{
      
// Function to perform the update 
// operation on the given array
static void update(int res[], int L, 
                   int R, int K)
{
      
    // Converting the indices to
    // 0 indexing.
    L -= 1;
    R -= 1;
  
    // Saving the XOR of K from the 
    // starting index in the range [L, R].
    res[L] ^= K;
  
    // Saving the XOR of K at the ending 
    // index in the given [L, R].
    res[R + 1] ^= K;
}
  
// Function to display the resulting array
static void display(int arr[], 
                    int res[], int n)
{
    int i;
    for(i = 1; i < n; i++)
    {
          
       // Finding the resultant value 
       // in the result array
       res[i] = res[i] ^ res[i - 1];
    }
  
    for(i = 0; i < n; i++)
    {
         
       // Combining the effects of the 
       // updates with the original array 
       // without changing the initial array.
       System.out.print((arr[i] ^ res[i]) + " ");
    }
    System.out.println();
}
  
// Driver code
public static void main(String []args)
{
    int arr[] = { 2, 4, 6, 8, 10 };
    int N = arr.length;
    int res[] = new int[N];
  
    // Query 1
    int L = 1, R = 3, K = 2;
    update(res, L, R, K);
  
    // Query 2
    L = 2;
    R = 4;
    K = 3;
    update(res, L, R, K);
  
    // Query 3
    display(arr, res, N);
}
}
  
// This code is contributed by chitranayal


Python3
# Python3 implementation to perform the 
# XOR range updates on an array 
  
# Function to perform the update operation 
# on the given array 
def update(res, L, R, K): 
  
    # Converting the indices to 
    # 0 indexing. 
    L = L - 1
    R = R - 1
  
    # Saving the XOR of K from the starting 
    # index in the range [L, R]. 
    res[L] = res[L] ^ K
  
    # Saving the XOR of K at the ending index 
    # in the given [L, R]. 
    res[R + 1] = res[R + 1] ^ K
  
# Function to display the resulting array 
def display(arr, res, n):
  
    for i in range(1, n):
  
        # Finding the resultant value in the 
        # result array 
        res[i] = res[i] ^ res[i - 1]
          
    for i in range(0, n):
  
        # Combining the effects of the updates 
        # with the original array without 
        # changing the initial array. 
        print (arr[i] ^ res[i], end = " ")
  
# Driver code 
arr = [ 2, 4, 6, 8, 10 ] 
N = len(arr) 
res = [0] * N
  
# Query 1 
L = 1
R = 3
K = 2
update(res, L, R, K) 
  
# Query 2 
L = 2
R = 4
K = 3
update(res, L, R, K) 
  
# Query 3 
display(arr, res, N)
  
# This code is contributed by Pratik Basu


C#
// C# implementation to perform the
// XOR range updates on an array
using System;
class GFG{
      
// Function to perform the update 
// operation on the given array
static void update(int []res, int L, 
                   int R, int K)
{
      
    // Converting the indices to
    // 0 indexing.
    L -= 1;
    R -= 1;
  
    // Saving the XOR of K from the 
    // starting index in the range [L, R].
    res[L] ^= K;
  
    // Saving the XOR of K at the ending 
    // index in the given [L, R].
    res[R + 1] ^= K;
}
  
// Function to display the resulting array
static void display(int []arr, 
                    int []res, int n)
{
    int i;
    for(i = 1; i < n; i++)
    {
          
        // Finding the resultant value 
        // in the result array
        res[i] = res[i] ^ res[i - 1];
    }
  
    for(i = 0; i < n; i++)
    {
          
        // Combining the effects of the 
        // updates with the original array 
        // without changing the initial array.
        Console.Write((arr[i] ^ res[i]) + " ");
    }
    Console.WriteLine();
}
  
// Driver code
public static void Main()
{
    int []arr = { 2, 4, 6, 8, 10 };
    int N = arr.Length;
    int []res = new int[N];
  
    // Query 1
    int L = 1, R = 3, K = 2;
    update(res, L, R, K);
  
    // Query 2
    L = 2;
    R = 4;
    K = 3;
    update(res, L, R, K);
  
    // Query 3
    display(arr, res, N);
}
}
  
// This code is contributed by Code_Mech


输出:
0 5 7 11 10

时间复杂度:

  • 更新的时间复杂度为O(1)
  • 显示数组的时间复杂度为O(N)

注意:当更新查询与显示查询相比很高时,此方法效果很好。