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. Thread--CountDownLatch & CyclicBarrier

    参考:http://www.importnew.com/21889.html CountDownLatch countDown() 方法执行完只是计数器减一, 并不会阻塞当前运行线程的的后续代码执行. ...

  2. C++逐行读取txt

    C++读取txt文件的时候可以使用std::ifstream来实现,如果打开文件失败的话,其变量会是空的,所以可以用来判断是否打开成功.  #include <stdlib.h>  #in ...

  3. Win10下 Java环境变量配置

    安装java的JDK   下载地址 此电脑->属性->高级设置 "系统变量"新建   变量名:Java_Home   变量值:D:\Program Files\Java ...

  4. 2020牛客寒假算法基础集训营3 B 牛牛的DRB迷宫II

    题目描述 牛牛有一个n*m的迷宫,对于迷宫中的每个格子都为'R','D','B'三种类型之一,'R'表示处于当前的格子时只能往右边走'D'表示处于当前的格子时只能往下边走,而'B'表示向右向下均可以走 ...

  5. 哈希表hashTable的Java设计

    1:哈希表的概念 2:设计原理 3:哈希表的Java设计

  6. Hadoop的常用指令

    -help:查看帮助 hadoop fs -help rm -rm [-f] [-r|-R] [-skipTrash] <src> ... : Delete all files that ...

  7. 解析java实体类

    对java实体类的众多理解: A .就是属性类,通常定义在model层里面 B. 一般的实体类对应一个数据表,其中的属性对应数据表中的字段. 好处: 1.对对象实体的封装,体现OO思想. 2.属性可以 ...

  8. 201312-2 ISBN号码 Java

    就是把-去掉,然后验证,只需要改最后一位. import java.util.Scanner; public class Main { public static void main(String[] ...

  9. 吴裕雄--天生自然TensorFlow高层封装:Estimator-DNNClassifier

    # 1. 模型定义. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist impor ...

  10. Tkinter控件

    1.顶层(Toplevel) Toplevel为其他控件提供单独的容器.共有四种类型(1)主顶层,作为根被应用,应该就是root(2)子顶层,依赖于根,根破坏,子顶层也被破坏(3)临时顶层,画在父顶层 ...