eolymp
bolt
Try our new interface for solving problems
Problems

Counting Haybales

Counting Haybales

Farmer John is trying to hire contractors to help rearrange his farm, but so far all of them have quit when they saw the complicated sequence of instructions FJ wanted them to follow. Left to complete the project by himself, he realizes that indeed, he has made the project perhaps more complicated than necessary. Please help him follow his instructions to complete the farm upgrade.

FJ's farm consists of n fields in a row, conveniently numbered 1..n. In each field there can be any number of haybales. Farmer John's instructions contain three types of entries:

1) Given a contiguous interval of fields, add a new haybale to each field.

2) Given a contiguous interval of fields, determine the minimum number of haybales in a field within that interval.

3) Given a contiguous interval of fields, count the total number of haybales inside that interval.

Input

The first line contains two positive integers n (1n2 * 105) and q (1q105).

The next line contains n nonnegative integers, each at most 105, indicating how many haybales are initially in each field.

Each of the next q lines contains a single uppercase letter, either M, P or S, followed by either two positive integers a and b (1abn), or three positive integers a, b and c (1abn, 1c105). There will be three positive integers if and only if the uppercase letter is P.

If the letter is M, print the minimum number of haybales in the interval of fields from a..b.

If the letter is P, put c new haybales in each field in the interval of fields from a..b.

If the letter is S, print the total number of haybales found within interval of fields from a..b.

Output

A line in the output should appear in response to every M or S entry in FJ's instructions.

Time limit 1 second
Memory limit 128 MiB
Input example #1
4 5
3 1 2 4
M 3 4
S 1 3
P 2 3 1
M 3 4
S 1 3
Output example #1
2
6
3
8
Source 2015 USACO December, Platinum