📜  查询给定矩阵中的按位或

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

给定一个由非负整数组成的N * N矩阵mat[][]和一些由子矩阵左上角和右下角组成的查询,任务是找到所有元素的按位或每个查询中给出的子矩阵。
例子:

朴素的方法:遍历子矩阵并找到该范围内所有数字的按位或。在最坏的情况下,这将为每个查询花费 O(n 2 ) 时间。
有效的方法:如果我们将整数看成一个二进制数,我们可以很容易地看出,要设置答案的i位的条件是设置子矩阵中至少一个整数的i位。
因此,我们将计算每个位的前缀计数。我们将利用这个发现在子矩阵整数的个数与第i位设置。如果它不为零,那么我们的答案的i位也将被设置。
为此,我们将创建一个 3d 数组, prefix_count[][][] ,其中prefix_count[i][x][y]将存储左上角位于{0, 0}和右下角{x, y}i位设置。参考
这篇文章来了解矩阵的prefix_count。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define bitscount 32
#define n 3
using namespace std;
 
// Array to store bit-wise
// prefix count
int prefix_count[bitscount][n][n];
 
// Function to find the prefix sum
void findPrefixCount(int arr[][n])
{
 
    // Loop for each bit
    for (int i = 0; i < bitscount; i++) {
 
        // Loop to find prefix-count
        // for each row
        for (int j = 0; j < n; j++) {
            prefix_count[i][j][0] = ((arr[j][0] >> i) & 1);
            for (int k = 1; k < n; k++) {
                prefix_count[i][j][k] = ((arr[j][k] >> i) & 1);
                prefix_count[i][j][k] += prefix_count[i][j][k - 1];
            }
        }
    }
 
    // Finding column-wise prefix
    // count
    for (int i = 0; i < bitscount; i++)
        for (int j = 1; j < n; j++)
            for (int k = 0; k < n; k++)
                prefix_count[i][j][k] += prefix_count[i][j - 1][k];
}
 
// Function to return the result for a query
int rangeOr(int x1, int y1, int x2, int y2)
{
 
    // 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 p;
        if (x1 == 0 and y1 == 0)
            p = prefix_count[i][x2][y2];
        else if (x1 == 0)
            p = prefix_count[i][x2][y2]
                - prefix_count[i][x2][y1 - 1];
        else if (y1 == 0)
            p = prefix_count[i][x2][y2]
                - prefix_count[i][x1 - 1][y2];
        else
            p = prefix_count[i][x2][y2]
                - prefix_count[i][x1 - 1][y2]
                - prefix_count[i][x2][y1 - 1]
                + prefix_count[i][x1 - 1][y1 - 1];
 
        // If count of variables with ith bit
        // set is greater than 0
        if (p != 0)
            ans = (ans | (1 << i));
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[][n] = { { 1, 2, 3 },
                     { 4, 5, 6 },
                     { 7, 8, 9 } };
 
    findPrefixCount(arr);
 
    int queries[][4] = { { 1, 1, 1, 1 }, { 1, 2, 2, 2 } };
    int q = sizeof(queries) / sizeof(queries[0]);
 
    for (int i = 0; i < q; i++)
        cout << rangeOr(queries[i][0],
                        queries[i][1],
                        queries[i][2],
                        queries[i][3])
             << endl;
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
 
    final static int bitscount = 32 ;
    final static int n = 3 ;
 
    // Array to store bit-wise
    // prefix count
    static int prefix_count[][][] = new int [bitscount][n][n];
     
    // Function to find the prefix sum
    static void findPrefixCount(int arr[][])
    {
     
        // Loop for each bit
        for (int i = 0; i < bitscount; i++)
        {
     
            // Loop to find prefix-count
            // for each row
            for (int j = 0; j < n; j++)
            {
                prefix_count[i][j][0] = ((arr[j][0] >> i) & 1);
                for (int k = 1; k < n; k++)
                {
                    prefix_count[i][j][k] = ((arr[j][k] >> i) & 1);
                    prefix_count[i][j][k] += prefix_count[i][j][k - 1];
                }
            }
        }
     
        // Finding column-wise prefix
        // count
        for (int i = 0; i < bitscount; i++)
            for (int j = 1; j < n; j++)
                for (int k = 0; k < n; k++)
                    prefix_count[i][j][k] += prefix_count[i][j - 1][k];
    }
     
    // Function to return the result for a query
    static int rangeOr(int x1, int y1, int x2, int y2)
    {
     
        // 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 p;
            if (x1 == 0 && y1 == 0)
                p = prefix_count[i][x2][y2];
            else if (x1 == 0)
                p = prefix_count[i][x2][y2]
                    - prefix_count[i][x2][y1 - 1];
            else if (y1 == 0)
                p = prefix_count[i][x2][y2]
                    - prefix_count[i][x1 - 1][y2];
            else
                p = prefix_count[i][x2][y2]
                    - prefix_count[i][x1 - 1][y2]
                    - prefix_count[i][x2][y1 - 1]
                    + prefix_count[i][x1 - 1][y1 - 1];
     
            // If count of variables with ith bit
            // set is greater than 0
            if (p != 0)
                ans = (ans | (1 << i));
        }
     
        return ans;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[][] = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
     
        findPrefixCount(arr);
     
        int queries[][] = { { 1, 1, 1, 1 }, { 1, 2, 2, 2 } };
        int q = queries.length;
     
        for (int i = 0; i < q; i++)
            System.out.println( rangeOr(queries[i][0],
                            queries[i][1],
                            queries[i][2],
                            queries[i][3]) );
    }
}
 
// This code is contributed by AnkitRai


Python3
# Python 3 implementation of the approach
bitscount = 32
n = 3
# Array to store bit-wise
# prefix count
prefix_count = [[[0 for i in range(n)] for j in range(n)] for k in range(bitscount)]
 
# Function to find the prefix sum
def findPrefixCount(arr):
    # Loop for each bit
    for i in range(bitscount):
        # Loop to find prefix-count
        # for each row
        for j in range(n):
            prefix_count[i][j][0] = ((arr[j][0] >> i) & 1)
            for k in range(1,n):
                prefix_count[i][j][k] = ((arr[j][k] >> i) & 1)
                prefix_count[i][j][k] += prefix_count[i][j][k - 1]
 
    # Finding column-wise prefix
    # count
    for i in range(bitscount):
        for j in range(1,n):
            for k in range(n):
                prefix_count[i][j][k] += prefix_count[i][j - 1][k]
 
# Function to return the result for a query
def rangeOr(x1, y1, x2, y2):
    # 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
        if (x1 == 0 and y1 == 0):
            p = prefix_count[i][x2][y2]
        elif (x1 == 0):
            p = prefix_count[i][x2][y2] - prefix_count[i][x2][y1 - 1]
        elif (y1 == 0):
            p = prefix_count[i][x2][y2] - prefix_count[i][x1 - 1][y2]
        else:
            p = prefix_count[i][x2][y2] - prefix_count[i][x1 - 1][y2] - prefix_count[i][x2][y1 - 1] + prefix_count[i][x1 - 1][y1 - 1];
 
        # If count of variables with ith bit
        # set is greater than 0
        if (p != 0):
            ans = (ans | (1 << i))
 
    return ans
 
# Driver code
if __name__ == '__main__':
    arr =  [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]
 
    findPrefixCount(arr)
    queries = [[1, 1, 1, 1],
                        [1, 2, 2, 2]]
    q = len(queries)
 
    for i in range(q):
        print(rangeOr(queries[i][0],queries[i][1],queries[i][2],queries[i][3]))
         
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
    static int bitscount = 32 ;
    static int n = 3 ;
 
    // Array to store bit-wise
    // prefix count
    static int [,,]prefix_count = new int [bitscount,n,n];
     
    // Function to find the prefix sum
    static void findPrefixCount(int [,]arr)
    {
     
        // Loop for each bit
        for (int i = 0; i < bitscount; i++)
        {
     
            // Loop to find prefix-count
            // for each row
            for (int j = 0; j < n; j++)
            {
                prefix_count[i,j,0] = ((arr[j,0] >> i) & 1);
                for (int k = 1; k < n; k++)
                {
                    prefix_count[i, j, k] = ((arr[j, k] >> i) & 1);
                    prefix_count[i, j, k] += prefix_count[i, j, k - 1];
                }
            }
        }
     
        // Finding column-wise prefix
        // count
        for (int i = 0; i < bitscount; i++)
            for (int j = 1; j < n; j++)
                for (int k = 0; k < n; k++)
                    prefix_count[i, j, k] += prefix_count[i, j - 1, k];
    }
     
    // Function to return the result for a query
    static int rangeOr(int x1, int y1, int x2, int y2)
    {
     
        // 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 p;
            if (x1 == 0 && y1 == 0)
                p = prefix_count[i, x2, y2];
            else if (x1 == 0)
                p = prefix_count[i, x2, y2]
                    - prefix_count[i, x2, y1 - 1];
            else if (y1 == 0)
                p = prefix_count[i, x2, y2]
                    - prefix_count[i, x1 - 1, y2];
            else
                p = prefix_count[i, x2, y2]
                    - prefix_count[i, x1 - 1, y2]
                    - prefix_count[i, x2, y1 - 1]
                    + prefix_count[i, x1 - 1, y1 - 1];
     
            // If count of variables with ith bit
            // set is greater than 0
            if (p != 0)
                ans = (ans | (1 << i));
        }
     
        return ans;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int [,]arr = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
     
        findPrefixCount(arr);
     
        int [,]queries = { { 1, 1, 1, 1 }, { 1, 2, 2, 2 } };
        int q = queries.GetLength(0);
     
        for (int i = 0; i < q; i++)
            Console.WriteLine( rangeOr(queries[i,0],
                            queries[i,1],
                            queries[i,2],
                            queries[i,3]) );
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


输出:

5
15

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

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