Non-Decreasing Dilemma
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!
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]$$$.
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.
For each query of type $$$2$$$, print a single integer, the answer to the query.
import sys
from cffi import FFI
input = sys.stdin.buffer.readline
ffi = FFI()
ffi.cdef('\n    typedef struct {\n        int first;\n        int last;\n        int suf;\n        int pre;\n        int length;\n        double ans;\n    } node_t;\n    ')
MX = 2 << (2 * 10 ** 5 - 1).bit_length()
data = ffi.new('node_t[]', MX + 3)
data[MX] = (0, 0, 0, 0, 0, 0)
id_node = data[MX]
res_left = MX + 1
res_right = MX + 2

def combine(a, b):
    if a.length == 0:
        return b
    if b.length == 0:
        return a
    first = a.first
    last = b.last
    suf = 0
    pre = 0
    length = a.length + b.length
    ans = a.ans + b.ans
    if a.last <= b.first:
        if a.length == a.pre:
            pre = b.pre + a.length
        else:
            pre = a.pre
        if b.length == b.suf:
            suf = a.suf + b.length
        else:
            suf = b.suf
        ans += a.suf * b.pre
    else:
        pre = a.pre
        suf = b.suf
    return (first, last, suf, pre, length, ans)

def mapValue(x):
    return (x, x, 1, 1, 1, 1)

class SegmentTree:

    def __init__(self, N, A):
        self._len = N
        self._size = _size = 1 << (self._len - 1).bit_length()
        for i in range(2 * _size):
            data[i] = id_node
        for (i, x) in enumerate(A):
            data[_size + i] = mapValue(x)
        for i in reversed(range(_size)):
            data[i] = combine(data[i + i], data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = id_node

    def __getitem__(self, idx):
        return data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        data[idx] = value
        idx >>= 1
        while idx:
            data[idx] = combine(data[2 * idx], data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """"""
        start += self._size
        stop += self._size
        data[res_left] = id_node
        data[res_right] = id_node
        while start < stop:
            if start & 1:
                data[res_left] = combine(data[res_left], data[start])
                start += 1
            if stop & 1:
                stop -= 1
                data[res_right] = combine(data[stop], data[res_right])
            start >>= 1
            stop >>= 1
        data[res_left] = combine(data[res_left], data[res_right])
        return data[res_left]

def main():
    (n, q) = map(int, input().split())
    segtree = SegmentTree(n, map(int, input().split()))
    ans = []
    for _ in range(q):
        (t, l, r) = map(int, input().split())
        l -= 1
        if t == 1:
            segtree[l] = mapValue(r)
        else:
            ans.append(int(segtree.query(l, r).ans))
    print('\n'.join(map(str, ans)))
if __name__ == '__main__':
    main()
import sys
from cffi import FFI
input = sys.stdin.buffer.readline
ffi = FFI()
ffi.cdef('\n    typedef struct {\n        int first;\n        int last;\n        int suf;\n        int pre;\n        int length;\n        double ans;\n    } node_t;\n    ')
MX = 2 << (2 * 10 ** 5 - 1).bit_length()
data = ffi.new('node_t[]', MX + 3)
data[MX] = (0, 0, 0, 0, 0, 0)
id_node = data[MX]
res_left = MX + 1
res_right = MX + 2

def combine(a, b):
    if a.length == 0:
        return b
    if b.length == 0:
        return a
    first = a.first
    last = b.last
    suf = 0
    pre = 0
    length = a.length + b.length
    ans = a.ans + b.ans
    if a.last <= b.first:
        if a.length == a.pre:
            pre = b.pre + a.length
        else:
            pre = a.pre
        if b.length == b.suf:
            suf = a.suf + b.length
        else:
            suf = b.suf
        ans += a.suf * b.pre
    else:
        pre = a.pre
        suf = b.suf
    return (first, last, suf, pre, length, ans)

def mapValue(x):
    return (x, x, 1, 1, 1, 1)

class SegmentTree:

    def __init__(self, N, A):
        self._len = N
        self._size = _size = 1 << (self._len - 1).bit_length()
        for i in range(2 * _size):
            data[i] = id_node
        for (i, x) in enumerate(A):
            data[_size + i] = mapValue(x)
        for i in reversed(range(_size)):
            data[i] = combine(data[i + i], data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = id_node

    def __getitem__(self, idx):
        return data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        data[idx] = value
        idx >>= 1
        while idx:
            data[idx] = combine(data[2 * idx], data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """"""
        start += self._size
        stop += self._size
        data[res_left] = id_node
        data[res_right] = id_node
        while start < stop:
            if start & 1:
                data[res_left] = combine(data[res_left], data[start])
                start += 1
            if stop & 1:
                stop -= 1
                data[res_right] = combine(data[stop], data[res_right])
            start >>= 1
            stop >>= 1
        data[res_left] = combine(data[res_left], data[res_right])
        return data[res_left]

def main():
    (n, q) = map(int, input().split())
    segtree = SegmentTree(n, map(int, input().split()))
    ans = []
    for _ in range(q):
        (t, l, r) = map(int, input().split())
        l -= 1
        if t == 1:
            segtree[l] = mapValue(r)
        else:
            ans.append(int(segtree.query(l, r).ans))
    print('\n'.join(map(str, ans)))
if __name__ == '__main__':
    main()
import sys
from cffi import FFI
input = sys.stdin.buffer.readline
ffi = FFI()
ffi.cdef('\n    typedef struct {\n        int first;\n        int last;\n        int suf;\n        int pre;\n        int length;\n        double ans;\n    } node_t;\n    ')
MX = 2 << (2 * 10 ** 5 - 1).bit_length()
data = ffi.new('node_t[]', MX + 3)
data[MX] = (0, 0, 0, 0, 0, 0)
id_node = data[MX]
res_left = MX + 1
res_right = MX + 2

def combine(a, b):
    if a.length == 0:
        return b
    if b.length == 0:
        return a
    first = a.first
    last = b.last
    suf = 0
    pre = 0
    length = a.length + b.length
    ans = a.ans + b.ans
    if a.last <= b.first:
        if a.length == a.pre:
            pre = b.pre + a.length
        else:
            pre = a.pre
        if b.length == b.suf:
            suf = a.suf + b.length
        else:
            suf = b.suf
        ans += a.suf * b.pre
    else:
        pre = a.pre
        suf = b.suf
    return (first, last, suf, pre, length, ans)

def mapValue(x):
    return (x, x, 1, 1, 1, 1)

class SegmentTree:

    def __init__(self, N, A):
        self._len = N
        self._size = _size = 1 << (self._len - 1).bit_length()
        for i in range(2 * _size):
            data[i] = id_node
        for (i, x) in enumerate(A):
            data[_size + i] = mapValue(x)
        for i in reversed(range(_size)):
            data[i] = combine(data[i + i], data[i + i + 1])

    def __delitem__(self, idx):
        self[idx] = id_node

    def __getitem__(self, idx):
        return data[idx + self._size]

    def __setitem__(self, idx, value):
        idx += self._size
        data[idx] = value
        idx >>= 1
        while idx:
            data[idx] = combine(data[2 * idx], data[2 * idx + 1])
            idx >>= 1

    def __len__(self):
        return self._len

    def query(self, start, stop):
        """"""
        start += self._size
        stop += self._size
        data[res_left] = id_node
        data[res_right] = id_node
        while start < stop:
            if start & 1:
                data[res_left] = combine(data[res_left], data[start])
                start += 1
            if stop & 1:
                stop -= 1
                data[res_right] = combine(data[stop], data[res_right])
            start >>= 1
            stop >>= 1
        data[res_left] = combine(data[res_left], data[res_right])
        return data[res_left]

def main():
    (n, q) = map(int, input().split())
    segtree = SegmentTree(n, map(int, input().split()))
    ans = []
    for _ in range(q):
        (t, l, r) = map(int, input().split())
        l -= 1
        if t == 1:
            segtree[l] = mapValue(r)
        else:
            ans.append(int(segtree.query(l, r).ans))
    print('\n'.join(map(str, ans)))
if __name__ == '__main__':
    main()