problems / 141

Retroactive Raises

hard segment-tree prefix-sums arrays 1000 ms · 256 MB

Retroactive Raises

The payroll service stores the monthly salary of n employees, whose ids are 1 to n. Ids were handed out by team, so an id range like [l, r] is how HR addresses a department. Employee i starts with a salary of s_i cents.

HR then sends q commands, which must be applied in the order they arrive:

  • raise l r v: every employee whose id lies in [l, r] (both ends included) has v cents added to their salary. v may be negative (a correction that claws back an earlier raise), and salaries may temporarily become zero or negative; store them as they are.
  • salary i: report the current salary of employee i in cents, taking into account every raise command received so far.

Answer every salary command.

Input

The first line contains two integers n and q (1 ≤ n ≤ 2·10^5, 1 ≤ q ≤ 2·10^5): the number of employees and the number of commands.

The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^9): the starting salaries.

Each of the next q lines contains one command, either raise l r v (1 ≤ l ≤ r ≤ n, −10^9 ≤ v ≤ 10^9) or salary i (1 ≤ i ≤ n). The command name is written exactly as shown, in lowercase.

Output

For each salary command, in the order they arrive, print one line with the requested salary in cents. If there are no salary commands, print nothing.

Notes

In the first example the salaries start as 100 200 300 400 500. The raise of 50 for ids 2..4 makes them 100 250 350 450 500, so employee 3 earns 350. The correction of −100 for ids 1..2 makes them 0 150 350 450 500.

In the second example the raises overlap, and salaries quickly stop fitting in a 32-bit integer.

Example 1

Input

5 6
100 200 300 400 500
raise 2 4 50
salary 3
raise 1 2 -100
salary 1
salary 2
salary 5

Output

350
0
150
500

Example 2

Input

3 7
1000000000 7 1000000000
raise 1 3 1000000000
raise 1 1 1000000000
raise 1 2 1000000000
salary 1
salary 2
raise 3 3 -1000000000
salary 3

Output

4000000000
2000000007
1000000000

Hints · reveal one at a time if you're stuck

Hint 1

Applying each raise to every employee in its range is too slow with 2·10^5 raises over a large payroll. Ask what a single raise actually changes, and whether you could store only the changes rather than the salaries.

Hint 2

Keep the starting salaries as they are and record each raise as a change at its left end and a cancelling change just past its right end. A salary is its start plus the running total of changes up to its id. Those are point updates and prefix sums, so a Fenwick tree or segment tree fits, and the values need 64-bit integers.

report an issue Prefer your own editor? sooks prep 141 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.