There are many ways to solve this problem. The following three methods are introduced. The main idea is to reverse the sequence (in fact, there is nothing to do with no reversal ), then, find the position J on the left of element I, which is greater than this element. Then, I-j + 1 is the corresponding solution of element I.
Method 1: Use the line segment tree to dynamically update the previous part of the query and the complexity O (nlogn ).
I will not elaborate on the methods used by the line segment tree to deal with such problems. For more information, see this article.
My code:
# Include <cstdio> <br/> # include <cstring> <br/> # include <cstdlib> <br/> # include <algorithm> <br/> using namespace STD; <br/> const int max = 80001; <br/> const int oo = 0x3f3f3f; <br/> struct node {<br/> int L, R, Val; <br/>} seg [max <2]; <br/> int H [Max]; <br/> void Init (int K, int L, int R) {<br/> seg [K]. L = L; <br/> seg [K]. R = r; <br/> seg [K]. val =-1; <br/> If (L = r) {<br/> return; <br/>}< br/> int mid = (L + r)> 1; <br/> ini T (k <1, L, mid); <br/> Init (k <1 | 1, Mid + 1, R ); <br/>}< br/> void add (int K, int idx) {<br/> If (SEG [K]. L = seg [K]. r) {<br/> seg [K]. val = idx; <br/> return; <br/>}< br/> int mid = (SEG [K]. L + seg [K]. r)> 1; <br/> If (idx <= mid) add (k <1, idx ); <br/> else add (k <1 | 1, idx); <br/> If (SEG [k <1 | 1]. val! =-1 & H [seg [k <1 | 1]. val]> = H [seg [k <1]. val]) seg [K]. val = seg [k <1 | 1]. val; <br/> else seg [K]. val = seg [k <1]. val; <br/>}< br/> int read (int K, int idx, int v) {<br/> If (SEG [K]. l> idx) Return-1; <br/> If (H [seg [K]. val] <v) Return-1; <br/> If (SEG [K]. L = seg [K]. r) return seg [K]. val; <br/> int mid = (SEG [K]. L + seg [K]. r)> 1; <br/> If (idx> mid & H [seg [k <1 | 1]. val]> = V) {<br/> return read (k <1 | 1, idx, V ); <br/>} else {<br/> return read (k <1, idx, V ); <br/>}< br/> int main () {<br/> int N; <br/> long ret = 0; <br/> scanf ("% d", & N); <br/> H [0] = Oo; <br/> for (INT I = 1; I <= N; I ++) scanf ("% d", & H [I]); <br/> reverse (H + 1, H + n + 1 ); <br/> Init (1, 0, n); <br/> Add (1, 0); <br/> for (INT I = 1; I <= N; I ++) {<br/> RET + = I-read (1, I-1, H [I])-1; <br/> Add (1, I ); <br/>}< br/> printf ("% LLD/N", RET); <br/> return 0; <br/>}< br/>
Method 2: using a monotonous stack, the stack maintains a descending interval and records the subscript of the element sequence larger than the former. If a new element is added to the queue, if the top element of the stack is greater than this element, the subscript of this element is added, and the subscript of the top element of the stack is subtracted from 1. Otherwise, the top element of the stack goes out of the stack, and so on. The algorithm complexity is O (n.
Algorithm Implementation is actually very simple. You only need to maintain a stack and use top to represent the top and bottom labels of the stack.
My code:
# Include <cstdio> <br/> # include <cstring> <br/> # include <cstdlib> <br/> # include <algorithm> <br/> using namespace STD; <br/> const int max = 80005; <br/> const int oo = 0x3f3f3f; <br/> int st [Max], H [Max]; <br/> int main () {<br/> int N; <br/> scanf ("% d", & N ); <br/> H [0] = Oo; <br/> for (INT I = 1; I <= N; I ++) scanf ("% d ", & H [I]); <br/> reverse (H + 1, H + 1 + n); <br/> int Top = 0; <br/> long ret = 0; <br/> st [top ++] = 0; <br/> for (INT I = 1; I <= N; I ++) {<br/> while (H [st [Top-1] <H [I]) Top --; <br/> RET + = I-ST [Top-1]-1; <br/> st [top ++] = I; <br/>}< br/> printf ("% LLD/N", RET); <br/> return 0; <br/>}< br/>
Method 3: Use and query the set directly. Record R [I] indicates the rightmost subscript that can be reached by the I-th element. The following compression path is used each time:
R [I] = I;
While (H [I]> H [R [I] + 1]) R [I] = R [R [I] + 1];
Detailed processing of my code, the algorithm complexity is O (n * α (n.
My code:
# Include <cstdio> <br/> # include <cstring> <br/> # include <cstdlib> <br/> # include <algorithm> <br/> using namespace STD; <br/> const int max = 80005; <br/> const int oo = 0x3f3f3f; <br/> int R [Max], H [Max]; <br/> int main () {<br/> int N; <br/> long ret = 0; <br/> scanf ("% d ", & N); <br/> H [N] = Oo; <br/> for (INT I = 0; I <n; I ++) scanf ("% d", & H [I]); <br/> r [N] = N; <br/> for (INT I = n-1; i> = 0; I --) {<br/> r [I] = I; <br/> while (H [I]> H [R [I] + 1]) R [I] = R [R [I] + 1]; <br/> RET + = R [I]-I; <br/>}< br/> printf ("% LLD/N", RET ); <br/> return 0; <br/>}< br/>
Comparison of the three algorithms:
The first algorithm must be the slowest. After all, the time complexity of O (nlogn) plus the large constant of the line tree, and the following two O (n) or approximate O (N) the algorithm is inferior.
From the perspective of the running time of the last two algorithms, the third algorithm of O (N * α (N) is faster, probably because my monotonous stack constant is too large, or the reason why my sequence has been reversed.