题目连接:
lowbit..
1 #include2 #include 3 #define ll long long 4 const int maxn=1e6+10; 5 6 int c[maxn]; 7 int a[20005]; 8 int m1[20005],m2[20005]; 9 10 int lowbit(int x)11 {12 return x&(-x);13 }14 15 int sum(int x)16 {17 int ret=0;18 while(x>0)19 {20 ret+=c[x];21 x-=lowbit(x);22 }23 return ret;24 }25 26 void add(int x,int d)27 {28 while(x =1;i--)51 {52 m2[i]=sum(a[i]);53 add(a[i],1);54 }55 ll ans=0;56 for(int i=1;i<=n;i++)57 ans+=m1[i]*1ll*(n-i-m2[i])+m2[i]*1ll*(i-m1[i]-1);58 printf("%lld\n",ans);59 }60 }