"The main topic"
All number pairs (i,j) satisfy any a[k]<=a[i] and a[k]<=a[j].
Figuratively speaking, there is a group of people standing in a row, each person has a certain height, and then asked how many people can see each other.
The number pairs (I,J) are simply referred to as the number pairs that can be seen.
"Parsing" monotonic stacks
First, borrow the previous question: [vijos]1926 purple bracelet, the maximum value of any interval maximum value of the largest difference or the second large value.
Reviewing the monotony stack is the stack that stores the monotonically data from high to low.
To borrow the previous practice, the things that are found are relative to all the visible pairs, for all A[i] equal to the number of visible pairs, only to forget once.
So in fact each time higher than others when the operation just add S (a[i]), the first time shorter than others with 1 instead of S (A[i]).
To expand the contents of the stack, not only the elements, but also save the number, so that the solution.
Monotonic Stack Code:
#include <cstdio> #include <cstring> #include <cstdlib>using namespace Std;const int n=500001;struct Stack{int W,c;} Stk[n];int size;int n,w[n],cnt;inline int read (void) {int s=0,f=1; char C=getchar (); for (;c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') f=-1;for (; ') 0 ' <=c&&c<= ' 9 '; C=getchar ()) s= (s<<1) + (s<<3) +c-' 0 '; return s*f;} int main (void) {n=read (); for (int i=1;i<=n;i++) W[i]=read (), for (int. i=1;i<=n;i++) {for (; Size&&stk[size] . w<w[i];size--) {cnt+=stk[size].c;stk[size].w=stk[size].c=0;} if (Size&&stk[size].w==w[i]) {cnt+=stk[size].c;stk[size].c++;if (size-1) cnt++;} Else{if (size) cnt++;stk[++size].w=w[i];stk[size].c=1;}} printf ("%d\n", CNT); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Rqnoj" 460 Connaught's queue