Guest User

Untitled

a guest
Jul 27th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 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 << endl;
  20.     return;
  21.   }
  22.  
  23.   for(int i = left; i >= 0; --i) {
  24.     int pos = upper_bound(ar + min(right, i + (long long)2), ar + n, ar[i]) - ar;
  25.     res = res + n - pos + 1;
  26.     //cout << "for " << ar[i] << " pos = " << pos << " and res = " << res << endl;
  27.   }
  28.  
  29.   res = res + n - right;
  30.   cout << res << endl;
  31. }
  32.  
  33. int main() {
  34.     int test;
  35.     cin >> test;
  36.    
  37.     while(test--)
  38.         solve();
  39.    
  40.     return 0;
  41. }
Add Comment
Please, Sign In to add comment