📌  相关文章
📜  使用给定值将子数组中的每个元素更新为按位异或的查询

📅  最后修改于: 2021-09-07 02:17:08             🧑  作者: Mango

给定一个数组arr[] ,并查询形式为(l, r, val) 的Q[][] ,每个查询的任务是将索引[l – 1, r – 1]中的所有元素更新为按位与val异或。打印完成所有查询后获得的最终数组。
例子:

天真的方法:
解决这个问题的最简单的方法是为每个查询遍历索引[l – 1, r – 1]并将arr[i]替换为arr[i]^val 。完成所有查询后,打印修改后的数组。
时间复杂度: O(N*sizeof(Q))
辅助空间: O(1)

方法:按照恒定时间复杂度处理每个查询来解决问题的步骤:

  • 用全零初始化数组temp[]
  • 对于(l, r, val)形式的每个查询,使用它们各自的XORval更新temp[l – 1]temp[r]
  • 为每个查询完成上述步骤后,将temp[]转换为前缀 XOR array
  • 最后,通过用其XOR替换每个i元素来执行更新 arr[] 。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to perform XOR in
// the range [lo, hi]
void findxor(int temp[], int N,
             int lo, int hi, int val)
{
    temp[lo] ^= val;
    if (hi != N - 1)
        temp[hi + 1] ^= val;
}
  
// Function to generate Prefix
// Xor Array
void updateArray(int temp[], int N)
{
    for (int i = 1; i < N; i++)
        temp[i] ^= temp[i - 1];
}
  
// Function to perfrom each Query
// and print the final array
void xorQuery(int arr[], int n,
              vector > Q)
{
  
    int temp[n];
    // initialize temp array to 0
    memset(temp, 0, sizeof(temp));
  
    // Perform each Query
    for (int i = 0; i < Q.size(); i++) {
        findxor(temp, n, Q[i][0] - 1,
                Q[i][1] - 1, Q[i][2]);
    }
  
    // Modify the array
    updateArray(temp, n);
  
    // Print the final array
    for (int i = 0; i < n; ++i) {
        cout << (arr[i] ^ temp[i]) << " ";
    }
}
  
// Driver Code
int main()
{
  
    int arr[] = { 2, 3, 6, 5, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    vector > Q = { { 1, 3, 2 },
                               { 2, 4, 4 } };
  
    xorQuery(arr, n, Q);
  
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
  
class GFG{
      
// Function to perform XOR in
// the range [lo, hi]
static void findxor(int temp[], int N,
                    int lo, int hi, int val)
{
    temp[lo] ^= val;
    if (hi != N - 1)
        temp[hi + 1] ^= val;
}
  
// Function to generate Prefix
// Xor Array
static void updateArray(int temp[], int N)
{
    for(int i = 1; i < N; i++)
        temp[i] ^= temp[i - 1];
}
  
// Function to perfrom each Query
// and print the final array
static void xorQuery(int arr[], int n,
                     int[][] Q)
{
    int[] temp = new int[n];
  
    // Perform each Query
    for(int i = 0; i < Q.length; i++)
    {
        findxor(temp, n, Q[i][0] - 1,
                         Q[i][1] - 1,
                         Q[i][2]);
    }
  
    // Modify the array
    updateArray(temp, n);
  
    // Print the final array
    for(int i = 0; i < n; ++i)
    {
        System.out.print((arr[i] ^ temp[i]) + " ");
    }
}
  
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 2, 3, 6, 5, 4 };
    int n = arr.length;
      
    int[][] Q = { { 1, 3, 2 },
                  { 2, 4, 4 } };
      
    xorQuery(arr, n, Q);
}
}
  
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
  
# Function to perform XOR in
# the range [lo, hi]
def findxor(temp, N, lo, hi, val):
  
    temp[lo] ^= val
    if (hi != N - 1):
        temp[hi + 1] ^= val
  
# Function to generate Prefix
# Xor Array
def updateArray(temp, N):
  
    for i in range(1, N):
        temp[i] ^= temp[i - 1]
  
# Function to perfrom each Query
# and print the final array
def xorQuery(arr, n, Q):
  
    temp =[0] * n
  
    # Perform each Query
    for i in range(len(Q)):
        findxor(temp, n, Q[i][0] - 1,
                         Q[i][1] - 1, 
                         Q[i][2])
  
    # Modify the array
    updateArray(temp, n)
  
    # Print the final array
    for i in range(n):
        print((arr[i] ^ temp[i]), end = " ")
  
# Driver Code
if __name__ == "__main__":
  
    arr = [ 2, 3, 6, 5, 4 ]
    n = len(arr)
    Q = [ [ 1, 3, 2 ],
          [ 2, 4, 4 ] ] 
  
    xorQuery(arr, n, Q)
  
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
  
class GFG{
      
// Function to perform XOR in
// the range [lo, hi]
static void findxor(int []temp, int N,
                    int lo, int hi, int val)
{
    temp[lo] ^= val;
      
    if (hi != N - 1)
        temp[hi + 1] ^= val;
}
  
// Function to generate Prefix
// Xor Array
static void updateArray(int []temp, int N)
{
    for(int i = 1; i < N; i++)
        temp[i] ^= temp[i - 1];
}
  
// Function to perfrom each Query
// and print the readonly array
static void xorQuery(int []arr, int n,
                     int[,] Q)
{
    int[] temp = new int[n];
  
    // Perform each Query
    for(int i = 0; i < Q.GetLength(0); i++)
    {
        findxor(temp, n, Q[i, 0] - 1,
                         Q[i, 1] - 1,
                         Q[i, 2]);
    }
  
    // Modify the array
    updateArray(temp, n);
  
    // Print the readonly array
    for(int i = 0; i < n; ++i)
    {
        Console.Write((arr[i] ^ temp[i]) + " ");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 3, 6, 5, 4 };
    int n = arr.Length;
      
    int[,] Q = { { 1, 3, 2 },
                 { 2, 4, 4 } };
      
    xorQuery(arr, n, Q);
}
}
  
// This code is contributed by 29AjayKumar


输出:
0 5 0 1 4

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live