HDU 5147 树状数组

Sequence II

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 949    Accepted Submission(s): 380


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. 1a<b<c<dn
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
利用树状数组 统计第i个数比它小的 和比它大的 再用dp不断累加比小的乘比它大的 就出来了
 
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int maxn=50010; int a[maxn]; int pre[maxn],suf[maxn]; int n; int c[maxn]; int lowbit(int x) {     return x&(-x); } void update(int i,int x) {     while(i<=n)     {         c[i]+=x;         i+=lowbit(i);     } } int sum(int i) {     int s=0;     while(i>0)     {         s+=c[i];         i-=lowbit(i);     }     return s; } 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++)         {             pre[i]=sum(a[i]);             update(a[i],1);         }         memset(c,0,sizeof(c));         for(int i=n;i>=1;i--)         {             suf[i]=(n-i)-sum(a[i]);                     update(a[i],1);         }         long long ans=0,dp=0;         for(int i=1;i<=n-1;i++)         {             ans+=dp*suf[i];             dp+=pre[i];         }         printf("%lld\n",ans);     }     return 0; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值