#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 1e5 + 7,maxm = 2*1e4 + 7;
int input[maxm],tree[maxn],c[maxn],d[maxn];
inline int lowbit(int a) {
return a & -a;
}
int sum(int i) {
int ans = 0;
while (i) {
ans += tree[i];
i -= lowbit(i);
}
return ans;
}
void add(int i,int v) {
while (i < maxn) {
tree[i]+= v;
i += lowbit(i);
}
}
int main()
{
int t,n;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++)cin >> input[i];
memset(tree, 0, sizeof(tree));
for (int i = 1; i <= n; i++) {
c[i] = sum(input[i]);
add(input[i], 1);
}
memset(tree, 0, sizeof(tree));
for (int i = n; i >= 1; i--) {
d[i] = sum(input[i]);
add(input[i], 1);
}
long long int ans = 0;
for (int i = 1; i <= n; i++) {
ans += (long long int)c[i] * (n - i - d[i]) + (long long int)(i - c[i] - 1) * d[i];
}
cout << ans << endl;
}
return 0;
}
uva1428
最新推荐文章于 2024-08-26 14:50:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)