#K54587. Range Sum Query and Update
Range Sum Query and Update
Range Sum Query and Update
Given an array of integers, your task is to process a series of queries efficiently. There are two types of queries:
- Update Query: Update the value at a given index.
- Range Sum Query: Compute the sum of the elements in a specific range, inclusive (i.e. from \(l\) to \(r\)).
All indices in the queries are 1-indexed. A query starting with 1
represents an update operation and a query starting with 2
represents a range sum query.
For the range sum query, compute the result as \[ \text{result} = \sum_{i=l}^{r} a_i \] using an efficient data structure (for example, a segment tree) to handle large datasets with up to \(10^5\) elements and queries.
inputFormat
The first line contains two space-separated 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\) space-separated integers representing the array elements.
Each of the following \(q\) lines represents a query, in one of the following formats:
1 index value
: Update the element at the positionindex
tovalue
.2 left right
: Output the sum of elements from positionleft
toright
(inclusive).
outputFormat
For each range sum query (i.e. queries starting with 2
), output the computed sum on a new line.
5 3
1 2 3 4 5
2 1 3
1 3 10
2 1 3
6
13
</p>