WOSS Dual Olympiad 2023 J3/S1: Moving Sand

View as PDF

Submit solution

Points: 7
Time limit: 10.0s
Memory limit: 1G

Authors:
Problem type

There are N piles of sand in a line. The ith pile from the left initially has si units of sand. Answer Q queries, the ith query contains 3 integers ai, bi, and ci:

If ai is 1: move ci units of sand from the bith pile to the (bi1)th pile. (2biN).

If ai is 2: move ci units of sand from the bith pile to the (bi+1)th pile. (1biN1).

If ai is 3: find the total units of sand in all of the piles between pile bi and ci, inclusive. (1biciN).

The queries will never make you remove more sand from a pile than it contains, or move a negative amount of sand.

Constraints

1N,Q106

1si103

Subtask 1 [40%]

All queries will have ai = 3.

Subtask 2 [60%]

No additional constraints.

Input Specification

The first line contains 2 space-separated integers, N and Q.

The second line contains N space-separated integers, the ith integer representing the units of sand in the ith pile.

The next Q lines each contain 3 space-separated integers: ai, bi, and ci.

Output Specification

For each query with ai=3, output a line containing a single integer: the total units of sand in all of the piles between pile bi and ci, inclusive.

Sample Input

Copy
5 6
4 2 10 3 4
3 2 4
1 2 2
2 3 8
3 1 3
2 1 4
3 2 5

Sample Output

Copy
15
8
21

Explanation for Sample

The piles are initially [4,2,10,3,4].

After the 2nd query they are [6,0,10,3,4].

After the 3rd query they are [6,0,2,11,4].

After the 5th query they are [2,4,2,11,4].


Comments

There are no comments at the moment.