Topic Connection: https://vjudge.net/problem/UVALive-4329
Lowbit.
1#include <cstdio>2#include <cstring>3 #definell Long Long4 Const intmaxn=1e6+Ten;5 6 intC[MAXN];7 inta[20005];8 intm1[20005],m2[20005];9 Ten intLowbit (intx) One { A returnx& (-x); - } - the intSumintx) - { - intret=0; - while(x>0) + { -ret+=C[x]; +x-=lowbit (x); A } at returnret; - } - - voidAddintXintd) - { - while(x<MAXN) in { -c[x]+=D; tox+=lowbit (x); + } - } the * intMain () $ {Panax Notoginseng intT,n; -scanf"%d",&t); the while(t--) + { AMemset (c,0,sizeof(c)); thescanf"%d",&n); + for(intI=1; i<=n;i++) - { $scanf"%d",&a[i]); $m1[i]=sum (a[i]); -Add (A[i],1); - } theMemset (c,0,sizeof(c)); - for(inti=n;i>=1; i--)Wuyi { them2[i]=sum (a[i]); -Add (A[i],1); Wu } -ll ans=0; About for(intI=1; i<=n;i++) $ans+=m1[i]*1ll* (N-i-m2[i]) +m2[i]*1ll* (i-m1[i]-1); -printf"%lld\n", ans); - } -}
uvalive4329 (tree-like array)