Subarray Addition

4

2 votes
Segment Trees, Segment tree, Data Structures, Advanced Data Structures, Fenwick Tree
Problem

You are given an array A having N integers. You take an array B of length N such that Bi=0 for all 1iN. You perform Q operations of the following two types:

  • 1LRX : add X to all elements of the subarray A[LR]
  • 2LR : add Ai to Bi for all LiR.

Print the elements of the array B after Q operations.

  Input format

  • The first line of input contains two integers N,Q denoting the length of the array A and the number of operations respectively.
  • The second line contains N integers A1,A2,,AN, denoting elements of the array A.
  • Each of the next Q lines contains a description of one of the two types of operations. 

Output format

Print N integers B1,B2,,BN, denoting elements of the array B after Q operations.

Constraints

1N,Q21051Ai,X1051LiRiN

Time Limit: 1.5
Memory Limit: 256
Source Limit:
Explanation

After the first operation, A becomes [3,2+1,6+1,1,3]=[3,3,7,1,3].

After the second operation, B becomes[0,0,0+7,0+1,0+3]=[0,0,7,1,3].

After the third operation, A becomes [3+3,3+3,7+3,1+3,3]=[6,6,10,4,3].

After the fourth operation, A becomes [6+,6+6,10,4,3]=[12,12,10,4,3].

After the fifth operation, B becomes[0+12,0+12,7+10,1+4,3]=[12,12,17,5,3].

Editor Image

?