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. python类(2)

    #从python开始学习编程 学习笔记 以后看书时注意一下书上表述:好像是类属性attribute,对象特性property,对象方法 1.对于一个类下的全部个体来说,某些属性可能存在个体差异.不是所 ...

  2. vnpy交易接口学习

    1.按照github中环境准备要求,配置好环境要求. https://github.com/vnpy/vnpy mongdb安装在D:\Program Files\MongoDB\Server\3.4 ...

  3. 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL

    在实际的项目中,Aras内部的方法可能并不能完全满足我们,比如Office的组件,就必须引入,那么在Aras内部的Method中,我们如何引入外部Dll文件 首先,我们新建一个Dll文件,简单的Dem ...

  4. 正则表达式模式修正符 比如/esi

    正则表达式模式修正符 比如/esi 作者: 字体:[增加 减小] 类型:转载 下面列出了当前在 PCRE 中可能使用的修正符.括号中是这些修正符的内部 PCRE 名.修正符中的空格和换行被忽略,其它字 ...

  5. [mysql8 报错] 关闭ONLY_FULL_GROUP_BY

    bug原因: 对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中.简而言之,就是SELECT后面接的列必须 ...

  6. jetty配置远程debug

    1.配置远程jetty 服务器的 bin/jetty.sh JAVA_OPTIONS+=("-Xdebug -Xrunjdwp:server=y,transport=dt_socket,ad ...

  7. 苹果下架2.5万赌博APP!一场净化风暴正在迅速成型

    当下智能手机发展得如火如荼,但对于大众来说,体验终究还是要落到包罗万千的APP上.APP身为智能手机的灵魂,全面渗入了大众的工作.生活.娱乐.学习等多个方面.每一个APP的背后,其实都在打开着一扇通往 ...

  8. ajax上传图片到服务器简单操作

    前端: 通过Ajax方式上传文件,使用FormData进行Ajax请求.上传文件或图片时,要求的格式为enctype ="multipart/form-data"(以二进制格式上传 ...

  9. lvm镜像卷

    镜像能够分配物理分区的多个副本,从而提高数据的可用性.当某个磁盘发生故障并且其物理分区变为不可用时,你仍然可以访问可用磁盘上的镜像数据.LVM在逻辑卷内执行镜像. 系统版本 # cat /etc/ce ...

  10. PAT Advanced 1127 ZigZagging on a Tree (30) [中序后序建树,层序遍历]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree c ...