本文共 954 字,大约阅读时间需要 3 分钟。
#include#include typedef __int64 LL;const LL M = 100005;LL a[M],c[M];LL n;LL lowbit(LL x){ return x&(-x);}void add(LL pos,LL x){ LL num = pos; while(pos <= n) { a[pos] += num; c[pos] += x; pos += lowbit(pos); }}LL get_sum1(LL pos){ LL res = 0; while(pos > 0) { res += c[pos]; pos -= lowbit(pos); } return res;}LL get_sum2(LL pos){ LL res = 0; while(pos > 0) { res += a[pos]; pos -= lowbit(pos); } return res;}int main(){ LL m,x,ret,k; while(~scanf("%I64d",&n)) { LL ret = 0; memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); for(LL i = 1;i <= n;i++) { scanf("%I64d",&m); add(m,1); x = i - get_sum1(m); if(x) { k = get_sum2(n) - get_sum2(m); ret += x * m + k; } } printf("%I64d\n",ret); }}
转载地址:http://aysgi.baihongyu.com/