# e-olymp 2307. The sum

The array of $n$ elements is given. Find the sum of numbers on a segment.

### Input

The first line contains two integers $n$ and $k$ $(1 \le n \le 10^5, 0 \le k \le 10^5)$ — the number of elements in array and the number of queries. The next $k$ lines contain the queries of two forms:

• $A$ $l$ $r$ $x$  — assign the value of $x$ to each element form position $l$ to $r$ $(1 \le l \le r \le n, 0 \le x \le 10^9)$
• $Q$ $l$ $r$ — find the sum of array elements on positions from $l$ to $r$ $(1 \le l \le r \le n)$

Initially the array is filled with zeroes.

### Output

For each query of the form «$Q$ $l$ $r$» print the sum of numbers on a segment.

### Tests

 № Input Output 1 5 9 A 2 3 2 A 3 5 1 A 4 5 2 Q 1 3 Q 2 2 Q 3 4 Q 4 5 Q 5 5 Q 1 5 3 2 3 4 2 7 2 10 6 A 1 10 1 Q 1 10 A 1 5 2 Q 1 10 A 4 7 3 Q 1 10 10 15 21 3 100000 2 A 1 100000 1000000000 Q 1 100000 100000000000000

### Algorithm

After reading the statement of the problem it is understandable that we should implement segment tree with support for multiple modification request — assignment. To make modifications to the whole segment and quickly answer for the amounts queries, we organize data structure in which each node store «colored» — if it is painted in any color or not and a sum on corresponding to this node segment.

Under the node coloring we will understand that all segment with all its subsegments should be colored in the appropriate color (assigned a specific number). Also define the mark that the node isn’t colored (in the code defined as WHITE), for example, any negative number, because queries contain only positive. This implementation allows us to do «delayed» update of the tree. Specifically, for each request of modification, instead of changing the values at all vertices where it is required, change only some of them, leaving «colored» markers for the other segments, which means that the whole segment with its subsegments should be painted in this color.
But if we do it so, after each modification request segment tree will stop being up to date. For resolving this problem, organise a function, that will «push» information from parents to children: void push(Node *tree, int currentNode, int left, int right) . This function is called during requests processing to update the appropriate values:

• assign parent’s color to the children
• recalculate the amount of the segment for children (as the length of the segment multiplied by the current color (number))
• set parent node as uncolored

Thus, to solve this task, we need two usual for segment tree functions:

• void update(Node *tree, int currentNode, int left, int right, int leftBorder, int rightBorder, int color)
• long long sum(Node *tree, int currentNode, int left, int right, int leftBorder, int rightBorder)

With the difference that, when we are going down by the tree, pushes information only as far as necessary (it should be noted, that in this way the asymptotic doesn’t impair and remains standard $O(\log n)$).

Rest details of the implementation can be found in the code of the program or in the sources of information listed at the end of the article.