#K45892. Array Operations: Updates and Range Sum Queries
Array Operations: Updates and Range Sum Queries
Array Operations: Updates and Range Sum Queries
You are given an array of n integers. You need to process q operations on this array. There are two types of operations:
- Type 1 (Update): Given an index x and a value v, update the element at index x to v.
- Type 2 (Query): Given two indices l and r, calculate the sum of the subarray from index l to index r (both inclusive). In mathematical form, for an array \(a_1,a_2,\ldots,a_n\), the query asks for \(\sum_{i=l}^{r}a_i\).
Your task is to perform these operations and output the results for every query of type 2. Operations are given sequentially and the updates affect subsequent queries.
inputFormat
The input is given from standard input in the following format:
n q a1 a2 ... an op1 x1 y1 op2 x2 y2 ... opq xq yq
Here, the first line contains two integers n and q. The second line contains n integers representing the initial values of the array. Each of the following q lines represents an operation. For an operation:
- If
op = 1
, the operation is an update, wherex
is the index to update andy
is the new value. - If
op = 2
, the operation is a query, wherex
andy
represent the indices l and r (1-indexed) for which the subarray sum is required.
outputFormat
For each query operation (where op = 2
), output the sum of the subarray specified by the given indices. Each result should be printed on a separate line on standard output.
5 5
1 2 3 4 5
2 1 3
1 2 10
2 2 4
1 3 6
2 1 5
6
17
26
</p>