📌  相关文章
📜  查询将每个数组元素替换为具有更新的给定值的异或

📅  最后修改于: 2021-09-08 12:43:57             🧑  作者: Mango

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

  • Add(X):将X插入数组。
  • Update(X):A i ^ X替换每个数组元素A i ,其中 ^ 是异或运算。

例子:

方法:想法是存储一个变量,该变量存储要与每个数组元素进行异或运算的值,并在数组中插入元素以消除异或值的影响时,应用异或运算与自己。请按照以下步骤解决问题:

  • 维护一个变量,比如effect ,以存储最终的 XOR 值。
  • 对于操作Add(X):在列表末尾添加X并更新其值 X^效果
  • 对于操作更新(X):更新“效果”的值 效果 ^ X。这是为了获得更新的XOR效果。
  • 完成上述所有操作后,更新所有数组元素及其与effect 的异或,即对于所有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)

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