#K65177. Segment Tree Queries and Updates
Segment Tree Queries and Updates
Segment Tree Queries and Updates
You are given an array of n integers and m operations. The operations are of two types:
- Update: Change the value at a given index.
- Query: Calculate the sum of the elements in a given range.
The operations use 1-indexed positions. For the update operation the format is:
$$1\; index\; value$$which means update the element at position index to value.
For the query operation the format is:
This asks for the sum of the array elements from left to right (both inclusive). To solve this efficiently, you are required to use a segment tree. The segment tree should support point updates and range sum queries.
Note that each query result should be printed on a new line in the order of the queries encountered.
inputFormat
The first line contains two integers n and m, representing the number of elements in the array and the total number of operations, respectively.
The second line contains n space-separated integers denoting the initial array.
Then follow m lines where each line represents an operation.
- If the line starts with 1, it will be in the format:
1 index value
, which means update the element at position index to value. - If the line starts with 2, it will be in the format:
2 left right
, which means output the sum of the subarray from left to right (inclusive).
outputFormat
For every operation of type 2
, output the sum of the elements in the specified range on a new line.
5 3
1 2 3 4 5
1 3 10
2 1 3
2 2 5
13
21
</p>