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; }
int main() { ios_base::sync_with_stdio(false); int n, m, k; int x, y; cin >> n >> m >> k; op.init(m+10); a.init(n+10); for (int i=1; i<=n; ++i) { cin >> x; a.add_interval(i, i, x); } for (int i=1; i<=m; ++i) cin >> l[i] >> r[i] >> d[i]; for (int i=0; i<k; ++i) { cin >> x >> y; op.add_interval(x, y, 1); } for (int i=1; i<=m; ++i) a.add_interval(l[i], r[i], op.sum(i)*d[i]); for (int i=1; i<=n; ++i) cout << a.sum(i) << (i < n ? " " : "\n"); 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 solve() { ll ans = 0; BIT b; for (int j = 0; j < n; j++) { ans += j - b.sum(b.a[j]); } }
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; }
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 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 main(int argc, const char * argv[]) { int ff; int v1, v2, t; char query[20]; BIT bit; scanf("%d", &ff); for (int tt = 1; tt <= ff; tt++) { printf("Case %d:\n", tt); int n; scanf("%d", &n); bit.build(n+1); for (int i = 1; i <= n; i++) { scanf("%d", &t); bit.modify(i, t); } bool fl = true; while (fl) { scanf("%s", query); switch (query[0]) { case 'Q': scanf("%d %d", &v1, &v2); printf("%d\n", bit.sum(v1, v2)); break; case 'A': scanf("%d %d", &v1, &v2); bit.modify(v1, v2); break; case 'S': scanf("%d %d", &v1, &v2); bit.modify(v1, -v2); break; case 'E': fl = false; break; } } } }