zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    操作系统要点总结
    ARP的通信过程
    判断网段、子网、网络号
    C++要点总结
    枚举类型
    C指针总结
    C运算符总结
    替换空格
    WCF编写时候的测试
    WCF创建到使用到发布
  • 原文地址:https://www.cnblogs.com/wangfang20/p/3230388.html
Copyright © 2011-2022 走看看