hdu 2492树状数组

View Code
#include<stdio.h>
#include<string.h>
#define max 100010
int b[max],c[max];
__int64 x[max],y[max];
int a[max];
int lowbit(int x)
{
return x&(-x);
}
void update(int arr[],int x)
{
while(x<=max)
{
arr[x]++;
x+=lowbit(x);
}
}
__int64 sum(int arr[],int x)
{
__int64 ans=0;
while(x>0)
{
ans+=arr[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int t,i,n;
__int64 ans;
scanf("%d",&t);
while(t--)
{
ans=0;
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
x[i]=sum(b,a[i]-1);
update(b,a[i]);
}
for(i=n;i>=1;i--)
{
y[i]=sum(c,a[i]-1);
update(c,a[i]);
}
for(i=1;i<=n;i++)
{
ans+=(__int64(x[i]*(n-i-y[i]))+__int64((i-1-x[i])*y[i]));
}
printf("%I64d\n",ans);
}
return 0;
}

你可能感兴趣的:(树状数组)