📜  二维差分阵列

📅  最后修改于: 2021-05-05 00:04:40             🧑  作者: Mango

给定尺寸的矩阵N * M和2D阵列的查询[] []与以下形式的每个查询{K,R1,C1,R2,C2},任务是与k添加到所有的C厄尔在子矩阵kting (r1,c1)(r2,c2)

例子:

方法:这个想法是基于差分数组的。 O(1)中的范围更新查询。请按照以下步骤解决问题:

  • 初始化一个二维差分数组D [] [] ,使得D [i] [j]存储A [i] [j] – A [i] [j – 1] (对于0≤i≤N和0 )或D [i] [j] = A [i] [j]否则。
  • 遍历每一行并计算并存储相邻元素之间的差异。
  • 要将子矩阵(r1,c1)更新为(r2,c2) ,从r1遍历到r2 (假设r1 D [i] [c1] = D [i] [c1] + k并且D [i] [c2 +1] = D [i] [c2 +1] – k
  • 最后,打印的改性阵列d [i] [j] + A [i] [j-1]对于j> 0d [i] [j]对于j = 0。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
#define N 3
#define M 3
 
// Function to initialize the difference array
void intializeDiff(int D[N][M + 1],
                   int A[N][M])
{
    for (int i = 0; i < N; i++) {
        D[i][0] = A[i][0];
        D[i][M] = 0;
    }
    for (int i = 0; i < N; i++) {
        for (int j = 1; j < M; j++)
            D[i][j] = A[i][j] - A[i][j - 1];
    }
}
 
// Function to add k to the specified
// submatrix (r1, c1) to (r2, c2)
void update(int D[N][M + 1], int k,
            int r1, int c1, int r2,
            int c2)
{
    for (int i = r1; i <= r2; i++) {
        D[i][c1] += k;
        D[i][c2 + 1] -= k;
    }
}
 
// Function to print the modified array
void printArray(int A[N][M], int D[N][M + 1])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (j == 0)
                A[i][j] = D[i][j];
            else
                A[i][j] = D[i][j] + A[i][j - 1];
            cout << A[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Function to perform the given queries
void performQueries(int A[N][M],
                    vector > Queries)
{
    // Difference array
    int D[N][M + 1];
 
    // Function to initialize
    // the difference array
    intializeDiff(D, A);
 
    // Count of queries
    int Q = Queries.size();
 
    // Perform Queries
    for (int i = 0; i < Q; i++) {
        update(D, Queries[i][0],
               Queries[i][1], Queries[i][2],
               Queries[i][3], Queries[i][4]);
    }
 
    printArray(A, D);
}
 
// Driver Code
int main()
{
 
    // Given Matrix
    int A[N][M] = { { 1, 2, 3 },
                    { 1, 1, 0 },
                    { 4, -2, 2 } };
 
    // Given Queries
    vector > Queries
        = { { 2, 0, 0, 1, 1 },
            { -1, 1, 0, 2, 2 } };
 
    performQueries(A, Queries);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
static int N = 3;
static int M = 3;
  
// Function to initialize the difference array
static void intializeDiff(int D[][], int A[][])
{
    for(int i = 0; i < N; i++)
    {
        D[i][0] = A[i][0];
        D[i][M] = 0;
    }
    for(int i = 0; i < N; i++)
    {
        for(int j = 1; j < M; j++)
            D[i][j] = A[i][j] - A[i][j - 1];
    }
}
    
// Function to add k to the specified
// submatrix (r1, c1) to (r2, c2)
static void update(int D[][], int k,
                   int r1, int c1,
                   int r2, int c2)
{
    for(int i = r1; i <= r2; i++)
    {
        D[i][c1] += k;
        D[i][c2 + 1] -= k;
    }
}
    
// Function to print the modified array
static void printArray(int A[][], int D[][])
{
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            if (j == 0)
                A[i][j] = D[i][j];
            else
                A[i][j] = D[i][j] + A[i][j - 1];
                  
            System.out.print(A[i][j] + " ");
        }
        System.out.println();
    }
}
    
// Function to perform the given queries
static void performQueries(int A[][],
                           Vector> Queries)
{
     
    // Difference array
    int D[][] = new int[N][M + 1];
    
    // Function to initialize
    // the difference array
    intializeDiff(D, A);
    
    // Count of queries
    int Q = Queries.size();
    
    // Perform Queries
    for(int i = 0; i < Q; i++)
    {
        update(D, Queries.get(i).get(0),
                  Queries.get(i).get(1),
                  Queries.get(i).get(2),
                  Queries.get(i).get(3),
                  Queries.get(i).get(4));
    }
    printArray(A, D);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Matrix
    int A[][] = { { 1, 2, 3 },
                  { 1, 1, 0 },
                  { 4, -2, 2 } };
      
    // Given Queries
    Vector> Queries = new Vector>();
    Vector list1 = new Vector();
    list1.add(2);
    list1.add(0);
    list1.add(0);
    list1.add(1);
    list1.add(1);
     
    Vector list2 = new Vector();
    list2.add(-1);
    list2.add(1);
    list2.add(0);
    list2.add(2);
    list2.add(2);
     
    Queries.add(list1);
    Queries.add(list2);
      
    performQueries(A, Queries);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 Program to implement
# the above approach
N = 3
M = 3
  
# Function to initialize the difference array
def intializeDiff(D, A):
    for i in range(N):       
        D[i][0] = A[i][0];
        D[i][M] = 0;  
    for i in range(N):
        for j in range(1, M):
            D[i][j] = A[i][j] - A[i][j - 1];
      
# Function to add k to the specified
# submatrix (r1, c1) to (r2, c2)
def update(D, k, r1, c1, r2, c2):
    for i in range(r1, r2 + 1):
        D[i][c1] += k;
        D[i][c2 + 1] -= k;
 
# Function to print the modified array
def printArray(A, D):  
    for i in range(N):
        for j in range(M):  
            if (j == 0):
                A[i][j] = D[i][j];
            else:
                A[i][j] = D[i][j] + A[i][j - 1];               
            print(A[i][j], end = ' ')
        print()
         
# Function to perform the given queries
def performQueries(A, Queries):
 
    # Difference array
    D = [[0 for j in range(M + 1)] for i in range(N)]
  
    # Function to initialize
    # the difference array
    intializeDiff(D, A);
  
    # Count of queries
    Q = len(Queries)
  
    # Perform Queries
    for i in range(Q):   
        update(D, Queries[i][0],
               Queries[i][1], Queries[i][2],
               Queries[i][3], Queries[i][4]);
      
    printArray(A, D);
  
# Driver Code
if __name__=='__main__':
  
    # Given Matrix
    A = [ [ 1, 2, 3 ],
                    [ 1, 1, 0 ],
                    [ 4, -2, 2 ] ];
  
    # Given Queries
    Queries = [ [ 2, 0, 0, 1, 1 ],[ -1, 1, 0, 2, 2 ] ];
  
    performQueries(A, Queries);
 
  # This code is contributed by Pratham76


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
static int N = 3;
static int M = 3;
 
// Function to initialize the difference array
static void intializeDiff(int[,] D, int[,] A)
{
    for(int i = 0; i < N; i++)
    {
        D[i, 0] = A[i, 0];
        D[i, M] = 0;
    }
    for(int i = 0; i < N; i++)
    {
        for(int j = 1; j < M; j++)
            D[i, j] = A[i, j] - A[i, j - 1];
    }
}
   
// Function to add k to the specified
// submatrix (r1, c1) to (r2, c2)
static void update(int[,] D, int k,
                   int r1, int c1,
                   int r2, int c2)
{
    for(int i = r1; i <= r2; i++)
    {
        D[i, c1] += k;
        D[i, c2 + 1] -= k;
    }
}
   
// Function to print the modified array
static void printArray(int[,] A, int[,] D)
{
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            if (j == 0)
                A[i, j] = D[i, j];
            else
                A[i, j] = D[i, j] + A[i, j - 1];
                 
            Console.Write(A[i, j] + " ");
        }
        Console.WriteLine();
    }
}
   
// Function to perform the given queries
static void performQueries(int[,] A,
                 List> Queries)
{
     
    // Difference array
    int[,] D = new int[N, M + 1];
   
    // Function to initialize
    // the difference array
    intializeDiff(D, A);
   
    // Count of queries
    int Q = Queries.Count;
   
    // Perform Queries
    for(int i = 0; i < Q; i++)
    {
        update(D, Queries[i][0],
                  Queries[i][1], Queries[i][2],
                  Queries[i][3], Queries[i][4]);
    }
    printArray(A, D);
}
 
// Driver Code
static void Main()
{
     
    // Given Matrix
    int[,] A = { { 1, 2, 3 },
                 { 1, 1, 0 },
                 { 4, -2, 2 } };
     
    // Given Queries
    List> Queries = new List>();
    Queries.Add(new List{ 2, 0, 0, 1, 1 });
    Queries.Add(new List{ -1, 1, 0, 2, 2 });
     
    performQueries(A, Queries);
}
}
 
// This code is contributed by divyesh072019


输出:

3 4 3
2 2 -1
3 -3 1

时间复杂度: O(N * M)
辅助空间: O(N * M)