标签:blank each time style after other sample change miss
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4766 Accepted Submission(s): 1727
#include<iostream> #include<string.h> #define ll long long using namespace std; ll a[100005],b[100005],c[100005]; //a[i]保存原始数据,b[i]保存比a[i]小的数的个数,c[i]保存所有比a[i]小的数的和 ll lowbit(ll x) { return x&(-x); } ll getnum(ll x)//求比x小的数的个数 { ll cnt=0; while(x>0) { cnt=cnt+b[x]; x=x-lowbit(x); } return cnt; } ll getsum(ll x)//求比x小的数的和 { ll ans=0; while(x>0) { ans=ans+c[x]; x=x-lowbit(x); } return ans; } void add(ll x,ll y)//更新,对第x个位置的数进行更新,y是更新值 { while(x<=100000) { b[x]=b[x]+1; c[x]=c[x]+y; x=x+lowbit(x); } } int main() { int n; while(~scanf("%d",&n)) { ll ans=0,sum=0; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); add(a[i],a[i]);//将第x个位置的值,修改为x sum=sum+a[i];//sum求所有数的和 ans=ans+a[i]*(i-getnum(a[i]));//i-getnum(a[i])是比a[i]大的数的个数 ans=ans+sum-getsum(a[i]);//sum-getsum(a[i])是所有比a[i]大的数的和 } printf("%lld\n",ans); } return 0; }
hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
标签:blank each time style after other sample change miss
原文地址:https://www.cnblogs.com/-citywall123/p/11559424.html