#K64252. Range Update and Sum Query
Range Update and Sum Query
Range Update and Sum Query
You are given a sequence of N integers. You need to perform Q operations on this sequence. There are two types of operations:
- Add Operation: Given four integers 1 l r V, add V to each element in the index range \(l\) to \(r\) (inclusive).
- Query Operation: Given three integers 2 l r, compute the sum of the elements from index \(l\) to \(r\) (inclusive) and output the result.
Operations are applied sequentially on the sequence, which is 0-indexed. Formally, if the sequence is \(a_0,a_1,\dots,a_{N-1}\), an Add Operation \(1\ l\ r\ V\) updates the sequence as: \[ a_i = a_i + V \quad \text{for } l \le i \le r. \] A Query Operation \(2\ l\ r\) requires the output of the sum: \[ \sum_{i=l}^{r} a_i. \]
inputFormat
The first line contains an integer T representing the number of test cases.
For each test case:
- The first line contains two integers N and Q separated by a space, where N is the number of elements and Q is the number of operations.
- The second line contains N space-separated integers representing the initial sequence.
- Each of the next Q lines contains an operation in one of the following formats:
- 1 l r V for an Add Operation.
- 2 l r for a Query Operation.
outputFormat
For each Query Operation in all test cases, output the result on a separate line.
## sample1
5 4
1 2 3 4 5
1 1 3 2
2 1 3
1 0 2 -1
2 0 4
15
18
</p>