#C7899. Range Sum Query with Point Updates
Range Sum Query with Point Updates
Range Sum Query with Point Updates
You are given an array of integers and a series of queries. There are two types of queries:
- Update Query: Change the value at a specified position.
- Range Sum Query: Calculate the sum of elements in a given range.
This problem can be solved efficiently using a Binary Indexed Tree (or Fenwick Tree). The binary indexed tree supports point updates and range queries in \(O(\log n)\) time. The key formula used in BIT is:
\( index += index \& -index \)
Implement a program that processes these queries. Note that the index provided in the queries is 1-indexed.
inputFormat
The first line contains two integers \(n\) and \(q\) where \(n\) is the number of elements in the array and \(q\) is the number of queries.
The second line contains \(n\) integers representing the array.
The following \(q\) lines each contain a query. Each query is in one of the following formats:
1 index value
- Update the element at positionindex
tovalue
.2 left right
- Output the sum of the elements in the range [left, right] (inclusive).
All indices are 1-indexed.
outputFormat
For each query of type 2 (range sum query), output a single integer representing the sum of elements in the specified range. Print each result on a new line.
## sample5 5
1 2 3 4 5
2 1 3
1 2 5
2 1 3
1 3 10
2 2 5
6
9
24
</p>