Guest User

Untitled

a guest
Jul 28th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve() {
  5.     long long int n;
  6.     cin >> n;
  7.     long long int ar[n], left = 0, right = n  - 1;
  8.   long long int res = 0;
  9.  
  10.   for(auto &i : ar)
  11.     cin >> i;
  12.  
  13.   while(left < n - 1 && ar[left] < ar[left+1])
  14.     ++left;
  15.   while(right > 0 && ar[right] > ar[right-1])
  16.     --right;
  17.  
  18.   if(left == n - 1) {
  19.     cout << n * (n + 1) / 2 - 1 << endl;
  20.     return;
  21.   }
  22.  
  23.   for(int i = left; i >= 0; --i) {
  24.     int pos = upper_bound(ar + max(right, i + (long long)2), ar + n, ar[i]) - ar;
  25.     res = res + n - pos + 1;
  26.   }
  27.  
  28.   res = res + n - right;
  29.   cout << res << endl;
  30. }
  31.  
  32. int main() {
  33.     int test;
  34.     cin >> test;
  35.  
  36.     while(test--)
  37.         solve();
  38.  
  39.     return 0;
  40. }
Add Comment
Please, Sign In to add comment