### PROBLEM LINK:

**Author:** Devendra Agarwal

**Tester:** Mugurel Ionut Andreica

**Editorialist:** Lalit Kundu

### DIFFICULTY:

Medium

### PREREQUISITES:

segment tree, lazy propagation, basic maths

### PROBLEM:

Given array A of N size and M operations of type:

- Add v to all elements in a range.
- Multiply v to all elements in a range.
- Reset all items to v in a range.
- Report sum in a range.

### QUICK EXPLANATION:

================

Build a segment tree, where at each node we store extrm{sum} and variables extrm{mul} and extrm{add}, which denotes that the lazy update A_i \leftarrow extrm{mul}*A_i + extrm{add} needs to be applied. If required, we update the current node’s sum and variables and propagate the laziness down the tree. Also, an multiplication update v at a node can be summarised as extrm{mul} \leftarrow extrm{mul}*v and extrm{add} \leftarrow extrm{add}*v and also addition update v at a node can be written as extrm{add} \leftarrow extrm{add} + v. Set operation with value v can be written as extrm{mul} \leftarrow 0 and extrm{add} \leftarrow v.

### EXPLANATION:

================

##BASIC SEGMENT TREE STUFF

I assume you know how segment trees and lazy propagation work and the basic concepts behind them like time complexity, nodes, intervals. First, I’ll introduce some terminology I’m going to use.

- Node: It’s one of the nodes in the segment tree, it represents a contiguous interval of the array A.
- Interval of a node is the actual range covered by the node.
- Answer of a node is defined the actual value needed for interval queries. Here, for example, we need sum of values in the range.

Let’s think step by step here.

- For range query problems, try to see if segment tree can be used.
- When does segment tree work? When you can merge two contiguous intervals(
*i.e*nodes) to get answer of merged interval(node) in sublinear time. If complexity of merging is O(K), then complexity for each operation can be worst case O( extrm{log N}*K). For example, when we talk about range minimum function, we can get minimum value of merged interval by taking minimum of answers of two individual intervals. - Also, for range updates you need to use lazy propagation. What does lazy propagation require to work? It requires that for an interval if we have multiple update operations, we can calculate the answer for that interval without actually updating every element in that interval. For example, if we are trying to find range minimum and range updation query is increase all elements by value v, then our new minimum is sum of existing minimum with v. Here also, we should be able to do this operation in sublinear time because it contributes to the factor of updations and queries.

Let’s see how we use above two points to find here a solution using segment trees. Let’s see the query part first: query is range sum, so merging two intervals is easy, just take the individual sums.

##THE LAZY PROPAGATION SOLUTION

Now, let’s see how we can handle all updations in such a way that we can find answer for an interval without actually updating the whole interval. We are going to store some data about the updations being done at that interval node and process it to find the answer. What could be this data? How do we find out? We need to observe what kind of operations we are doing. After some certain updations, our A_i could be transformed to something like ((A_i*v_1 + v_2)*v_3 + v_4 + v_5)*v_6 + v_7, where v_1 to v_7 are values of range multiplication or range addition. Now, we can store all these values v_1 to v_7 at our node, but we might have to do O( extrm{number of queries}) operations at each node, which is not really sublinear. We need to find a compact notation at each node interval.

Now, thing worth noting here is that A_i has been transformed to a linear function of A_i *i.e.* something of form ( extrm{mul}*A_i + extrm{add}). Now, let’s say I make one more multiplication range update v, what’s the new value of A_i. It’s ( extrm{mul}*v*A_i + extrm{add}*v). So, we update extrm{mul} *= v and extrm{add} *= v at our node. Similarly, if we make a sum update with value v, the new value of A_i is ( extrm{mul}*A_i + extrm{add} + v), so we update extrm{add} += v. For setting all elements to v, we can just make one multiplication with 0 and then addition with value v.

So, if this interval is in range L to R, for interval sum, we need \sum_{i=L}^{R}( extrm{mul}*A_i + extrm{add}) which we can write as (R-L+1)* extrm{add} + extrm{mul}*\sum_{i=L}^{R}A_i. So, we have to store sum of original A_i and R and L(basically size) and two variables extrm{mul} and extrm{add} at each node. Now, to make things easier we can just directly store the extrm{sum} of a node(*i.e.* sum of all elements in that interval) instead of storing sum of original A_i. Then, for each range multiplication update or range addition update, we also update this extrm{sum} along with the variables extrm{mul} and extrm{add}.

Also, as we do we in lazy propagation, we propagate the laziness to the children of a node, if we need to query a children of a lazy node. In this problem, we can individually propagate variables extrm{mul} and extrm{add}. So, if at a node extrm{mul} e 1, then we can say that this node is multiplication lazy and if required, we’ll propagate this variable down to the children of this node. Similarly, if at a node extrm{add} e 0, we can say that this node is addition lazy and propagate this laziness down to its children.

### COMPLEXITY:

For building the segment tree we need O(N extrm{log} N) and each query is O( extrm{log} N), so total complexity is O(N extrm{log} N + Q extrm{log} N).

### PROBLEMS TO SOLVE:

QSET

SPOJ HORRIBLE

FNCS

MSTICK

ANUSAR

FRBSUM

Sherlock and Unique Substrings