description: Alice has recently received an array a_1, a_2, ..., 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}, ..., a_r]. More formally, count the number of pairs of integers (p,q) such that l ≤ p ≤ q ≤ r and a_p ≤ a_{p+1} ≤ ... ≤ a_{q-1} ≤ a_q. 
  
  
  
  Help Alice answer Bob's queries!
  
  Input
  
  The first line contains two integers n and q (1 ≤ n, q ≤ 2 ⋅ 10^5) — the size of the array, and the number of queries, respectively.
  
  The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 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 ≤ x_i ≤ n; 1 ≤ y_i ≤ 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 ≤ l_i ≤ r_i ≤ 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
  
  For each query of type 2, print a single integer, the answer to the query.
  
  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
  
  
  Output
  
  
  6
  4
  10
  7
  
  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].
solution: import java.util.*;
  import java.io.*;
  
  public class E_1567 {
  	public static void main(String[] args) throws Exception {
  		Scanner sc = new Scanner(System.in);
  		PrintWriter pw = new PrintWriter(System.out);
  		
  		int n = sc.nextInt(), q = sc.nextInt();
  		int N = 1;
  		while(N < n)
  			N <<= 1;
  		
  		int[] temp = new int[N + 1];
  		for(int i = 1; i <= n; i++)
  			temp[i] = sc.nextInt();
  		
  		//System.out.println(Arrays.toString(temp));
  		SegmentTree tree = new SegmentTree(temp);
  		
  		while(q-->0) {
  			//pw.println(Arrays.toString(tree.sum));
  			//pw.println(Arrays.toString(tree.pre));
  			//pw.println(Arrays.toString(tree.post));
  			int t = sc.nextInt();
  			if(t == 1) {
  				tree.update_point(sc.nextInt(), sc.nextInt());
  			} else {
  				pw.println(tree.query(sc.nextInt(), sc.nextInt()));
  			}
  		}
  		
  		pw.flush();
  	}
  	
  	public static class SegmentTree {
  		
  		public static class Tuple {
  			long sum;
  			int pre, post;
  			
  			public Tuple(long sum, int pre, int post) {
  				this.sum = sum;
  				this.pre = pre;
  				this.post = post;
  			}
  		}
  		
  		int N;
  		long[] sum;
  		int[] in, pre, post, middle;
  		
  		public SegmentTree(int[] array) {
  			in = array;
  			N = array.length - 1;
  			sum = new long[N<<1];
  			pre = new int[N<<1];
  			post = new int[N<<1];
  			middle = new int[N<<1];
  			build(1, 1, N);
  		}
  		
  		public void build(int node, int b, int e) {
  			if(b == e) {
  				sum[node] = 1;
  				pre[node] = 1;
  				post[node] = 1;
  				return;
  			}
  			
  			int left = node<<1, right = node<<1|1;
  			int mid = (b + e)>>1;
  			
  			build(left, b, mid);
  			build(right, mid + 1, e);
  			
  			middle[node] = mid;
  			
  			if(in[mid] <= in[mid + 1]) {
  				int common = post[left] + pre[right];
  				long newsum = sum[left] + sum[right];
  				newsum -= 1l * post[left] * (post[left] + 1) / 2;
  				newsum -= 1l * pre[right] * (pre[right] + 1) / 2;
  				newsum += 1l * common * (common + 1) / 2;
  				sum[node] = newsum;
  				if(pre[right] == post[right] && pre[right] == (e - mid))
  					post[node] = common;
  				else
  					post[node] = post[right];
  				if(pre[left] == post[left] && pre[left] == (e - mid))
  					pre[node] = common;
  				else
  					pre[node] = pre[left];
  			} else {
  				sum[node] = sum[left] + sum[right];
  				pre[node] = pre[left];
  				post[node] = post[right];
  			}
  		}
  		
  		public long query(int l, int r) {
  			return query(1, 1, N, l, r).sum;
  		}
  		
  		public Tuple query(int node, int b, int e, int l, int r) {
  			if(r < b || l > e)
  				return null;
  			if(b >= l && e <= r)
  				return new Tuple(sum[node], pre[node], post[node]);
  			
  			int left = node<<1, right = node<<1|1;
  			int mid = (b + e)>>1;
  			
  			Tuple t1 = query(left, b, mid, l, r);
  			Tuple t2 = query(right, mid + 1, e, l, r);
  			
  			if(t1 == null)
  				return t2;
  			else if(t2 == null)
  				return t1;
  			
  			Tuple ans = new Tuple(0, 0, 0);
  			if(in[mid] <= in[mid + 1]) {
  				int common = t1.post + t2.pre;
  				long newsum = t1.sum + t2.sum;
  				newsum -= 1l * t1.post * (t1.post + 1) / 2;
  				newsum -= 1l * t2.pre * (t2.pre + 1) / 2;
  				newsum += 1l * common * (common + 1) / 2;
  				ans.sum = newsum;
  				if(t2.pre == t2.post && t2.pre == (e - mid))
  					ans.post = common;
  				else
  					ans.post = t2.post;
  				if(t1.pre == t1.post && t1.pre == (e - mid))
  					ans.pre = common;
  				else
  					ans.pre = t1.pre;
  			} else {
  				ans.sum = t1.sum + t2.sum;
  				ans.pre = t1.pre;
  				ans.post = t2.post;
  			}
  			
  			return ans;
  		}
  		
  		public void update_point(int node, int v) {
  			in[node] = v;
  			node += N - 1;
  			int length = 1;
  			while(node > 1) {
  				node /= 2;
  				int left = node<<1, right = node<<1|1;
  				int mid = middle[node];
  				if(in[mid] <= in[mid + 1]) {
  					int common = post[left] + pre[right];
  					long newsum = sum[left] + sum[right];
  					newsum -= 1l * post[left] * (post[left] + 1) / 2;
  					newsum -= 1l * pre[right] * (pre[right] + 1) / 2;
  					newsum += 1l * common * (common + 1) / 2;
  					sum[node] = newsum;
  					if(pre[right] == post[right] && pre[right] == length)
  						post[node] = common;
  					else
  						post[node] = post[right];
  					if(pre[left] == post[left] && pre[left] == length)
  						pre[node] = common;
  					else
  						pre[node] = pre[left];
  				} else {
  					sum[node] = sum[left] + sum[right];
  					pre[node] = pre[left];
  					post[node] = post[right];
  				}
  				length *= 2;
  			}
  		}
  	}
  
  	public static class Scanner {
  		StringTokenizer st;
  		BufferedReader br;
  
  		public Scanner(InputStream system) {
  			br = new BufferedReader(new InputStreamReader(system));
  		}
  
  		public Scanner(String file) throws Exception {
  			br = new BufferedReader(new FileReader(file));
  		}
  
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  
  		public double nextDouble() throws IOException {
  			return Double.parseDouble(next());
  		}
  
  		public char nextChar() throws IOException {
  			return next().charAt(0);
  		}
  
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  
  		public int[] nextIntArray(int n) throws IOException {
  			int[] array = new int[n];
  			for (int i = 0; i < n; i++)
  				array[i] = nextInt();
  			return array;
  		}
  
  		public Integer[] nextIntegerArray(int n) throws IOException {
  			Integer[] array = new Integer[n];
  			for (int i = 0; i < n; i++)
  				array[i] = new Integer(nextInt());
  			return array;
  		}
  
  		public long[] nextLongArray(int n) throws IOException {
  			long[] array = new long[n];
  			for (int i = 0; i < n; i++)
  				array[i] = nextLong();
  			return array;
  		}
  
  		public double[] nextDoubleArray(int n) throws IOException {
  			double[] array = new double[n];
  			for (int i = 0; i < n; i++)
  				array[i] = nextDouble();
  			return array;
  		}
  
  		public static int[] shuffle(int[] a) {
  			int n = a.length;
  			Random rand = new Random();
  			for (int i = 0; i < n; i++) {
  				int tmpIdx = rand.nextInt(n);
  				int tmp = a[i];
  				a[i] = a[tmpIdx];
  				a[tmpIdx] = tmp;
  			}
  			return a;
  		}
  
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  
  		public void waitForInput() throws InterruptedException {
  			Thread.sleep(3000);
  		}
  	}
  }
  