zoukankan      html  css  js  c++  java
  • B

    Long long ago, there is a sequence A with length n. All numbers in this sequence is no smaller than 1 and no bigger than n, and all numbers are different in this sequence. 
    Please calculate how many quad (a,b,c,d) satisfy: 
    1. 1a<b<c<dn1≤a<b<c<d≤n 
    2. Aa<AbAa<Ab 
    3. Ac<AdAc<Ad

    InputThe first line contains a single integer T, indicating the number of test cases. 
    Each test case begins with a line contains an integer n. 
    The next line follows n integers A1,A2,,AnA1,A2,…,An. 

    [Technical Specification] 
    1 <= T <= 100 
    1 <= n <= 50000 
    1 <= AiAi <= n
    OutputFor each case output one line contains a integer,the number of quad.Sample Input

    1
    5
    1 3 2 4 5

    Sample Output

    4
    题解:找多少种满足条件的四元数组对,类似于找逆序对的方法,来找顺序对。从前往后跑一边记录下以i为结尾的顺序对有多少个,从后往前跑一边记录以i为开头的顺序对有多少个,之后从前往后跑一遍累加答案即可。
    #include <iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    int lowbit(int x){return x&-x;}
    const int maxn=50010;
    int pre[maxn],suf[maxn],summ[maxn];
    int a[200100],h[200100],c[200020];
    int n,m;
    void update(int x,int v)//单点修改(x节点加上v)
    {
        for(int i=x;i<=n;i+=lowbit(i))
            c[i]+=v;
    }
    int sum(int x)//sum[1,x]
    {
        int ans=0;
        for(int i=x;i>=1;i-=lowbit(i))
            ans+=c[i];
        return ans;
    }
    
    int main()
    {
        std::ios::sync_with_stdio(0);
        int T;
        cin>>T;
        while(T--){
            cin>>n;
            memset(c,0,sizeof(c));
            for(int i=1;i<=n;i++)cin>>a[i];
            for(int i=1;i<=n;i++){
                pre[i]=sum(a[i]);
                update(a[i],1);
            }
            memset(c,0,sizeof(c));
            for(int i=n;i>=1;i--){
                suf[i]=n-i-sum(a[i]);
                update(a[i],1);
            }
            for(int i=1;i<=n;i++)summ[i]=summ[i-1]+pre[i];
            ll ans=0;
            for(int i=2;i<=n-2;i++)//枚举b的位置
            {
                ans+=(ll)summ[i]*suf[i+1];
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    jQuery -&gt; 获取指定上下文中的DOM元素
    安装Nginx须要系统的辅助软件(linux)
    ASP.NET MVC + MySQL で開発環境構築
    Parameters.Add和Parameters.AddWithValue
    MVC js动态生成from提交数据然后生成文件下载
    C# 压缩文件 的创建
    C#中的GetElementsByClassName方法
    windows下使用pip安装python模块lxml
    总结WCF开发中遇到的几个问题
    Xpath
  • 原文地址:https://www.cnblogs.com/cherish-lin/p/10958023.html
Copyright © 2011-2022 走看看