fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <stack>
  5. using namespace std ;
  6.  
  7. #define MAX 100001
  8.  
  9. int N,arr[MAX] ;
  10. stack<int> S;
  11.  
  12. int main(){
  13.  
  14. scanf("%d",&N);
  15. for(int i=1;i<=N;i++)
  16. scanf("%d",&arr[i]);
  17. reverse(arr+1,arr+1+N);
  18. long long ans = 0;
  19. for(int i=1;i<=N;i++){
  20. while(!S.empty() && arr[S.top()] < arr[i])
  21. S.pop();
  22. ans = ans + (S.empty() ? i-1 : i-S.top()-1);
  23. S.push(i);
  24. }
  25. printf("%lld\n",ans);
  26. return 0 ;
  27. }
Success #stdin #stdout 0s 3868KB
stdin
6
10
3
7
4
12
2
stdout
5