You are given an array of integers. Implement a class that can support the following operations:
- Get the sum of elements in a specific index range
- Update one element to another value
For example:
Initial array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum(2, 5) = 18 # sum of 3, 4, 5, 6
update(3, -1) # update the element at index 3 to -1
sum(2, 5) = 13
这道题要求你设计一个数组数据结构,能够同时支持区间求和和单点更新。核心思路通常是用树状数组(Fenwick Tree)或线段树来维护前缀和 / 区间和,这样每次查询和修改都能在对数时间内完成。题目示例中,先查询索引区间 [2, 5] 的和得到 18,再把下标 3 的元素更新为 -1 后,同一区间的结果变为 13,体现了数据在修改后仍要保持高效可查询。
正文完