码迷,mamicode.com
首页 > 编程语言 > 详细

Hdu 2492 树状数组 Ping pong

时间:2015-09-01 01:26:27      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define MAXN 100005
 5 #define M 20005
 6 typedef __int64 LL ;
 7 using namespace std;
 8 LL n,max_num ;
 9 LL s[M],a[M],b[M],c[MAXN];
10 LL lowbit(LL x){
11     return x&(-x);
12 }
13 LL sum(LL x){
14     LL ret = 0;
15     while(x > 0){
16         ret += c[x];
17         x -= lowbit(x);
18     }
19     return ret;
20 }
21 void add(LL x, LL d){
22     while(x<=max_num){
23         c[x] += d;
24         x += lowbit(x);
25     }
26 }
27 int main(){
28     LL t,i,j,ans;
29     cin>>t;
30     while(t--){
31         cin>>n;
32         max_num = 0;
33         for(i = 1;i <= n ;i++){
34             scanf("%I64d",&s[i]);
35             if(s[i]>max_num)
36                 max_num = s[i];
37         }
38         memset(c,0,sizeof(c));
39         for(i = 1;i <= n ; i++){
40             add(s[i],1);
41             a[i] = sum(s[i]-1);
42         }
43         memset(c,0,sizeof(c));
44         for(i = n; i>0 ;i--){
45             add(s[i],1);
46             b[i] = sum(s[i]-1);
47         }
48         ans = 0;
49         for(i = 1;i <= n ;i++){
50             //cout<<a[i]<<" "<<b[i]<<endl;
51             ans = ans+a[i]*(n-i-b[i])+(i-a[i]-1)*b[i];
52         }
53         cout<<ans<<endl;
54     }
55     return 0;
56 }

 

Hdu 2492 树状数组 Ping pong

标签:

原文地址:http://www.cnblogs.com/fancy-itlife/p/4774663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!