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. 【BZOJ2044】三维导弹拦截 DP+(有上下界的)网络流

    [BZOJ2044]三维导弹拦截 Description 一场战争正在A国与B国之间如火如荼的展开. B国凭借其强大的经济实力开发出了无数的远程攻击导弹,B国的领导人希望,通过这些导弹直接毁灭A国的指 ...

  2. UESTC 485 Game(康托,BFS)

    Today I want to introduce an interesting game to you. Like eight puzzle, it is a square board with 9 ...

  3. grafana-----Templating

    模板允许更多的互动和动态的仪表板.可以将变量用在度量查询中,不必硬编码诸如服务器.应用程序和传感器名称之类的东西.变量显示在仪表板顶部的下拉式选择框中.这些下拉菜单可以很容易地改变在你的仪表板显示的数 ...

  4. VCL控件组件大都应该重载TWinControl的虚函数WndProc来进行处理窗口消息的工作

    TWinControl的构造函数中会调用MakeObjectInstance并且传递MainWndProc作为窗口消息处理函数,而MainWndProc则会调用虚函数WndProc来处理窗口消息.留个 ...

  5. [NOIP2018TG]旅行

    [NOIP2018TG]旅行 树很简单,对每个点sort儿子,贪心走就行了 基环树呢? 如果是1e5可能不太好做 但是5000的话枚举断边就可以\(n^2\)了 #include<bits/st ...

  6. DBProxy 入门到精通系列(一):DBProxy概述

    DBProxy概述 DBProxy是支持分库分表,读写分离的中间件,博客内容除了阅读github官方文档外,还会附加上本人个人测试的结果,但了解一个中间件,需要对这个中间件有一个基本的认识,方便控制. ...

  7. vue 基础核心学习

    <html> <body> <div id="app"> {{ message }} </div> <div id=" ...

  8. Mybatis参数总结(转载)

    转载自: MyBatis传入多个参数的问题 mybatis传递参数总结 一.单个参数 1.基本数据类型 (1)直接使用 List<ChargeRuleDO> tests(long id); ...

  9. 20170330 webservice代理类测试

    代理类测试 执行事物码SE80,找到之前创建好的代理类,如下图所示: 双击该代理类,进入其显示界面,如下图所示: 点击执行按钮,或者快捷键F8.如下图所示:. 逻辑端口文本框就是之前创建的逻辑端口技术 ...

  10. activiti部署到linux后流程图不显示汉字的问题

    linux和windows的字体文件一般是不一样的,默认情况下,linux的java7中一般不支持中文,activiti的动态流程图中的汉字需要java调用汉字的字库,这里需要配置一下java的汉字字 ...