Fences and Peak Blocks
You are given an array of integers and toggleable fences placed between consecutive positions. The fence with index lies between positions and for . All fences start inactive (down).
For any interval , define its blocks as the maximal contiguous subsegments obtained by cutting at every fence whose index satisfies . The score of is the sum, over all its blocks, of the maximum value within each block. Formally, if the blocks are with , then the score is
Process operations of the following types:
- Type 1: set .
- Type 2: toggle the fence between positions and (from down to up, or from up to down).
- Type 3: given and , report the score of .
Indices are -based. When , the score equals regardless of fences.
Input
The first line contains two integers and (, ), the number of positions and the number of operations. The second line contains integers (). Each of the next lines describes one operation in one of the following formats (all indices are -based):
Output
For each operation of type 3, print a single line with one integer: the score of the queried interval . No output is produced for operations of type 1 or type 2.
Samples
Sample 1
Input
5 9 5 1 4 3 2 3 1 5 2 2 3 1 5 2 4 3 2 5 1 4 10 3 2 5 2 2 3 1 5
Output
5 9 7 13 12
Initially, all fences are down. Query over uses one block with maximum . After toggling the fence at , splits into and with maxima and , summing to . Toggling the fence at and querying yields blocks , , with maxima , , and . After setting , the same query sums to . Finally, toggling again leaves only the fence at , so becomes two blocks with maxima and .