📌  相关文章
📜  查询用更新的给定值用其XOR替换每个数组元素

📅  最后修改于: 2021-05-17 21:15:26             🧑  作者: Mango

给定一个数组,该数组最初由0作为唯一的存在元素,并且由以下两种类型的Q操作组成:

  • Add(X):将X插入数组。
  • Update(X):将每个数组元素A i替换为A i ^ X ,其中^是XOR操作。

例子:

方法:这个想法是存储一个变量,该变量存储将与每个数组元素进行XOR运算的值,并且在将元素插入数组中以消除XOR值的影响时,请应用XOR操作本身。请按照以下步骤解决问题:

  • 维护一个变量,例如effect ,以存储最终的XOR值。
  • 对于操作Add(X):在列表末尾添加X并使用来更新其值 X ^效应
  • 对于操作更新(X):更新“效果”的值 具有效果^X。这是为了获得最新的XOR效果。
  • 完成上述所有操作后,使用效果的XOR更新所有数组元素,即,对于所有A i ,更新A i = A i ^ effect

下面是上述方法的实现:

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
#define int long long
 
// Function to insert an element
// into the array
void add(vector& arr, int x)
{
    arr.push_back(x);
}
 
// Function to update every array
// element a[i] by a[i] ^ x
void update(int& effect, int x)
{
    effect = effect ^ x;
}
 
// Function to compute the final
// results after the operations
void computeResults(vector& arr,
                    int effect)
{
    for (int i = 0; i < arr.size(); i++) {
        arr[i] = arr[i] ^ effect;
        cout << arr[i] << " ";
    }
}
 
// Driver Code
signed main()
{
    vector arr = { 0 };
    int effect = 0;
 
    // Queries
    add(arr, 5);
    update(effect, 2);
 
    // Function Call
    computeResults(arr, effect);
}


Java
// Java program of
// the above approach
import java.util.*;
class GFG{
   
static Vector arr =
              new Vector();
static int effect;
   
// Function to insert an element
// into the array
static void add(int x)
{
  arr.add(x);
}
 
// Function to update every array
// element a[i] by a[i] ^ x
static void update(int x)
{
  effect = effect ^ x;
}
 
// Function to compute the final
// results after the operations
static void computeResults()
{
  for (int i = 0; i < arr.size(); i++)
  {
    arr.set(i, arr.get(i) ^ effect);
    System.out.print(arr.get(i) + " ");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  arr = new Vector();
  arr.add(0);
  effect = 0;
 
  // Queries
  add(5);
  update(2);
 
  // Function Call
  computeResults();
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program of the above approach
 
# Function to insert an element
# into the array
def add(arr, x):
     
    arr.append(x)
 
# Function to update every array
# element a[i] by a[i] ^ x
def update(effect, x):
 
    effect = effect ^ x
    return effect
 
# Function to compute the final
# results after the operations
def computeResults(arr, effect):
 
    for i in range(len(arr)):
        arr[i] = arr[i] ^ effect
        print(arr[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
 
    arr = [0]
    effect = 0
 
    # Queries
    add(arr, 5)
    effect = update(effect, 2)
 
    # Function call
    computeResults(arr, effect)
 
# This code is contributed by mohit kumar 29


C#
// C# program of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
   
static List arr = new List();
static int effect;
   
// Function to insert an element
// into the array
static void add(int x)
{
  arr.Add(x);
}
 
// Function to update every array
// element a[i] by a[i] ^ x
static void update(int x)
{
  effect = effect ^ x;
}
 
// Function to compute the final
// results after the operations
static void computeResults()
{
  for (int i = 0; i < arr.Count; i++)
  {
    arr[i] = arr[i] ^ effect;
    Console.Write(arr[i] + " ");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  arr = new List();
  arr.Add(0);
  effect = 0;
 
  // Queries
  add(5);
  update(2);
 
  // Function Call
  computeResults();
}
}
 
// This code is contributed by shikhasingrajput


输出:
2 7




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