Hacker with Team

4.5

8 votes
Approved, Data Structures, Easy, Fenwick Tree, Fenwick tree
Problem

Alex has learnt many new concepts in hacking, and created his own team. He has N people (excluding Alex) in his team, where ith person has individual skill value Si where 1iN. They are standing in a particular order, where 1st person being the leftmost person and Nth person being the rightmost person.

Alex has found a special way to evaluate the contribution done by each person in the team. Contribution done by ith person (Ci ) in the team can be calculated as sum of his skill value and the skill values of his K adjacent team members on the left side, where the value of K will be decided by Alex.

In case, there are not enough members on the left side, contribution will be equal to the sum of his skill value and the skill values of all the team members on the left side.

Alternatively,
      Ci = ij=maximum(iK,1)Sj

Alex can perform 2 types of operations now:

1) He can write a program to calculate the total contribution done by all the team members in the range of l to r (both inclusive), where contribution of each person will be calculated using value of K decided by him.

2) He can ask any person to change his skill value to X.

Being a great hacker, he is busy with other tasks, and needs your help in performing above operations.

Input Format:
First line contains 2 space-separated integers N and Q, where N denotes the number of people in Alex's team and Q denotes the number of operations Alex wants to get completed.

Second line contains N space-separated integers, where ith integer denotes the skill value of ith person in the team.

In next Q lines, each line contains first integer O, which represents the type of operation.

  1) For O=1, line will contain 3 more space-separated integers l,r and K.
Here Alex wants to know the total contribution done by all team members in the range l to r (both inclusive), where contribution of each person will be calculated using value of K decided by him.

  2) For O=2, line will contain 2 more space-separated integers i and X where Alex asks ith person to change his skill value to X.

Output Format:

For each operation of the 1st type, print the required answer in the new line.

Constraints:

1N,Q105
1Si,X108
1lrN
1KN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Here N=6, and Q=3.

For first operation O=1 and l=4 , r=5 and K=2. It means, we need to evaluate the contribution done by all the team members in the range [4,5] with K=2. Therefore:
C4=S4+S3+S2=4+3+2=9
C5=S5+S4+S3=5+4+3=12

So total contribution is 21.

For second operation O=2, i.e 4th person will change his skill to 3, i.e S4=3.

For third operation, O=1 and l=4 , r=5 and K=2. Now the required contribution will be 19.

Editor Image

?