Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Implementing 2D Difference Arrays for Efficient Range Updates

Notes 2

Understanding Difference Arrays

Given an array arr, its difference array diff is defined as:

diff[0] = arr[0]
diff[i] = arr[i] - arr[i-1] for i > 0

To reconstruct the original array from the difference array:

arr[i] = diff[0] + diff[1] + ... + diff[i]

For range updates [l, r] with value x:

diff[l] += x
diff[r+1] -= x

Key properties:

  1. Enables efficient range additions
  2. Requires batch updates before queries
  3. diff[l] += x affects all element from l onward
  4. diff[r+1] -= x cencels the effect after r

Practical Example

def range_updates():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    idx = 0
    n = int(data[idx])
    m = int(data[idx+1])
    idx += 2
    
    arr = list(map(int, data[idx:idx+n]))
    idx += n
    
    diff = [0] * (n + 2)
    diff[0] = arr[0]
    for i in range(1, n):
        diff[i] = arr[i] - arr[i-1]
    
    for _ in range(m):
        l = int(data[idx]) - 1
        r = int(data[idx+1]) - 1
        val = int(data[idx+2])
        idx += 3
        
        diff[l] += val
        diff[r+1] -= val
    
    arr[0] = diff[0]
    for i in range(1, n):
        arr[i] = arr[i-1] + diff[i]
    
    print(' '.join(map(str, arr)))

range_updates()

2D Difference Arrays

The 2D diffreence array formula:

diff[i][j] = arr[i][j] - arr[i-1][j] - arr[i][j-1] + arr[i-1][j-1]

To update a rectangular region (x1,y1) to (x2,y2) with value x:

diff[x1][y1] += x
diff[x1][y2+1] -= x
diff[x2+1][y1] -= x
diff[x2+1][y2+1] += x

Implementation example:

def process_2d_array():
    n, m = map(int, input().split())
    matrix = [[0]*(m+2) for _ in range(n+2)]
    diff = [[0]*(m+2) for _ in range(n+2)]
    
    for i in range(1, n+1):
        row = list(map(int, input().split()))
        for j in range(1, m+1):
            matrix[i][j] = row[j-1]
    
    for i in range(1, n+1):
        for j in range(1, m+1):
            diff[i][j] = matrix[i][j] - matrix[i-1][j] - matrix[i][j-1] + matrix[i-1][j-1]
    
    # Example update
    x1, y1, x2, y2, val = 1, 1, 2, 2, 5
    diff[x1][y1] += val
    diff[x1][y2+1] -= val
    diff[x2+1][y1] -= val
    diff[x2+1][y2+1] += val
    
    # Reconstruct matrix
    for i in range(1, n+1):
        for j in range(1, m+1):
            matrix[i][j] = matrix[i-1][j] + matrix[i][j-1] - matrix[i-1][j-1] + diff[i][j]
    
    for row in matrix[1:n+1]:
        print(' '.join(map(str, row[1:m+1])))

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.