#K67467. Dynamic Range Sum Query with Segment Tree
Dynamic Range Sum Query with Segment Tree
Dynamic Range Sum Query with Segment Tree
You are given an array of integers and a series of queries. There are two types of queries:
- Update Query:
1 X Y
— Update the element at position X to the value Y. - Sum Query:
2 L R
— Compute the sum of the subarray from index L to R (inclusive).
Your task is to process these queries efficiently. A common data structure to handle such range queries and updates is the Segment Tree.
The sum for a query of type 2 is defined mathematically as \[ S = \sum_{i=L}^{R} a_i \] where \(a_i\) represents the element at the i-th position.
Input is taken from stdin
and output should be printed to stdout
. For each query of type 2, print the computed sum on a new line.
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 initial array.
The following Q
lines each contain a query in one of the following formats:
1 X Y
— Update the element at positionX
toY
.2 L R
— Output the sum of the subarray fromL
toR
.
outputFormat
For each query of type 2
, output a single line containing the sum of the specified subarray.
5 5
1 2 3 4 5
2 1 3
1 3 10
2 2 5
1 5 6
2 1 5
6
21
23
</p>