📜  使用给定的Q XOR查询构造一个列表

📅  最后修改于: 2021-04-22 00:36:47             🧑  作者: Mango

给定一个列表S ,该列表最初包含单个值0 。以下是以下类型的Q查询:

  • 0 X :在列表中插入X
  • 1 X :对于S中的每个元素A,将其替换为A XORX。

任务是在执行给定的Q查询之后,以升序打印列表中的所有元素。

例子:

天真的方法:解决问题的最简单方法是:

  1. 0初始化一个列表,然后遍历每个查询并执行以下操作:
    • 对于查询类型0,请在列表中添加给定值。
    • 对于查询类型1遍历列表,并使用value将每个元素各自的按位XOR更新。
  2. 执行完所有查询后,按升序打印最终列表。

时间复杂度: O(N * Q),其中N是列表的长度,Q是查询的数量
辅助空间: O(1)

高效的方法:通过跟踪从右到左的累积按位XOR运算,以相反的顺序处理数字要容易得多。请按照以下步骤解决问题:

  1. 0初始化变量xor
  2. 从右到左遍历查询数组,在查询类型为0时将xor ^ value添加到列表中,否则用xor ^ value更新xor。
  3. 遍历查询后,将xor添加到列表中并对最终列表进行排序。
  4. 完成上述操作后,打印最终列表。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
#define N 5
#define M 2
 
// Function to return required list
// after performing all the queries
list ConstructList(int Q[N][M])
{
 
    // Store cumulative Bitwise XOR
    int xr = 0;
 
    // Initialize final list to return
    list ans;
 
    // Perform each query
    for (int i = N - 1; i >= 0; i--)
    {
        if (Q[i][0] == 0)
            ans.push_back(Q[i][1] ^ xr);
        else
            xr ^= Q[i][1];
    }
 
    // The initial value of 0
    ans.push_back(xr);
 
    // Sort the list
    ans.sort();
 
    // Return final list
    return ans;
}
 
// Driver Code
int main()
{
    // Given Queries
    int Q[N][M] = {{ 0, 6 }, { 0, 3 },
                   { 0, 2 }, { 1, 4 },
                   { 1, 5 }};
 
    // Function Call
    list ans = ConstructList(Q);
    for (auto it = ans.begin(); it != ans.end(); ++it)
        cout << ' ' << *it;
}
 
// This code is contributed by gauravrajput1


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to return required list
    // after performing all the queries
    static List ConstructList(int[][] Q)
    {
 
        // Store cumulative Bitwise XOR
        int xor = 0;
 
        // Initialize final list to return
        List ans = new ArrayList<>();
 
        // Perform each query
        for (int i = Q.length - 1; i >= 0; i--) {
 
            if (Q[i][0] == 0)
                ans.add(Q[i][1] ^ xor);
 
            else
                xor ^= Q[i][1];
        }
 
        // The initial value of 0
        ans.add(xor);
 
        // Sort the list
        Collections.sort(ans);
 
        // Return final list
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Queries
        int[][] Q = {
            { 0, 6 }, { 0, 3 }, { 0, 2 },
            { 1, 4 }, { 1, 5 }
        };
 
        // Function Call
        System.out.println(ConstructList(Q));
    }
}


Python3
# Python3 program for the above approach
 
# Function to return required list
# after performing all the queries
def ConstructList(Q):
 
    # Store cumulative Bitwise XOR
    xor = 0
 
    # Initialize final list to return
    ans = []
 
    # Perform each query
    for i in range(len(Q) - 1, -1, -1):
        if(Q[i][0] == 0):
            ans.append(Q[i][1] ^ xor)
 
        else:
            xor ^= Q[i][1]
 
    # The initial value of 0
    ans.append(xor)
 
    # Sort the list
    ans.sort()
 
    # Return final list
    return ans
 
# Driver Code
 
# Given Queries
Q = [ [0, 6], [0, 3],
      [0, 2], [1, 4],
      [1, 5] ]
 
# Function call
print(ConstructList(Q))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
    // Function to return required list
    // after performing all the queries
    static List ConstructList(int[,] Q)
    {
 
        // Store cumulative Bitwise XOR
        int xor = 0;
 
        // Initialize readonly list to return
        List ans = new List();
 
        // Perform each query
        for (int i = Q.GetLength(0) - 1; i >= 0; i--)
        {
            if (Q[i, 0] == 0)
                ans.Add(Q[i, 1] ^ xor);
 
            else
                xor ^= Q[i, 1];
        }
 
        // The initial value of 0
        ans.Add(xor);
 
        // Sort the list
        ans.Sort();
 
        // Return readonly list
        return ans;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        // Given Queries
        int[,] Q = {{ 0, 6 }, { 0, 3 }, { 0, 2 },
                    { 1, 4 }, { 1, 5 }};
 
        // Function Call
        foreach( int i in ConstructList(Q))
            Console.Write(i + ", ");
    }
}
 
// This code is contributed by Princi Singh


输出:
[1, 2, 3, 7]







时间复杂度: O(Q * log(Q)) ,其中Q是查询数。
辅助空间: O(N) ,其中N是结果列表中元素的数量。