#K35957. Segment Tree for Range Sum Query and Update
Segment Tree for Range Sum Query and Update
Segment Tree for Range Sum Query and Update
You are given an array of n integers. You need to process q operations on this array. There are two types of operations:
- Update Operation: Type 1 operation of the form
1 index value
means update the element at positionindex
to the givenvalue
. - Query Operation: Type 2 operation of the form
2 left right
means calculate the sum of the elements fromleft
toright
(both inclusive).
Your task is to implement these operations efficiently using a segment tree. In particular, your implementation must handle both update and range sum query operations. The final output should print the result of each query operation to stdout, each on a new line.
Note: All array indices in the operations are 1-indexed.
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
integers representing the initial elements of the array.
The following q
lines each describe an operation in one of the following two formats:
1 index value
— Update the element at the givenindex
tovalue
.2 left right
— Query and print the sum fromleft
toright
(inclusive).
outputFormat
For every query operation (type 2), output the resulting sum on a new line.
## sample5 5
1 2 3 4 5
2 1 3
1 2 10
2 1 3
1 3 5
2 1 5
6
14
25
</p>