Test instructions: n points, coordinates are known, where the horizontal axis is 1~n. The maximum value of the slope within all sub-intervals of the interval [L, R].
The first thing to know is that the maximum slope in the [L, R] interval is necessarily the two adjacent points.
Then the problem becomes the same as the maximum of all the sub-intervals in the interval [L, R].
This problem can be done using a monotone stack.
Each time you find the first point to the left of the current point that is greater than the current value, then update the answer. Pose a lot.
1 ImportJava.io.BufferedInputStream;2 ImportJava.io.BufferedOutputStream;3 ImportJava.io.PrintWriter;4 ImportJava.util.Scanner;5 6 7 8 Public classMain {9 StaticScanner cin =NewScanner (NewBufferedinputstream (system.in));Ten StaticPrintWriter cout =NewPrintWriter (NewBufferedoutputstream (System.out)); One Static Final intMAXN = 100005; A Public Static voidMain (string[] args) { - int[]height =New int[MAXN]; - while(Cin.hasnext ()) { the intn =cin.nextint (); - intQ =cin.nextint (); -Height[0] = 0; - for(inti = 1; I <= N; i++){ +Height[i] =cin.nextint (); -Height[i-1] = Math.Abs (height[i]-height[i-1]); + } A int[]stack =New int[MAXN]; at inttop =-1; - for(inti = 0; i < Q; i++){ - intL =cin.nextint (); - intR =cin.nextint (); - Longres = 0, cur = 0; -top =-1; in for(intj = l; J < R; J + +){ - while(Top >= 0 && Height[stack[top]] <=Height[j]) { toCur-= 1L * Height[stack[top]] * (stack[top)-(top==0 l-1: stack[top-1])); +top--; - } the if(Top >= 0){ *Cur + = 1l* (J-stack[top]) *Height[j]; $}Else{Panax NotoginsengCur + = 1L * (j-l + 1) *Height[j]; - } theStack[++top] =J; +Res + =cur; A } the + cout.println (RES); - } $ Cout.flush (); $ } - } - the}
Codeforces Round #333 (Div. 1)--b. Lipshitz Sequence monotonic Stack