#C513. Range Sum Query with Updates
Range Sum Query with Updates
Range Sum Query with Updates
You are given an array of n integers and a sequence of q queries. There are two types of queries:
- Update Query ('1'): Update the element at index i to a new value.
- Sum Query ('2'): Compute the sum of the elements in the range [l, r].
Your task is to process these queries efficiently. A typical approach is to use a Fenwick Tree (Binary Indexed Tree), which allows both update and prefix sum queries in O(log n) time. The sum of a range [l, r] can be computed as \( sum(r) - sum(l-1) \) where \( sum(x) \) is the prefix sum up to index x. The solution must handle queries from standard input and output the results for every sum query to standard output.
inputFormat
The input is read from standard input in the following format:
- An integer n, representing the number of elements in the array.
- n space-separated integers representing the array elements.
- An integer q, representing the number of queries.
- q lines follow, each containing a query. Each query is in one of the formats:
- 1 i x — Update the element at index i to x.
- 2 l r — Output the sum of the elements in the range from index l to r (inclusive).
outputFormat
For each query of type '2', output a single line with the sum of the array elements in the specified range.
## sample5
1 2 3 4 5
3
2 1 3
1 2 10
2 1 3
9
16
</p>