River Gauges

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

River Gauges

The flood office keeps n rain gauges along a river, numbered 1 to n from the source to the mouth. Gauge i currently shows a reading of a_i millimetres.

During the storm season the office receives q messages, which must be handled in the order they arrive. Each message is one of two kinds:

  • reading i x: the warden at gauge i reports that the gauge now shows x. This replaces the gauge's previous reading; it is not added to it. Every later message sees the new value.
  • total l r: a dispatcher asks for the total rainfall shown right now by all gauges on the stretch of river between gauge l and gauge r, both included. The two ends may be given in either order (the stretch between gauges 5 and 2 consists of gauges 2, 3, 4, 5), and they may be the same gauge.

Answer every total message.

Input

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

The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9): the initial readings.

Each of the next q lines contains one message, either reading i x (1 ≤ i ≤ n, 0 ≤ x ≤ 10^9) or total l r (1 ≤ l, r ≤ n). The kind is written exactly as shown, in lowercase.

Output

For each total message, in the order they arrive, print one line with the requested total. If there are no total messages, print nothing.

Notes

In the first example the stretch between gauges 2 and 4 initially shows 1 + 4 + 1 = 6. Then gauge 3 is reset from 4 to 0, so the same stretch now shows 2, and the whole river shows 3 + 1 + 0 + 1 + 5 = 10. The last message asks about gauge 5 alone.

In the second example the ends of the stretch are given in reverse order, and readings can be large enough that totals do not fit in a 32-bit integer.

Example 1

Input

5 5
3 1 4 1 5
total 2 4
reading 3 0
total 2 4
total 1 5
total 5 5

Output

6
2
10
5

Example 2

Input

4 4
1000000000 1000000000 1000000000 7
total 4 1
reading 4 1000000000
reading 2 5
total 3 2

Output

3000000007
1000000005

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

Hint 1

Each message either changes one value or asks for the sum over a range, and both happen many times. Why is walking the range for every total too slow, and why does rebuilding prefix sums after every change also fail?

Hint 2

Use a Fenwick tree over the gauges. A reset changes the total by the difference between the new reading and the old one, so keep the current readings in a plain array too. Swap reversed range ends first, and use 64-bit totals.

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

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