hdu 4000 树状数组

思路:找出所有 a<b<c||a<c<b的情况,在找出所有的a<b<c的情况。他们相减剩下就是a<c<b的情况了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Maxn 100010
#define lowbit(x) (x&(-x))
using namespace std;
int C[Maxn],n;
int Sum(int pos)
{
    int sum=0;
    while(pos)
    {
        sum+=C[pos];
        pos-=lowbit(pos);
    }
    return sum;
}
void update(int pos)
{
    while(pos<=n)
    {
        C[pos]++;
        pos+=lowbit(pos);
    }
}
int main()
{
    int t,i,a,cnt,Case=0;
    __int64 ans1,ans2;
    scanf("%d",&t);
    while(t--)
    {
        memset(C,0,sizeof(C));
        ans1=0;
        ans2=0;
        cnt=0;
        scanf("%d",&n);
        int temp;
        __int64 x;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a);
            temp=Sum(a);
            x=(n-a-cnt+temp);
            ans1+=x*(x-1)/2;
            ans2+=temp*x;
            cnt++;
            update(a);
        }
        printf("Case #%d: %I64d
",++Case,(ans1-ans2)%100000007);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wangfang20/p/3230388.html