inline void Work(void) { T.clear(); T.Add(b[1], 1); int j = 0; last = 1; for (int i = 2; i <= m; i++) { while (j && Ask(b, i - j, b[i]) != rank[j + 1]) j = p[j]; if (j == 0) Ask(b, i - j, b[i]); if (Ask(b, i - j, b[i]) == rank[j + 1]) j++; p[i] = j; T.Add(b[i], 1); } T.clear(); j = 0; last = 1; for (int i = 1; i <= n; i++) { while (j && Ask(a, i - j, a[i]) != rank[j + 1]) j = p[j]; if (j == 0) Ask(a, i - j, a[i]); if (Ask(a, i - j, a[i]) == rank[j + 1]) j++; if (j == m) { Ans[i] = true; j = p[j]; } T.Add(a[i], 1); } f[0] = 0; for (int i = 1; i <= n; i++) { // printf("%d ", Ans[i]); f[i] = f[i - 1]; if (Ans[i] > 0) f[i] = max(f[i], f[i - m] + 1); } // puts(""); printf("%d\n", f[n]); }
int main(void){ int T; scanf("%d", &T); while(T--) { scanf("%d", &n); int maxa = 0; for(int i = 1; i <= n; i++) { scanf("%d", a+i); maxa = maxa > a[i] ? maxa: a[i]; } BIT t; t.resize(maxa); t.clear(); for(int i = 1; i <= n; i++) { t.add(a[i], 1); c[i] = t.sum(a[i] - 1); } t.clear(); for(int i = n; i > 0; i--) { t.add(a[i], 1); d[i] = t.sum(a[i] - 1); } long long ans = 0; for(int i = 1; i <= n; i++) { ans += (long long)c[i]*(n-i-d[i]) + (long long)(i-c[i]-1)*d[i]; } printf("%lld\n", ans); } return 0; }
inline void Prepare(void) { T.clear(); T.n = MAXH; for (int i = 1; i <= m; i++) { rank[i] = make_pair(T.Cal(b[i] - 1) + 1, T.Cal(b[i])); T.Add(b[i], 1); } }