其他分享
首页 > 其他分享> > B - Sequence II (HDU 5147)

B - Sequence II (HDU 5147)

作者:互联网

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. 1≤a<b<c<d≤n1≤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 <= nOutputFor 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;
}

 

标签:HDU,sequence,int,5147,long,II,maxn,line,include
来源: https://www.cnblogs.com/cherish-lin/p/10958023.html