Source Code
#include <bits/stdc++.h>
using namespace std;

void solve() {

    int n, x;
    cin >> n;
    int pos[n], l = 2e9, r = -2e9;
    for (int i = 0; i < n; i++) {
        cin >> x;
        pos[x] = i;
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        l = min(l, pos[i]);
        r = max(r, pos[i]);
        ans += 1ll * (l + 1) * (n - r);
    }
    cout << ans;

}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
        if (t) cout << "\n";
    }

    return 0;
}
Copy
Mex AshrafYousry
GNU G++17
25 ms
2.0 MB
Accepted