Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void solve() {
- long long int n;
- cin >> n;
- long long int ar[n], left = 0, right = n - 1;
- long long int res = 0;
- for(auto &i : ar)
- cin >> i;
- while(left < n - 1 && ar[left] < ar[left+1])
- ++left;
- while(right > 0 && ar[right] > ar[right-1])
- --right;
- if(left == n - 1) {
- cout << n * (n + 1) / 2 - 1 << endl;
- return;
- }
- for(int i = left; i >= 0; --i) {
- int pos = upper_bound(ar + max(right, i + (long long)2), ar + n, ar[i]) - ar;
- res = res + n - pos + 1;
- }
- res = res + n - right;
- cout << res << endl;
- }
- int main() {
- int test;
- cin >> test;
- while(test--)
- solve();
- return 0;
- }
Add Comment
Please, Sign In to add comment