← 返回 databricks 的题目列表Lazy Array (Delayed Evaluation Array)
类型:online_judge
You are given a length-n array abstract data structure LazyArray, whose initial values are either provided or default to 0. The structure supports lazy (delayed) updates that are recorded and only materialized when values are queried.
Design and implement LazyArray supporting at least:
range_add(l, r, delta): add delta to every element in index range [l, r].
get(i): return the final value at index i after applying all historical updates.
Constraints:
0 <= l <= r < n, 0 <= i < n
Make range_add as efficient as possible; get should also be efficient.
If initial values are non-zero, the initial array will be provided in input.
Provide time-complexity analysis.
Scale example: n up to 2e5, number of operations q up to 2e5.
Example
Input
5 5
add 1 3 2
get 0
get 1
add 0 4 -1
get 3
Output
0
2
1