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. Super Resolution

    Super Resolution Accepted : 121   Submit : 187 Time Limit : 1000 MS   Memory Limit : 65536 KB  Super ...

  2. 利用Hibernate注解生成表

    转自:http://blog.csdn.net/madison__/article/details/55677099 Hibernate4注释 @Entity(name = "tbl_use ...

  3. django注册在使用hashlib对密码加密时报Unicode-objects must be encoded before hashing

    在使用sh1等hashlib方法进行加密时报:Unicode-objects must be encoded before hashing 解决办法:对要加密的字符串指定编码格式 解决之前: s1=s ...

  4. Linux centos下设置定时备份任务

    实现准备 # 需要备份文件路径:/opt/apollo/logs/access_log [root@localhost opt]# cd apollo/ [root@localhost apollo] ...

  5. Linux中的awk命令

    awk '条件1{动作1} 条件2{动作2} ...'  文件名 条件: BEGIN          在处理文件里的第一行数据之前执行 END              在处理完文件里的最后一行数据 ...

  6. vue 项目 npm install 报错解决

    node-sass 安装报错解决办法 2017年04月15日 14:34:25 阅读数:20189 E:\kibana>npm install node-sass > node-sass@ ...

  7. Mybatis使用pageHelper步骤

    1.在pom.xml中添加如下依赖: <dependency> <groupId>com.github.pagehelper</groupId> <artif ...

  8. m进制转n进制

    http://www.cnblogs.com/pkuoliver/archive/2010/10/27/Convert-m-number-to-n-number.html 从这道题中可以看出,数论中存 ...

  9. tensorflow 张量的阶、形状、数据类型及None在tensor中表示的意思。

    x = tf.placeholder(tf.float32, [None, 784]) x isn't a specific value. It's a placeholder, a value th ...

  10. Python基础-高阶函数

    1.高阶函数变量可以指向函数函数的参数可以接收变量一个函数可以接收另一个函数作为参数 def f(n): return n * n def fansik(a, b, func): return fun ...