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 ...
随机推荐
- Tensorflow学习教程------普通神经网络对mnist数据集分类
首先是不含隐层的神经网络, 输入层是784个神经元 输出层是10个神经元 代码如下 #coding:utf-8 import tensorflow as tf from tensorflow.exam ...
- 利用mysecureshell搭建sftp服务
1.下载对应的mysecureshell-1.33-1.x86_64.rpm包 2.安装mysecureshell-1.33-1.x86_64.rpm 3.添加ftp用户 useradd ftp 4. ...
- 基础nginx配置文件
nginx的配置文件很长,如果开始就看全部的话会懵逼,以下以最简单的配置文件来学习. 目标:定义一个虚拟主机127.0.0.1 端口是8080 [root@localhost conf]# cat ...
- mysql 去除重复 Select中DISTINCT关键字的用法(查询两列,只去掉重复的一列)
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...
- Tomcat启动失败原因: More than one fragment with the name [spring_web] was found. 解决
将一个eclipse上搭建好的项目移到idea开发时遇到的问题,tomcat启动时报了3个错误 -Nov- :: ms -Nov- ::)-127.0.0.1] org.apache.tomcat.u ...
- Python Learning Day3
爬虫练习 说是练习,实际是尝试了一些还没有具体了解的方式吧hhhhh' 基于urllib实现 import urllib.request import re url="https://www ...
- JavaWeb乱码问题及统一全站编码(通过Filter实现)
1. public class CharacterFilter implements Filter { private String characterEncoding = null; FilterC ...
- try{}catch{}finally{}使用总结
import java.util.Scanner; class MyException extends Exception { public MyException(String Message) { ...
- goahead调试经验
一.参考网址 1.源码的github地址 2.Web开发之Goahead 二.技术细节 1.默认网页的存放目录和名称 1)目录:在main.c文件中有*rootWeb定义,如: 2)网页名:在mai ...
- java实现图片文件与Base64的互转
通过form表单上传图片时,有时候web容器对文件大小的限制会影响我们上传.这时,前端页面可以考虑将图片转换成base64串来实现上传. 图片与Base64的互转,其实就是利用了文件流与Base64的 ...