hdu 5147 (树状数组的应用)
来源:互联网 发布:淘宝卖家正在被处罚 编辑:程序博客网 时间:2024/05/16 06:44
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5147
Sequence II
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 299 Accepted Submission(s): 131
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
Recommend
heyang | We have carefully selected several similar problems for you: 5149 5148 5146 5145 5142
思路:(1)首先暴力枚举啥的肯定不行~(2)看看时间1000ms,再看一下数据大小50000,所以肯定有一个O(n)的硬循环,再想想来个O(logn)的处理就差不多了~
(3)枚举C点,那么[1 , c-1]区间内,满足i<j&&a[i]<a[j] 的个数设为x; [c,n]区间内比c大的个数设为用;根据乘法原理,x*y就是所要的,那么枚举所有的C点,累加所有的x*y;
#include <iostream>#include <stdio.h>#include <string>#include <string.h>#include <algorithm>const int N=1e5+100;using namespace std;typedef long long ll;int c[N],n; //树状数组int f[N],g[N];int a[N];int lowbit(int x){ return x&-x;}void update(int x){ while(x<=n) { c[x]+=1; x+=lowbit(x); }}int getsum(int x){ int ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans;}int main(){ int T; scanf("%d",&T); while(T--) { 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++) { f[i]=getsum(a[i]); update(a[i]); } memset(c,0,sizeof(c)); for(int i=n;i>=1;i--) { update(a[i]); g[i]=n-i+1-getsum(a[i]); } ll ans=0,sum=0; for(int i=1;i<=n;i++) { ans+=sum*g[i]; sum+=f[i]; } printf("%I64d\n",ans); } return 0;}
0 0
- hdu 5147 (树状数组的应用)
- hdu 3874 Necklace(树状数组的灵活应用)
- HDU 3874 Necklace 树状数组的应用
- hdu 3564 树状数组的灵活应用
- HDU 5122 树状数组应用
- 树状数组的应用
- 树状数组的应用
- hdu 5147 树状数组
- hdu 5147 树状数组
- HDU 5147 树状数组
- hdu(1166):敌兵布阵—树状数组的应用
- hdu 2689 Sort it 一维树状数组的应用
- hdu 2642 Stars--二维树状数组的应用
- hdu-5057(离线的树状数组)
- 树状数组的应用(SOJ2497)
- 树状数组的应用(SOJ2559)
- 树状数组的应用(SOJ2728)
- HDU 1556 Color the ball (树状数组简单应用)
- 常见的浏览器兼容问题的处理
- 《一步之遥》之观后感
- csu 1346: 变色龙
- 【vs】*pdb时出错;请检查是否是磁盘空间不足、路径无效或权限不够
- 源码系列:二叉排序树
- hdu 5147 (树状数组的应用)
- 秀丸 行内容批量修改
- C++学习之类
- HDU 5147 Sequence II
- 在MyEclipse中,学习xml,配置aop的方法:如下:
- 组合数学
- 巧用视图
- OpenStack 最小化安装配置(五):KeyStone服务配置
- C++学习之类中构造函数初始化操作