白鸟白鸟 不要回头望
平方是容易的。
感觉不太能 polylog,而且出现次数又比较能根号。于是考虑根分,块长为
对于大数,我们直接枚举,假设这个数是
然后考虑小数和小数。
平衡一下得到
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = 2.5e5 + 10, B = 400;
template<typename T>
void dbg(const T &t) { cout << t << endl; }
template<typename Type, typename... Types>
void dbg(const Type& arg, const Types&... args) {
cout << arg << ' ';
dbg(args...);
}
namespace Loop1st {
int n, a[N], s[N];
ll ans;
basic_string<int>buc[N];
struct Fenwick {
int tr[N];
void clear() { for (int i = 1; i <= n; i++) tr[i] = 0; }
void add(int x, int v) { while (x <= n) tr[x] += v, x += x & -x; }
int ask(int x) { int res = 0; while (x) res += tr[x], x -= x & -x; return res; }
} BIT;
void main() {
cin >> n;
ans = 0;
BIT.clear();
for (int i = 1; i <= n; i++) buc[i].clear();
for (int i = 1; i <= n; i++) cin >> a[i], buc[a[i]] += i;
for (int x = 1; x <= n; x++) {
if (buc[x].size() > B) {
for (int i = 1; i <= n; i++) s[i] = s[i - 1] + (a[i] == x);
for (int y = 1; y <= n; y++) if (buc[y].size() <= B || x < y) {
ll s0 = 0, s1 = 0, s2 = 0;
for (int j : buc[y]) {
ans += (ll)(s[n] - s[j]) * (s0 * s[j] - s1) + s1 * s[j] - s2;
s0++; s1 += s[j]; s2 += s[j] * s[j];
}
}
int sz = buc[x].size();
ans += (ll)sz * (sz - 1) * (sz - 2) * (sz - 3) / 24;
}
}
for (int i = 1; i <= n; i++) if (buc[a[i]].size() <= B) {
int sz = buc[a[i]].size();
for (int j = sz - 1; ; j--) {
if (buc[a[i]][j] == i) {
ans -= BIT.ask(i) * (sz - j - 1);
break;
}
ans += BIT.ask(buc[a[i]][j] - 1);
BIT.add(buc[a[i]][j], 1);
}
}
cout << ans << '\n';
}
}
int main() {
// freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
cin >> T;
while (T--) Loop1st::main();
return 0;
}
// start coding at 13:30
// finish debugging at 13:45