HDU 5147 Sequence II 树状数组
来源:互联网 发布:近年来自然灾害数据 编辑:程序博客网 时间:2024/06/06 11:03
点击打开链接
Sequence II
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 577 Accepted Submission(s): 226
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
Please calculate how many quad (a,b,c,d) satisfy:
1.
2.
3.
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 integersA1,A2,…,An .
[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <=Ai <= n
Each test case begins with a line contains an integer n.
The next line follows n integers
[Technical Specification]
1 <= T <= 100
1 <= n <= 50000
1 <=
Output
For each case output one line contains a integer,the number of quad.
Sample Input
151 3 2 4 5
Sample Output
4
Source
BestCoder Round #23
官方题解:
要统计四元组的数量我们可以通过枚举c,然后统计区间[1,c-1]有多少二元组(a,b)满足a< b且Aa<Ab ,以及统计出区间[c+1,n]有多少d满足Ac<Ad ,根据乘法原理,把这两项乘起来就可以统计到答案里了.然后我们来处理子问题:区间[1,c-1]内有多少二元组(a,b).那么我们可以枚举b,然后统计区间[1,b-1]内有多少a满足Aa<Ab ,那么这个可以通过用树状数组询问前缀和来实现.时间复杂度是O(nlogn).
//1684MS1840K#include<stdio.h>#include<string.h>#define ll __int64#define N 50007int a[N],pre[N],suf[N],C[N];//pre记录前缀,suf记录后缀int lowbit(int x){ return x&(-x);}void add(int pos,int val){ while(pos<N) { C[pos]+=val; pos+=lowbit(pos); }}int getsum(int x){ int result=0; while(x>0) { result+=C[x]; x-=lowbit(x); } return result;}int main(){ int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(C,0,sizeof(C)); for(int i=1;i<=n;i++)//计算前缀的个数 { pre[i]=getsum(a[i]); add(a[i],1); } memset(C,0,sizeof(C)); for(int i=n;i>=1;i--)//计算后缀的个数 { suf[i]=n-i-getsum(a[i]);//倒着输入,第i个数后面有n-i个数,再看看这n-i个数中是不是存在比a[i]小的 add(a[i],1); } ll ans=0,p=0;//p代表前缀的总个数 for(int i=1;i<n;i++)//枚举c的位置 { ans+=suf[i]*p; p+=pre[i]; } printf("%I64d\n",ans); }}
0 0
- hdu 5147 Sequence II(树状数组)
- HDU 5147 Sequence II 树状数组
- hdu 5147 Sequence II(树状数组)
- HDU 5147 Sequence II 树状数组
- HDU 5147 Sequence II 枚举+树状数组
- hdu 5147 Sequence II BestCoder #23 —树状数组
- 杭电hdu 5147 Sequence II (树状数组)
- [HDU 5147] Sequence II (树状数组+前缀和)
- 【树状数组】 HDOJ 5147 Sequence II
- HDOJ 5147 Sequence II 树状数组
- [ACM] hdu 5147 Sequence II (树状数组,前缀和,后缀和)
- hdu 5147 Sequence II(树状数组,前缀和,后缀和)
- HDU 5147 Sequence II
- hdu 5147 Sequence II
- Hdu 5147 Sequence II(树状数字 or 线段树 + 输入外挂 前缀和+后缀和)
- HDU5147 Sequence II(树状数组+前缀和+后缀和)
- hdu5147 Sequence II树状数组求逆序对
- hdu 5147 树状数组
- android adb常用指令
- Ansible 最佳实践 之 Vault Pseudo leaf encryption
- poj解题报告——1035
- 网页中嵌入微博
- apply 和call的用法
- HDU 5147 Sequence II 树状数组
- Android学习笔记十.fragment(二) 之《图书详情》实战
- 七款诊断调试工具、五大编程类工具、六大图片、图标处理类工具
- android下载时,通知栏进度条卡死问题
- 实现strncpy
- android顶部菜单栏+滑动图片
- Assertion failure 错误
- Mule 官方例子研究(六)
- Numpy.nonzero() 详解 numpy module中 nonzero()函数