zoukankan      html  css  js  c++  java
  • UVA1428 Ping pong

    思路

    分别统计这个位置左边和右边各有多少大于和小于它的数,乘起来即可
    使用权值树状数组

    代码

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define int long long
    using namespace std;
    const int MAXN = 100000;
    struct BIT{
        int a[MAXN+10];
        int lowbit(int x){
            return x&(-x);
        }
        int query(int x){
            int ans=0;
            while(x){
                ans+=a[x];
                x-=lowbit(x);
            }
            return ans;
        }
        void add(int pos,int x){
            while(pos<=MAXN){
                a[pos]+=x;
                pos+=lowbit(pos);
            }
        }
        void init(void){
            memset(a,0,sizeof(a));
        }
    }L,R;
    int T,n,a[MAXN],ans=0;
    signed main(){
        freopen("test.in","r",stdin);
        freopen("test.out","w",stdout);
        scanf("%lld",&T);
        while(T--){
            ans=0;
            L.init();
            R.init();
            scanf("%lld",&n);
            for(int i=1;i<=n;i++){
                scanf("%lld",&a[i]);
                R.add(a[i],1);
            }
            for(int i=1;i<=n;i++){
                R.add(a[i],-1);
                ans+=L.query(a[i]-1)*(R.query(MAXN)-R.query(a[i]-1));
                ans+=R.query(a[i]-1)*(L.query(MAXN)-L.query(a[i]-1));
                L.add(a[i],1);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    URL
    VI,CI,UI
    ubuntu优化使用
    Django入门之自定义页面
    python3 连接SQLserver
    Python3 捕捉异常
    python3 异常处理
    Django入门
    较大型站立会议(交付前两天)--张永组-2014-04-15
    站立会议-2014-04-14
  • 原文地址:https://www.cnblogs.com/dreagonm/p/10683903.html
Copyright © 2011-2022 走看看