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; }
void CDQ(int nl, int nr) { if(nl>=nr) return; int mid=(nl+nr)>>1; CDQ(nl, mid); CDQ(mid+1,nr); int tl=mid; for(int tr=nr; tr>mid; tr--) { while(tl>=nl && arry[tl].l >= arry[tr].l) { bit.add( arry[tl].r, arry[tl].v); tl--; } ans[ arry[tr].t ] += bit.sum( arry[tr].r ); } for(int i=mid; i>tl; i--) bit.add( arry[i].r, -arry[i].v); tl=nl; int tp=nl; for(int tr=mid+1; tr<=nr; tr++) { while(tl<=mid && arry[tl].l < arry[tr].l) temp[tp++] = arry[tl++]; temp[tp++] = arry[tr]; } while(tl<=mid) temp[tp++] = arry[tl++]; for(int i=nl; i<=nr; i++) arry[i]=temp[i]; }
void add(int x, int c) { assert(!col[c].count(x)); int a = prv(x, c); int b = nxt(x, c); if (a != -1) { bit.del(a, b); bit.add(a, x); } bit.add(x, b); col[c].insert(x); }
void solve(){ if(!len){ puts("Poor stack!"); return; } b = BIT(ns); rep(i,ns) b.add(i,1); rep(i,ms){ for(int j = k[i]-1; j >= 0; --j){ int pos = b.find(a[j]); b.add(pos,-1); } } rep(i,ns) if(b.sum(i,i+1)) printf("%d", seq[i]); puts(""); }
int calc(int p) { printf("calc(%d)\n", p); int cnt[20]; memset(cnt, 0, sizeof cnt); int max_power = 0; memset(power, 0, sizeof power); for(int i = 0; i < n; i++) { int num = v[i]; while(num % p == 0) { power[i]++; num /= p; } cnt[power[i]]++; max_power = max(max_power, power[i]); } printf("max_power=%d ", max_power); bit.init(); for(int i = 1; i <= max_power; i++) { bit.add(i, cnt[i]); } int ans = 0; int cur = p; int base[20]; for(int i = 1; cur <= maxa; i++) { base[i] = cur; cur = (cur * 1LL * p) % mod; } for(int i = 0; i < n; i++) { if(power[i] == 0) continue; printf("total(%d - %d)=%d ", bit.sum(max_power), bit.sum(power[i] - 1), total(bit.sum(max_power) - bit.sum(power[i] - 1))); ans = (ans + (base[power[i]] * 1LL * total(bit.sum(max_power) - bit.sum(power[i] - 1))) % mod) % mod; printf("i=%d ans=%d ", i, ans); for(int j = power[i] - 1; j >= 1; j--) { ans = (ans + (base[j] * 1LL * total(cnt[j])) % mod) % mod; printf("cnt[%d]=%d ans=%d ", j, cnt[j], ans); } printf("ans=%d\n", ans); cnt[power[i]]--; bit.add(power[i], -1); } return ans; }
void del(int x, int c) { assert(col[c].count(x)); int a = prv(x, c); int b = nxt(x, c); bit.del(x, b); if (a != -1) { bit.del(a, x); bit.add(a, b); } col[c].erase(x); }
int dfs(int c, int k) { int iv = bit.sum(1000) - bit.sum(vertex[c]); if (k <= 1) { return iv; } visited[c] = 1; bit.add(vertex[c], 1); int res = 1 << 30; if (k > 1) { for (int v : edge[c]) { if (!visited[v]) { res = min(res, iv + dfs(v, k - 1)); } } } bit.add(vertex[c], -1); visited[c] = 0; return res; }
int main() { int x, r, h; int a, b; scanf("%d%d", &N, &Q); bit.init(MAX_X + MAX_H); for (int i = 0; i < N; i++) { scanf("%d%d%d", &x, &r, &h); for (int j = 0; j < h; j++) { bit.add(x + j + 1, (double)r * r * M_PI * h * 1.0/3.0 * (pow(h - j, 3) - pow(h - j - 1, 3)) / pow(h, 3)); } } for (int i = 0; i < Q; i++) { scanf("%d%d", &a, &b); ans = bit.sum(b) - bit.sum(a); printf("%lf\n", ans); } return 0; }
void erase(long long val){ if(tset.find(val) == tset.end()) return; tree.add(val, -1); tset.erase(val); }
void insert(long long val){ if(tset.find(val) != tset.end()) return; tree.add(val, 1); tset.insert(val); }