Azusa, the witch of the highlands, has discovered a garden full of weird trees! Therefore, together with her friend, Laika, she decided to spend some time there taking care of the garden.
The garden can be viewed as a sequence of trees, where the trees are indexed from to . Each tree has a certain non-negative integer height. Azusa will then spend her time according to a schedule containing entries, which can be of several types:
- A tree cutting phase, characterised by three integers , and . In this phase, Azusa will spend the next k days cutting trees. Each day she finds the tallest tree whose index is between and and decreases its height by . In case there are several trees of this maximal height, she chooses the leftmost one. If the tallest tree has height , then nothing happens on that day.
- A magic phase, characterised by two integers and . In this phase, Azusa changes the tree with index so that it has height .
- A tree inspection phase, characterised by two integers and . In this phase, Azusa will find the sum of the heights of the trees with indices between and .
(Note that "between" is meant inclusively; e.g. are "between" and .)
Azusa is curious what the results of the tree inspection phases will be, and wants to know them without having to go through the entire schedule. Can you help her?
Interaction protocol
The contestant must implement the following four functions:
void initialise(int N, int Q, int h[]);
void cut(int l, int r, int k);
void magic(int i, int x);
long long int inspect(int l, int r);
The function initialise
is given (the number of trees), (the number of entries in the schedule), and an array , where is the height of tree , for . This function is called by the committee's code exactly once, before any of the other three functions are called. The cut
, magic
and inspect
functions represent tree cutting, magic and tree inspecting phases respectively, and are characterised by their respective parameters. The contestant's implementation of the inspect function must return the sum of the heights of the trees with indices between and .
The contestant should not implement the main function. This will be implemented in the committee's grader.cpp
file; you will be given a sample grader.cpp in the attachments. Our main function will read , the sequence of initial heights, and the schedule entries. The three types of schedule entries (cut(l, r, k)
, magic(i, x)
and inspect(l, r)
) are encoded as and respectively. This is the input format that will be used in the examples below.
Note that the contestant is allowed to use global variables, additional functions, methods and classes.
Restrictions
- It is guaranteed that the
cut
,magic
andinspect
functions will be called exactly times in total.
# | Points | Restrictions |
---|---|---|
1 | 5 | |
2 | 8 | |
3 | 8 | , there are no magic operations. |
4 | 19 | There are no magic operations. |
5 | 10 | |
6 | 21 | |
7 | 29 | No further restrictions. |
Example
input
6 10
1 2 3 1 2 3
1 1 6 3
3 1 6
1 1 3 3
3 1 6
1 1 3 1000
3 1 6
2 1 1000
3 1 6
1 1 3 999
3 1 5
output
9
6
5
1005
4
Explanation
In the first phase, after each of the days of tree cutting, the heights of the trees are ; and . The sum of these values is , which is the answer to the inspection in the second phase.
In the third phase, after each of the days of tree cutting, the heights of the trees are and . The sum of these values is , which is the answer to the inspection in the fourth phase.
In the fifth phase, after each of the days of tree cutting, the heights of the trees are . This is because a tree with height cannot be cut. The sum of these values is , which is the answer to the inspection in the sixth phase.
In the seventh phase, the first tree is grown to height , giving us tree heights . The sum of these values is , which is the answer to the inspection in the eighth phase.
In the ninth phase, each of the days of tree cutting reduces the height of the first tree by . This gives us tree heights at the end of the phase. The sum of the first five of these values is , which is the answer to the inspection in the tenth and final phase.