B - Sequence II (HDU 5147)
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)的更多相关文章
- Sequence II HDU - 5919(主席树)
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...
- hdu 5147 Sequence II 树状数组
Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- hdu 5147 Sequence II (树状数组 求逆序数)
题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- HDU 5919 Sequence II 主席树
Sequence II Problem Description Mr. Frog has an integer sequence of length n, which can be denoted ...
- HDOJ 5147 Sequence II 树阵
树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others) ...
- bestcoder#23 1002 Sequence II 树状数组+DP
Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Sequence II
6990: Sequence II 时间限制: 3 Sec 内存限制: 128 MB提交: 206 解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 We define an ...
随机推荐
- promise核心6 自定义promise
1.定义整体结构(不写实现) 定义一个自己的promise的库 lib(库的简写) 一个js文件.一个js模块(不能用es6 也不能commjs)(用es5模块语法 ) 匿名函数自调用.IIFE ( ...
- BZOJ 3170 [Tjoi2013]松鼠聚会
题解:切比雪夫距离转化为曼哈顿距离 枚举源点,横纵坐标互不影响,分开考虑,前缀和优化 横纵分开考虑是一种解题思路 #include<iostream> #include<cstdio ...
- c#中的Task异步编程
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/index翻译 1. 引入 Task异步 ...
- 理解String的intern()方法
API文档中的介绍: intern public String intern() Returns a canonical representation for the string object. A ...
- A - Shortest path of the king (棋盘)
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- AXURE方便的功能
(1)建立一个公共的页面,可以把一些常用的组建放进去,就和代码要封装方法一样,这样省区了用到一次画一次的麻烦. 其中可以包括:弹出框.图标.搜索框之类的. 当然还可以把经常用到的 登陆.注册.忘记密码 ...
- POJ 1850:Code 组合数学
Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8710 Accepted: 4141 Description ...
- uni-app真机调试报错request:fail abort解决方法
Android端真机调试访问本地接口数据时报错:request:fail abort 报错代码 onLoad: function(e) { uni.request({ url: 'http://loc ...
- c# 多线程——入门学习
1. 概念介绍 1.1 线程 线程是操作系统能够进行运算调度的最小单位,包含在进程之中,是进程中的实际运作单位.一条线程指的时进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不 ...
- sudo apt-get update数字签名错误解决方法
lzb@lzb:~/projects/curl-master$ sudo apt-get update 命中: http://mirrors.aliyun.com/ubuntu xenial InRe ...