#K81162. Range Sum Query with Updates using Fenwick Tree
Range Sum Query with Updates using Fenwick Tree
Range Sum Query with Updates using Fenwick Tree
You are given an array of integers and a sequence of operations. The operations are of two types:
- Update Operation: Replace the element at a given index with a new value.
- Sum Query: Compute the sum of the subarray from index l to r (inclusive).
You must perform these operations efficiently. A common technique is to use a Fenwick Tree (also known as a Binary Indexed Tree). The Fenwick Tree can be updated and queried in \(O(\log n)\) time.
Note: All indices in the input are 0-indexed.
Your solution should read the input from stdin and output the results of all sum queries to stdout.
inputFormat
The first line contains two integers n
and q
: the number of elements in the array and the number of operations, respectively.
The second line contains n
space-separated integers representing the array.
Each of the following q
lines contains an operation in one of the two formats:
u index value
: Update the element at positionindex
tovalue
.s l r
: Output the sum of the elements in the range [l
,r
].
outputFormat
For each sum query (operations starting with s
), output a single integer on its own line representing the sum of the specified subarray.
5 5
1 2 3 4 5
s 1 3
u 2 10
s 1 3
u 0 6
s 0 4
9
16
27
</p>