Non-Decreasing Dilemma
Contest: Codeforces Round 742 (Div. 2)
Difficulty rating: 2200
Tags: data structures, divide and conquer, math

Alice has recently received an array $$$a_1, a_2, \dots, a_n$$$ for her birthday! She is very proud of her array, and when she showed her friend Bob the array, he was very happy with her present too!

However, soon Bob became curious, and as any sane friend would do, asked Alice to perform $$$q$$$ operations of two types on her array:

- $$$1$$$ $$$x$$$ $$$y$$$: update the element $$$a_x$$$ to $$$y$$$ (set $$$a_x = y$$$).
- $$$2$$$ $$$l$$$ $$$r$$$: calculate how many non-decreasing subarrays exist within the subarray $$$[a_l, a_{l+1}, \dots, a_r]$$$. More formally, count the number of pairs of integers $$$(p,q)$$$ such that $$$l \le p \le q \le r$$$ and $$$a_p \le a_{p+1} \le \dots \le a_{q-1} \le a_q$$$.

Help Alice answer Bob's queries!

Input format:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the size of the array, and the number of queries, respectively.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of Alice's array.

The next $$$q$$$ lines consist of three integers each. The first integer of the $$$i$$$-th line is $$$t_i$$$, the operation being performed on the $$$i$$$-th step ($$$t_i = 1$$$ or $$$t_i = 2$$$).

If $$$t_i = 1$$$, the next two integers are $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i \le n$$$; $$$1 \le y_i \le 10^9$$$), updating the element at position $$$x_i$$$ to $$$y_i$$$ (setting $$$a_{x_i} = y_i$$$).

If $$$t_i = 2$$$, the next two integers are $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$), the two indices Bob asks Alice about for the $$$i$$$-th query.

It's guaranteed that there is at least one operation of the second type.

Output format:
For each query of type $$$2$$$, print a single integer, the answer to the query.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
For the first query, $$$l = 2$$$ and $$$r = 5$$$, and the non-decreasing subarrays $$$[p,q]$$$ are $$$[2,2]$$$, $$$[3,3]$$$, $$$[4,4]$$$, $$$[5,5]$$$, $$$[2,3]$$$ and $$$[4,5]$$$.

Example input:
5 6
3 1 4 1 5
2 2 5
2 1 3
1 4 4
2 2 5
1 2 6
2 2 5

Example output:
6
4
10
7

Editorial:
Note that if there exists a non-decreasing array of length $$$x$$$, then it contains $$$\frac{x(x+1)}{2}$$$ non-decreasing subarrays. Therefore, we can break our solution down to counting the lengths of the non-decreasing "chains" within the queried subarray.
We can solve this problem using a data structure called a segment tree. On each node of this segment tree, we shall maintain four pieces of information:
$$$\cdot$$$ the length of the longest non-decreasing prefix.
$$$\cdot$$$ the length of the longest non-decreasing suffix.
$$$\cdot$$$ a boolean flag denoting if the entire segment is non-decreasing.
$$$\cdot$$$ the total number of nondecreasing subarrays not part of the longest prefix or suffix.
I won't explain the merging process here (you can examine the implementation below), but it is not too complex.
Using this, we can traverse the segment tree, from the left of our queried range to the right, while maintaining the number of non-decreasing subarrays in the prefix of the range and the length of the outgoing non-decreasing suffix to get our answer.
Time complexity: $$$\mathcal{O}(q\log n)$$$.