#C10947. Processing Array Queries
Processing Array Queries
Processing Array Queries
You are given an array of n integers and a series of q queries. There are two types of queries:
- Update query: The query is in the form
1 i x
, which means add the value x to the element at index i (1-indexed). - Sum query: The query is in the form
2 l r
, which asks you to calculate the sum of the subarray from index l to r (inclusive, 1-indexed). Formally, you need to compute \(\sum_{i=l}^{r} a_i\).
After processing all queries, output the result of each sum query in the order they appear. Note that update queries modify the array permanently before subsequent queries.
Examples:
Input: 5 1 2 3 4 5 4 2 1 3 1 2 5 2 1 3 2 3 5</p>Output: 6 11 12
Explanation: Initially, the array is [1, 2, 3, 4, 5]. The first query outputs the sum of elements from index 1 to 3: 1+2+3 = 6. After the update query (adding 5 to index 2), the array becomes [1, 7, 3, 4, 5]. The next query gives 1+7+3 = 11, and the last query sums 3+4+5 = 12.
inputFormat
The input is given via standard input (stdin
) in the following format:
- An integer
n
denoting the size of the array. - A line containing
n
space-separated integers representing the array elements. - An integer
q
denoting the number of queries. q
lines follow, each representing a query:- For an update query:
1 i x
- For a sum query:
2 l r
outputFormat
For each sum query, print the corresponding sum on a separate line to standard output (stdout
).
5
1 2 3 4 5
4
2 1 3
1 2 5
2 1 3
2 3 5
6
11
12
</p>