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

uvalive4329(树状数组)

时间:2017-04-21 09:54:38      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:lld   const   ems   mem   return   cst   turn   code   i++   

题目连接:https://vjudge.net/problem/UVALive-4329

lowbit..

 1 #include<cstdio>
 2 #include<cstring>
 3 #define ll long long
 4 const int maxn=1e6+10;
 5 
 6 int c[maxn];
 7 int a[20005];
 8 int m1[20005],m2[20005];
 9 
10 int lowbit(int x)
11 {
12     return x&(-x);
13 }
14 
15 int sum(int x)
16 {
17     int ret=0;
18     while(x>0)
19     {
20         ret+=c[x];
21         x-=lowbit(x);
22     }
23     return ret;
24 }
25 
26 void add(int x,int d)
27 {
28     while(x<maxn)
29     {
30         c[x]+=d;
31         x+=lowbit(x);
32     }
33 }
34 
35 int main()
36 {
37     int t,n;
38     scanf("%d",&t);
39     while(t--)
40     {
41         memset(c,0,sizeof(c));
42         scanf("%d",&n);
43         for(int i=1;i<=n;i++)
44         {
45             scanf("%d",&a[i]);
46             m1[i]=sum(a[i]);
47             add(a[i],1);
48         }
49         memset(c,0,sizeof(c));
50         for(int i=n;i>=1;i--)
51         {
52             m2[i]=sum(a[i]);
53             add(a[i],1);
54         }
55         ll ans=0;
56         for(int i=1;i<=n;i++)
57             ans+=m1[i]*1ll*(n-i-m2[i])+m2[i]*1ll*(i-m1[i]-1);
58         printf("%lld\n",ans);
59     }
60 }

 

uvalive4329(树状数组)

标签:lld   const   ems   mem   return   cst   turn   code   i++   

原文地址:http://www.cnblogs.com/yijiull/p/6741623.html

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