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=;
int pre[maxn],suf[maxn],summ[maxn];
int a[],h[],c[];
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=;
for(int i=x;i>=;i-=lowbit(i))
ans+=c[i];
return ans;
} int main()
{
std::ios::sync_with_stdio();
int T;
cin>>T;
while(T--){
cin>>n;
memset(c,,sizeof(c));
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<=n;i++){
pre[i]=sum(a[i]);
update(a[i],);
}
memset(c,,sizeof(c));
for(int i=n;i>=;i--){
suf[i]=n-i-sum(a[i]);
update(a[i],);
}
for(int i=;i<=n;i++)summ[i]=summ[i-]+pre[i];
ll ans=;
for(int i=;i<=n-;i++)//枚举b的位置
{
ans+=(ll)summ[i]*suf[i+];
}
cout<<ans<<endl;
}
return ;
}

B - Sequence II (HDU 5147)的更多相关文章

  1. Sequence II HDU - 5919(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  2. hdu 5147 Sequence II 树状数组

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  3. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  5. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  6. HDU 5919 Sequence II 主席树

    Sequence II Problem Description   Mr. Frog has an integer sequence of length n, which can be denoted ...

  7. HDOJ 5147 Sequence II 树阵

    树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    ...

  8. bestcoder#23 1002 Sequence II 树状数组+DP

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. Sequence II

    6990: Sequence II 时间限制: 3 Sec  内存限制: 128 MB提交: 206  解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 We define an ...

随机推荐

  1. 自定义View不显示的问题

    问题描述: 我自定义了一个把 SwipeRefreshLayout 和 RecyclerView 封装在一起的 View ,但是发现 List 不能正常的显示出来,本以为是数据源出现问题,debug了 ...

  2. Mysql--主库不停机搭建备库

    参考:http://blog.csdn.net/luozuolincool/article/details/38494817 mysqldump --skip-lock-tables --single ...

  3. scala通过尾递归解析提取字段信息

    一.背景 获取数据中以“|”作为字段间的分隔符,但个别字段中数据也是以“|”作为分隔符.因此,在字段提取时需要保护数据完整性. 二.实现 1.数据以“|”分隔,可以采用递归方式迭代解析.通过尾递归方式 ...

  4. kill -HUP 什么意思?

    参考 74.在DNS系统测试时,设named进程号是53,命令 D 通知进程重读配置文件.A kill –USR2 53 B kill –USR1 53 C kill -INT 63 D kill – ...

  5. wordcloud安装错误信息的解决

    参考链接:https://www.zhihu.com/people/si-kong-ji-54/posts 在windows下command里运行指令   pip install wordcloud  ...

  6. Python Scrapy的QQ音乐爬虫 音乐下载、爬取歌曲信息、歌词、精彩评论

    QQ音乐爬虫(with scrapy)/QQ Music Spider UPDATE 2019.12.23 已实现对QQ音乐文件的下载,出于版权考虑,不对此部分代码进行公开.此项目仅作为学习交流使用, ...

  7. 7)给tab下面添加一个子非模态对话框

    1)还是沿袭(6)那个代码 2)下面是步骤: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·~~~~~~~~~~~~~~~~~~~~~~~~~~~ 然后,修改这个对 ...

  8. Object arrays cannot be loaded when allow_pickle=False

    问题再现 代码是Deep Learning with Python中的: from keras.datasets import imdb (train_data, train_labels), (te ...

  9. mysql的show status和show global status区别在哪

    show status                   本次会话的参数状态show global status        本次MYSQL服务开启(或重置)到现在总请求数

  10. Ubuntu的man中文包安装

    apt-get install manpages-zh vi /etc/manpath.config :,$s#/usr/share/man#/usr/share/man/zh_CN#g 第一个命令: ...