📜  Q 查询的数组的所有子集的按位与的按位或

📅  最后修改于: 2022-05-13 01:56:04.937000             🧑  作者: Mango

Q 查询的数组的所有子集的按位与的按位或

给定两个大小为N的数组arr[]和大小为Qquery[] ,任务是找到数组子集的 AND 的 OR。在每个查询中,您都会获得一个索引和一个值,您必须将数组的给定索引处的值替换为给定值,并在每次查询后打印数组所有子集的 AND 或。

例子:

方法:这个问题可以用贪心算法来解决。请按照以下步骤解决问题:

  • 初始化一个大小为32的数组bits[]并存储所有元素的设置位的计数。
  • 使用变量p[0, Q-1]范围内迭代:  
    • 首先减去先前值的位,然后添加新值的位。
    • 使用变量i[0, 31]范围内迭代:  
      • 如果当前位设置为前一个值,则在第 i 个索引处的bits[]数组中减去一位。
      • 如果当前位设置为新值,则在第 i 个索引处向bits[]数组添加一位。
    • 用给定数组arr[] 中的前一个值替换新值。
    • 初始化一个变量ans以存储位数组的OR
    • 使用变量i[0, 31]范围内迭代:  
      • 如果当前位不等于 0,则将当前位的OR添加到ans中。
    • 完成上述步骤后,打印ans作为每个查询所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the OR of AND of all
// subsets of the array for each query
void Or_of_Ands_for_each_query(int arr[], int n,
                               int queries[][2], int q)
{
    // An array to store the bits
    int bits[32] = { 0 };
 
    // Iterate for all the bits
    for (int i = 0; i < 32; i++) {
        // Iterate over all the numbers and
        // store the bits in bits[] array
        for (int j = 0; j < n; j++) {
            if ((1 << i) & arr[j]) {
                bits[i]++;
            }
        }
    }
 
    // Iterate over all the queries
    for (int p = 0; p < q; p++) {
 
        // Replace the bits of the value
        // at arr[queries[p][0]] with the bits
        // of queries[p][1]
        for (int i = 0; i < 32; i++) {
            if ((1 << i) & arr[queries[p][0]]) {
                bits[i]--;
            }
            if (queries[p][1] & (1 << i)) {
                bits[i]++;
            }
        }
 
        // Substitute the value in the array
        arr[queries[p][0]] = queries[p][1];
        int ans = 0;
 
        // Find OR of the bits[] array
        for (int i = 0; i < 32; i++) {
            if (bits[i] != 0) {
                ans |= (1 << i);
            }
        }
        // Print the answer
        cout << ans << endl;
    }
}
 
// Driver Code
int main()
{
    // Given Input
    int n = 3, q = 2;
    int arr[] = { 3, 5, 7 };
    int queries[2][2] = { { 1, 2 }, { 2, 1 } };
 
    // Function Call
    Or_of_Ands_for_each_query(arr, n, queries, q);
 
    return 0;
}


Java
// java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the OR of AND of all
    // subsets of the array for each query
    static void Or_of_Ands_for_each_query(int arr[], int n,
                                          int queries[][],
                                          int q)
    {
        // An array to store the bits
        int bits[] = new int[32];
        Arrays.fill(bits, 0);
 
        // Iterate for all the bits
        for (int i = 0; i < 32; i++) {
            // Iterate over all the numbers and
            // store the bits in bits[] array
            for (int j = 0; j < n; j++) {
                if (((1 << i) & arr[j]) != 0) {
                    bits[i]++;
                }
            }
        }
 
        // Iterate over all the queries
        for (int p = 0; p < q; p++) {
 
            // Replace the bits of the value
            // at arr[queries[p][0]] with the bits
            // of queries[p][1]
            for (int i = 0; i < 32; i++) {
                if (((1 << i) & arr[queries[p][0]]) != 0) {
                    bits[i]--;
                }
                if ((queries[p][1] & (1 << i)) != 0) {
                    bits[i]++;
                }
            }
 
            // Substitute the value in the array
            arr[queries[p][0]] = queries[p][1];
            int ans = 0;
 
            // Find OR of the bits[] array
            for (int i = 0; i < 32; i++) {
                if (bits[i] != 0) {
                    ans |= (1 << i);
                }
            }
 
            // Print the answer
            System.out.println(ans);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given Input
        int n = 3, q = 2;
        int arr[] = { 3, 5, 7 };
        int queries[][] = { { 1, 2 }, { 2, 1 } };
 
        // Function Call
        Or_of_Ands_for_each_query(arr, n, queries, q);
    }
}
 
// This code is contributed by subhammahato348.


Python3
# Python3 program for the above approach
 
# Function to find the OR of AND of all
# subsets of the array for each query
 
 
def Or_of_Ands_for_each_query(arr, n, queries, q):
 
    # An array to store the bits
    bits = [0 for x in range(32)]
 
    # Iterate for all the bits
    for i in range(0, 32):
 
        # Iterate over all the numbers and
        # store the bits in bits[] array
        for j in range(0, n):
            if ((1 << i) & arr[j]):
                bits[i] += 1
 
    # Iterate over all the queries
    for p in range(0, q):
 
        # Replace the bits of the value
        # at arr[queries[p][0]] with the bits
        # of queries[p][1]
        for i in range(0, 32):
            if ((1 << i) & arr[queries[p][0]]):
                bits[i] -= 1
            if (queries[p][1] & (1 << i)):
                bits[i] += 1
 
        # Substitute the value in the array
        arr[queries[p][0]] = queries[p][1]
        ans = 0
 
        # Find OR of the bits[] array
        for i in range(0, 32):
            if (bits[i] != 0):
                ans |= (1 << i)
 
        # Print the answer
        print(ans)
 
#  Driver Code
 
 
#  Given Input
n = 3
q = 2
arr = [3, 5, 7]
queries = [[1, 2], [2, 1]]
 
# Function Call
Or_of_Ands_for_each_query(arr, n, queries, q)
 
# This code is contributed by amreshkumar3


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the OR of AND of all
// subsets of the array for each query
static void Or_of_Ands_for_each_query(int[] arr, int n,
                                      int[,] queries,
                                      int q)
{
     
    // An array to store the bits
    int[] bits = new int[32];
    for(int i = 0; i < 32; i++)
    {
        bits[i] = 0;
    }
 
    // Iterate for all the bits
    for(int i = 0; i < 32; i++)
    {
         
        // Iterate over all the numbers and
        // store the bits in bits[] array
        for(int j = 0; j < n; j++)
        {
            if (((1 << i) & arr[j]) != 0)
            {
                bits[i]++;
            }
        }
    }
 
    // Iterate over all the queries
    for(int p = 0; p < q; p++)
    {
         
        // Replace the bits of the value
        // at arr[queries[p][0]] with the bits
        // of queries[p][1]
        for(int i = 0; i < 32; i++)
        {
            if (((1 << i) & arr[queries[p, 0]]) != 0)
            {
                bits[i]--;
            }
            if ((queries[p, 1] & (1 << i)) != 0)
            {
                bits[i]++;
            }
        }
 
        // Substitute the value in the array
        arr[queries[p, 0]] = queries[p, 1];
        int ans = 0;
 
        // Find OR of the bits[] array
        for(int i = 0; i < 32; i++)
        {
            if (bits[i] != 0)
            {
                ans |= (1 << i);
            }
        }
 
        // Print the answer
        Console.WriteLine(ans);
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Input
    int n = 3, q = 2;
    int[] arr = { 3, 5, 7 };
    int[,] queries = { { 1, 2 }, { 2, 1 } };
     
    // Function Call
    Or_of_Ands_for_each_query(arr, n, queries, q);
}
}
 
// This code is contributed by target_2


Javascript


输出
7
3

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