P19 · 2 seconds · 256 MB
Fenwick Queries
`nums` is a 0-based array. `queries` is a list of operations: - `[1, i, v]` — set `nums[i] = v` (overwrite, not add) - `[2, l, r]` — ask for the sum `nums[l] + … + nums[r]` (inclusive) Return an array of the answers to every type-2 query, in order.
function fenwickQueries(nums, queries) → number[]
CONSTRAINTS
- 1 ≤ nums.length ≤ 2000
- 1 ≤ queries.length ≤ 2000
- 0 ≤ i, l, r < nums.length
- l ≤ r
- -10⁴ ≤ nums[j], v ≤ 10⁴
Input: nums = [1, 2, 3, 4], queries = [[2, 0, 3], [1, 1, 10], [2, 0, 3]]
Output: [10, 18]
Initial sum 10. After nums[1]=10 the array is [1,10,3,4] and the sum is 18.
Input: nums = [5], queries = [[2, 0, 0], [1, 0, 0], [2, 0, 0]]
Output: [5, 0]
1
2
3
4
5
6
7
8
Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.