Problem 683
Range Add and Range Sum
Hard · 30 pts · Array, Fenwick Tree, Segment Tree
Problem
You are given an array of N integers (positions 1 to N) and Q operations. '1 l r v' adds v to every element from position l to r. '2 l r' asks for the sum of the elements from l to r. Print the answer to every type 2 query. Both operations must run fast, so use a segment tree with lazy propagation, or two Fenwick trees: with B1 and B2 storing range additions, the prefix sum up to i is (initial prefix) + B1(i) × i - B2(i).
Input
The first line contains N and Q. The second line contains the N values. Each of the next Q lines contains one operation. There is at least one query of type 2.
Output
Print the answer to each type 2 query, one per line.
Constraints
1 <= N, Q <= 100000; -10^9 <= A[i] <= 10^9; -1000 <= v <= 1000
Example input
5 3 1 2 3 4 5 2 2 4 1 1 3 10 2 2 4
Example output
9 29
Explanation
With A = 1 2 3 4 5, the sum of positions 2 to 4 is 9. After adding 10 to positions 1 to 3, the array is 11 12 13 4 5 and the same query gives 12 + 13 + 4 = 29.