Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-278

Range Update Point Query

Problem

Start with an array of n zeros and process operations: [1, left, right, value] adds value to every index in the inclusive range, and [2, index] asks for the current value at an index. Return the query results in order.

Use a difference array where a range update touches only two positions, and a query takes the prefix sum. Values may be negative.

Hint

Adding val at left and subtracting it at right + 1 makes the prefix sum apply the range.

Test cases

case 1
in: [5,[[1,1,3,2],[2,0],[2,2],[1,0,2,1],[2,1],[2,4]]]
expected: [0,2,3,0]
case 2
in: [1,[[2,0]]]
expected: [0]
case 3
in: [3,[]]
expected: []
case 4
in: [2,[[1,0,1,5],[2,1],[1,0,0,-5],[2,0]]]
expected: [5,0]
Starter codePython
def range_update_point_query(n, operations):
    # Your code here
    pass

4 test cases · run your code against them in the editor