Sequence II

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
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≤n
2. Aa<Ab
3. Ac<Ad
 
Input
The 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,…,An.

[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <= Ai <= n

 
Output
For each case output one line contains a integer,the number of quad.
 
Sample Input
1
5
1 3 2 4 5
 
Sample Output
4
 
Source
思路:用树状数组求逆序对;
   pre[i]存前边比a[i]小的;
   nex[i]存a[i]开始的二元组;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=5e4+,M=1e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int tree[N];
int a[N];
int pre[N];
int nex[N];
void init()
{
memset(tree,,sizeof(tree));
memset(pre,,sizeof(pre));
memset(nex,,sizeof(nex));
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int c)
{
while(x<N)
{
tree[x]=tree[x]+c;
x+=lowbit(x);
}
}
int query(int x)
{
int sum=;
while(x)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
pre[i]=query(a[i]-);
update(a[i],);
}
memset(tree,,sizeof(tree));
for(int i=n;i>=;i--)
{
nex[i]=query(n)-query(a[i])+nex[i+];
update(a[i],);
}
ll ans=;
for(int i=;i<=n;i++)
ans+=(ll)pre[i]*nex[i+];
printf("%lld\n",ans);
}
return ;
}

hdu 5147 Sequence II 树状数组的更多相关文章

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

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

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

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

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

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

  4. HDU 5273 Dylans loves sequence【 树状数组 】

    题意:给出n个数,再给出q个询问,求L到R的逆序对的个数 先自己写的时候,是每次询问都重新插入来求sum(r)-sum(l) 果断T 后来还是看了别人的代码---- 预处理一下,把所有可能的区间的询问 ...

  5. hdu 5147 Sequence II

    http://acm.hdu.edu.cn/showproblem.php?pid=5147 题意:问有多少个这样的四元组(a,b,c,d),满足条件是 1<=a<b<c<d; ...

  6. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  7. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  8. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  9. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

随机推荐

  1. 【BZOJ4716】假摔 二分+暴力

    [BZOJ4716]假摔 Description [题目背景] 小Q最近喜欢上了一款游戏,名为<舰队connection>,在游戏中,小Q指挥强大的舰队南征北战,从而成为了一名dalao. ...

  2. 【BZOJ2506】calc 分段+vector+莫队

    [BZOJ2506]calc Description          给一个长度为n的非负整数序列A1,A2,…,An.现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且A ...

  3. shell脚本中格式化日期

    date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] ...

  4. 【转】C#操作word定位光标

    十一.上下左右移动光标位 private void moveLeft() { object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdWor ...

  5. Spring 事务机制详解(事务的隔离性和传播性)

    原文出处: 陶邦仁 Spring事务机制主要包括声明式事务和编程式事务,此处侧重讲解声明式事务,编程式事务在实际开发中得不到广泛使用,仅供学习参考. Spring声明式事务让我们从复杂的事务处理中得到 ...

  6. Error: unable to connect to node rabbit@10: nodedown 修改hostname后异常

    https://blog.csdn.net/witsmakemen/article/details/22651365 [root@d bin]# rabbitmqctl start_appStarti ...

  7. 解决windows server 2003不识别移动硬盘

    解决windows server2003不显示移动硬盘的问题: 1.进入命令提示符环境(也就是DOS) 2.进入DISKPART程序 3.输入AUTOMOUNT ENABLE指令 4.输入OK 下次U ...

  8. Hosts文件的位置

    Operating System Version(s) Location Unix, Unix-like, POSIX   /etc/hosts Microsoft Windows 3.1 %WinD ...

  9. DMR技术白皮书

    DMR技术白皮书 主页(http://pttcn.net):DMR技术白皮书 关于DMR 1.模拟技术的局限性 虽然模拟技术仍具有不少优势,如低廉的成本.可自定的功能以及简便的搭建方式等.但模拟技术已 ...

  10. (4.5)DBCC的概念与用法(DBCC TRACEON、DBCC IND、DBCC PAGE)

    转自:http://www.cnblogs.com/huangxincheng/p/4249248.html DBCC的概念与用法 一:DBCC 1:什么是DBCC 我不是教学老师,我也说不到没有任何 ...