hdu238树状数组

来源:互联网 发布:网络党建工作调研报告 编辑:程序博客网 时间:2024/06/06 03:29
#include<cstdio>#include<cstring>#include<cstdlib>const int maxn=100005;typedef __int64 ll;struct node{    ll s,num;};node tnode[maxn];int n;int lowbit(int x){    return x&(-x);}node find(int x){    node ans;    ans.s=0;    ans.num=0;    if(x>0)    {        node t;        t=find(x-lowbit(x));        ans.s=tnode[x].s+t.s;        ans.num=tnode[x].num+t.num;    }    return ans;}node Query(int x,int y){    node ans,t1,t2;    t1=find(x-1);    t2=find(y);    ans.s=t2.s-t1.s;    ans.num=t2.num-t1.num;    return ans;}void Update(int x,int v){    if(x<=n)    {        tnode[x].s+=v;        tnode[x].num+=1;        Update(x+lowbit(x),v);    }}int main(){    while(~scanf("%d",&n))    {        memset(tnode,0,sizeof(tnode));        int x;        ll ans=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            node t=Query(x+1,n);            ans+=t.s+x*t.num;            Update(x,x);        }        printf("%I64d\n",ans);    }    return 0;}

0 0
原创粉丝点击