#K14061. Segment Tree Range Sum Queries
Segment Tree Range Sum Queries
Segment Tree Range Sum Queries
This problem involves processing a series of update and range sum queries on an array using a Segment Tree data structure. You are required to build a segment tree from the given array, then process q
queries. There are two types of queries:
- Update Query: Format
1 i x
– Update the value at index i (`1-indexed`) to x. - Range Sum Query: Format
2 L R
– Output the sum of elements from index L to R (both inclusive, 1-indexed).
Your task is to return the results for the range sum queries after processing all q
queries.
The segment tree must be implemented efficiently to handle the queries within the given constraints.
inputFormat
The first line of input contains two integers n
and q
, where n
is the size of the array and q
is the number of queries.
The second line contains n
space-separated integers representing the array elements.
Each of the next q
lines contains a query in one of the following two formats:
1 i x
— Update thei
-th element (1-indexed) tox
.2 L R
— Query the sum of elements from indexL
toR
(1-indexed).
outputFormat
For each query of type 2
, output a single line containing the sum of the specified range.
5 3
1 2 3 4 5
2 1 3
1 3 10
2 2 4
6
16
</p>