Source Code
#include <bits/stdc++.h>
using namespace std;
const int N = 8e6 + 9;
const int mod = 1e9 + 7;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int arr[n];
        int fre[n + 1] = {0};
        for(int i = 0; i < n; i++){
            cin >> arr[i];
            fre[arr[i]] = i;
        }
        int l = fre[0] , r = fre[0];
        long long ans = 0;
        for(int i = 0; i <= n - 1; i++){
            int ind = fre[i];
            l = min(l , ind);
            r = max(r , ind);
            long long a = l + 1;
            long long b = n - r;
            ans += a * b;
        }
        cout << ans << "\n";

    }
    return 0;
}  
Copy
Mex AbduSaber
GNU G++17
51 ms
1.9 MB
Accepted