Q查询更新后所有可能的非空子数组的按位与的按位或
给定一个由N个正整数组成的数组arr[]和一个[L, R]类型的查询数组Q[] ,任务是找到该数组所有可能的非空子数组的按位与的按位或在为每个查询更新索引L到R处的数组元素之后。
例子:
Input: arr[ ] = {1, 2, 3}, Q[ ] = {{1, 4}, {3, 0}}
Output: 7 6
Explanation:
All subarrays are {1}, {2}, {3}, {1, 2}, {2, 3} and {1, 2, 3}
For query 1: Update arr[1] = 4, then new array is {4, 2, 3}. The bitwise & of all subarrays are 4, 2, 3, 0, 2, 0 and the bitwise OR( {4, 2, 3, 0, 2, 0}) equals 7.
For query 2: Update arr[3] = 0, then new array is {4, 2, 0}. The bitwise & of all subarrays are 4, 2, 0, 0, 0, 0 and the bitwise OR( {4, 2, 0, 0, 0, 0}) equals 6.
Input: arr[ ] = {1, 2, 1}, Q[ ] = {{2, 1}}
Output: 1
朴素方法:解决问题的最简单方法是遍历数组Q[]并为每个查询将数组元素arr[L]更新为R并找到所有子数组及其按位与并将它们存储在新数组中。存储位与后,找到新形成的数组的位或。
时间复杂度: O(N 2 Q)
辅助空间: O(N 2 )
有效方法:上述方法也可以通过使用所有生成的子数组的按位与结果的按位或与数组中存在的所有元素的按位或结果相同的事实来优化。
因此,想法是在每次更新数组后执行查询并打印所有数组元素的按位或值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Bitwise OR
// of Bitwise AND of all possible
// subarrays after performing the
// every query
void performQuery(vector arr,
vector> Q)
{
// Traversing each pair
// of the query
for (int i = 0; i < Q.size(); i++) {
// Stores the Bitwise OR
int or1 = 0;
// Updating the array
int x = Q[i][0];
arr[x - 1] = Q[i][1];
// Find the Bitwise OR of
// new updated array
for (int j = 0; j < arr.size(); j++) {
or1 = or1 | arr[j];
}
// Print the ans
cout< arr({ 1, 2, 3 });
vector v1({1,4});
vector v2({3,0});
vector> Q;
Q.push_back(v1);
Q.push_back(v2);
performQuery(arr, Q);
}
// This code is contributed by ipg2016107.
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the Bitwise OR
// of Bitwise AND of all possible
// subarrays after performing the
// every query
static void performQuery(int arr[],
int Q[][])
{
// Traversing each pair
// of the query
for (int i = 0; i < Q.length; i++) {
// Stores the Bitwise OR
int or = 0;
// Updating the array
int x = Q[i][0];
arr[x - 1] = Q[i][1];
// Find the Bitwise OR of
// new updated array
for (int j = 0; j < arr.length; j++) {
or = or | arr[j];
}
// Print the ans
System.out.print(or + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3 };
int Q[][] = { { 1, 4 }, { 3, 0 } };
performQuery(arr, Q);
}
}
Python3
# Python program for the above approach
# Function to find the Bitwise OR
# of Bitwise AND of all possible
# subarrays after performing the
# every query
def performQuery(arr, Q):
# Traversing each pair
# of the query
for i in range (0, len(Q)):
# Stores the Bitwise OR
orr = 0
# Updating the array
x = Q[i][0]
arr[x - 1] = Q[i][1]
# Find the Bitwise OR of
# new updated array
for j in range(0,len(arr)):
orr = orr | arr[j]
# Print the ans
print(orr ,end= " ")
# Driver Code
arr = [1, 2, 3]
Q = [[1, 4] , [3, 0]]
performQuery(arr, Q)
# This code is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the Bitwise OR
// of Bitwise AND of all possible
// subarrays after performing the
// every query
static void performQuery(int []arr, int [,]Q)
{
// Traversing each pair
// of the query
for (int i = 0; i < Q.Length; i++) {
// Stores the Bitwise OR
int or = 0;
// Updating the array
int x = Q[i,0];
arr[x - 1] = Q[i,1];
// Find the Bitwise OR of
// new updated array
for (int j = 0; j < arr.Length; j++) {
or = or | arr[j];
}
// Print the ans
Console.Write(or + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3 };
int [,]Q = { { 1, 4 }, { 3, 0 } };
performQuery(arr, Q);
}
}
// This code is contributed by shivanisinghss2110
Javascript
7 6
时间复杂度: O(N*Q)
辅助空间: O(1)