📌  相关文章
📜  在给定数组的索引范围 [L, R] 中查询按位 OR

📅  最后修改于: 2021-09-22 10:09:36             🧑  作者: Mango

给定一个由NQ查询组成的数组arr[] ,其中包含范围[L, R] 。任务是找到该索引范围内所有元素的按位或。
例子:

天真的方法:遍历范围并找到该范围内所有数字的按位或。对于每个查询,这将花费 O(n) 时间。
有效的方法:如果我们将整数视为二进制数,我们可以很容易地看出,要设置答案的i位的条件是应设置范围[L, R]中任何整数的i位.
因此,我们将为每个位计算前缀计数。我们将使用它来查找设置了i位的范围内的整数数量。如果它大于0,那么我们答案的i位也将被设置。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define MAX 100000
#define bitscount 32
using namespace std;
 
// Array to store bit-wise
// prefix count
int prefix_count[bitscount][MAX];
 
// Function to find the prefix sum
void findPrefixCount(int arr[], int n)
{
 
    // Loop for each bit
    for (int i = 0; i < bitscount; i++) {
        // Loop to find prefix count
        prefix_count[i][0] = ((arr[0] >> i) & 1);
        for (int j = 1; j < n; j++) {
            prefix_count[i][j] = ((arr[j] >> i) & 1);
            prefix_count[i][j] += prefix_count[i][j - 1];
        }
    }
}
 
// Function to answer query
int rangeOr(int l, int r)
{
 
    // To store the answer
    int ans = 0;
 
    // Loop for each bit
    for (int i = 0; i < bitscount; i++) {
        // To store the number of variables
        // with ith bit set
        int x;
        if (l == 0)
            x = prefix_count[i][r];
        else
            x = prefix_count[i][r]
                - prefix_count[i][l - 1];
 
        // Condition for ith bit
        // of answer to be set
        if (x != 0)
            ans = (ans | (1 << i));
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 7, 5, 3, 5, 2, 3 };
    int n = sizeof(arr) / sizeof(int);
 
    findPrefixCount(arr, n);
 
    int queries[][2] = { { 1, 3 }, { 4, 5 } };
    int q = sizeof(queries) / sizeof(queries[0]);
 
    for (int i = 0; i < q; i++)
        cout << rangeOr(queries[i][0],
                        queries[i][1])
             << endl;
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
static int MAX = 100000;
static int bitscount = 32;
 
// Array to store bit-wise
// prefix count
static int [][]prefix_count = new int [bitscount][MAX];
 
// Function to find the prefix sum
static void findPrefixCount(int arr[], int n)
{
 
    // Loop for each bit
    for (int i = 0; i < bitscount; i++)
    {
        // Loop to find prefix count
        prefix_count[i][0] = ((arr[0] >> i) & 1);
        for (int j = 1; j < n; j++)
        {
            prefix_count[i][j] = ((arr[j] >> i) & 1);
            prefix_count[i][j] += prefix_count[i][j - 1];
        }
    }
}
 
// Function to answer query
static int rangeOr(int l, int r)
{
 
    // To store the answer
    int ans = 0;
 
    // Loop for each bit
    for (int i = 0; i < bitscount; i++)
    {
        // To store the number of variables
        // with ith bit set
        int x;
        if (l == 0)
            x = prefix_count[i][r];
        else
            x = prefix_count[i][r]
                - prefix_count[i][l - 1];
 
        // Condition for ith bit
        // of answer to be set
        if (x != 0)
            ans = (ans | (1 << i));
    }
 
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
     
    int arr[] = { 7, 5, 3, 5, 2, 3 };
    int n = arr.length;
    findPrefixCount(arr, n);
    int queries[][] = { { 1, 3 }, { 4, 5 } };
    int q = queries.length;
    for (int i = 0; i < q; i++)
            System.out.println (rangeOr(queries[i][0],queries[i][1]));
 
}
}
 
// This code is contributed by Tushil.


Python3
# Python3 implementation of the approach
 
import numpy as np
 
MAX = 100000
bitscount = 32
 
# Array to store bit-wise
# prefix count
prefix_count = np.zeros((bitscount,MAX));
 
# Function to find the prefix sum
def findPrefixCount(arr, n) :
 
    # Loop for each bit
    for i in range(0, bitscount) :
        # Loop to find prefix count
        prefix_count[i][0] = ((arr[0] >> i) & 1);
         
        for j in range(1, n) :
            prefix_count[i][j] = ((arr[j] >> i) & 1);
            prefix_count[i][j] += prefix_count[i][j - 1];
 
# Function to answer query
def rangeOr(l, r) :
 
    # To store the answer
    ans = 0;
 
    # Loop for each bit
    for i in range(bitscount) :
         
        # To store the number of variables
        # with ith bit set
        x = 0;
         
        if (l == 0) :
            x = prefix_count[i][r];
        else :
            x = prefix_count[i][r] - prefix_count[i][l - 1];
 
        # Condition for ith bit
        # of answer to be set
        if (x != 0) :
            ans = (ans | (1 << i));
             
    return ans;
 
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 7, 5, 3, 5, 2, 3 ];
    n = len(arr);
 
    findPrefixCount(arr, n);
 
    queries = [ [ 1, 3 ], [ 4, 5 ] ];
     
    q = len(queries);
 
    for i in range(q) :
        print(rangeOr(queries[i][0], queries[i][1]));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
static int MAX = 100000;
static int bitscount = 32;
 
// Array to store bit-wise
// prefix count
static int [,]prefix_count = new int [bitscount,MAX];
 
// Function to find the prefix sum
static void findPrefixCount(int []arr, int n)
{
 
    // Loop for each bit
    for (int i = 0; i < bitscount; i++)
    {
        // Loop to find prefix count
        prefix_count[i,0] = ((arr[0] >> i) & 1);
        for (int j = 1; j < n; j++)
        {
            prefix_count[i,j] = ((arr[j] >> i) & 1);
            prefix_count[i,j] += prefix_count[i,j - 1];
        }
    }
}
 
// Function to answer query
static int rangeOr(int l, int r)
{
 
    // To store the answer
    int ans = 0;
 
    // Loop for each bit
    for (int i = 0; i < bitscount; i++)
    {
        // To store the number of variables
        // with ith bit set
        int x;
        if (l == 0)
            x = prefix_count[i,r];
        else
            x = prefix_count[i,r]
                - prefix_count[i,l - 1];
 
        // Condition for ith bit
        // of answer to be set
        if (x != 0)
            ans = (ans | (1 << i));
    }
 
    return ans;
}
 
// Driver code
public static void Main (String[] args)
{
     
    int []arr = { 7, 5, 3, 5, 2, 3 };
    int n = arr.Length;
    findPrefixCount(arr, n);
    int [,]queries = { { 1, 3 }, { 4, 5 } };
    int q = queries.GetLength(0);
    for (int i = 0; i < q; i++)
            Console.WriteLine(rangeOr(queries[i,0],queries[i,1]));
 
}
}
 
// This code is contributed by 29AjayKumar


PHP
> $i) & 1);
        for ($j = 1; $j < $n; $j++)
        {
            $prefix_count[$i][$j] = (($arr[$j] >> $i) & 1);
            $prefix_count[$i][$j] += $prefix_count[$i][$j - 1];
        }
    }
}
 
// Function to answer query
function rangeOr($l, $r)
{
    global $MAX,$bitscount,$prefix_count;
     
    // To store the answer
    $ans = 0;
 
    // Loop for each bit
    for ($i = 0; $i < $bitscount; $i++)
    {
        // To store the number of variables
        // with ith bit set
         
        if ($l == 0)
            $x = $prefix_count[$i][$r];
        else
            $x = $prefix_count[$i][$r]
                - $prefix_count[$i][l - 1];
 
        // Condition for ith bit
        // of answer to be set
        if ($x != 0)
            $ans = ($ans | (1 << $i));
    }
 
    return $ans;
}
 
    // Driver code
    $arr =array( 7, 5, 3, 5, 2, 3 );
    $n = sizeof($arr) / sizeof($arr[0]);
 
    findPrefixCount($arr, $n);
 
    $queries = array(array( 1, 3 ), array( 4, 5 ));
    $q = sizeof($queries) / sizeof($queries[0]);
 
    for ($i = 0; $i < $q; $i++)
        echo rangeOr($queries[$i][0],
                        $queries[$i][1])."\n";
 
    return 0;
     
    // This code is contributed by ChitraNayal
?>


Javascript


输出:

7
3

预计算的时间复杂度为 O(n) 并且每个查询都可以在 O(1) 中回答

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程