#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;
}
hdu238树状数组
最新推荐文章于 2021-05-30 16:17:32 发布
